All files / src/validation licenseSchema.ts

100% Statements 35/35
100% Branches 3/3
100% Functions 2/2
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 461x 1x 1x 1x 1x             1x 5x 5x 5x 5x 5x   5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x 5x 5x 5x   5x 5x   1x 4x 4x 4x 4x 4x  
import { z } from 'zod'
import { toTypedSchema } from '@vee-validate/zod'
import { makeMsg, type TFunc } from '@/libs/vee/messages'
import {emptyToNull} from '@/utils'
import {PATTERN} from './pattern'
 
export type LicenseFields = {
  name: string | null;
  eventDate: string | null;
}
 
export function makeLicenseZodSchema(
  t: TFunc,
  L: Record<string, string>,
) {
  const m = makeMsg(t, L)
  return z.object({
 
    name: z.preprocess(
    emptyToNull,
    z
      .string({ invalid_type_error: m.req('name'), required_error: m.req('name') })
      .trim()
      .min(1, m.req('name'))
      .nonempty(m.req('name'))
      .nullable()
      .refine((v) => v !== null, { message: m.req('name') }),
    ),
 
    eventDate: z.preprocess(
    emptyToNull,
    z
      .string()
      .regex(PATTERN.DATE_YM, {message: m.date('eventDate')})
    )
 
  })
}
 
export function makeLicenseSchema(
  t: TFunc,
  L: Record<string, string>,
) {
  return toTypedSchema(makeLicenseZodSchema(t, L))
}