All files / src/validation profileSchema.ts

100% Statements 57/57
100% Branches 1/1
100% Functions 1/1
100% Lines 57/57

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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 931x 1x 1x     1x 1x                       1x 2x   2x   2x 2x 2x 2x 2x       2x 2x 2x 2x 2x       2x 2x 2x 2x 2x       2x 2x 2x 2x 2x         2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x     2x 2x  
import { z } from 'zod'
import { toTypedSchema } from '@vee-validate/zod'
import {
  optionalEmptyToUndef,
} from '@/libs/vee/rules'
import { makeMsg, type TFunc } from '@/libs/vee/messages'
import {PATTERN} from "./pattern";
 
export type ProfileForm = {
  familyName: string
  givenName: string
  familyNameKana: string
  givenNameKana: string
  birthDate?: string | null
  genderId?: number | null
  initial?: string
}
 
export function makeProfileSchema(t: TFunc, L: Record<string, string>) {
  const m = makeMsg(t, L)
 
  const base = z.object({
    // 姓(必須)
    familyName: z
      .string()
      .trim()
      .nonempty(m.req('familyName'))
      .max(80, m.max('familyName', 80)),
    // 独自ルールで漢字ひらがなカタカナを追加したい
 
    // 名(必須)
    givenName: z
      .string()
      .trim()
      .nonempty(m.req('givenName'))
      .max(80, m.max('givenName', 80)),
    // 独自ルールで漢字ひらがなカタカナを追加したい
 
    // セイ(必須)
    familyNameKana: z
      .string()
      .trim()
      .nonempty(m.req('familyNameKana'))
      .max(80, m.max('familyNameKana', 80)),
    // 独自ルールでカタカナのみを追加したい
 
    // メイ(必須)
    givenNameKana: z
      .string()
      .trim()
      .nonempty(m.req('givenNameKana'))
      .max(80, m.max('givenNameKana', 80)),
    // 独自ルールでカタカナのみを追加したい
 
    // イニシャル(任意)
    // initial: stringMaxOptional(32, m.max('initial', 32)),
    initial: z
      .union([
        z
          .string()
          .min(1) // 入力された時は1文字以上
          .max(32, { message: m.max('initial', 32) })
          .regex(PATTERN.INITIAL, { message: m.regex('initial') }),
        z.literal(''), // 空文字は「未入力」として許容
        z.null(),
      ])
      .optional(),
 
    // 生年月日(任意、YYYY-MM-DD)
    // 空文字は optionalEmptyToUndef() 側で undefined に寄せる
    birthDate: optionalEmptyToUndef()
      .or(
        z
          .string()
          .regex(PATTERN.DATE, {message: m.date('birthDate')}),
      )
      .nullable()
      .optional(),
 
    // 性別ID(任意:number|null)
    genderId: z
      .number()
      .int()
      .positive(m.positive('genderId'))
      .max(255, m.max('genderId', 255))
      .nullable()
      .optional(),
  })
 
  // return toTypedSchema<ProfileForm>(base)
  return toTypedSchema(base)
}