From 71abc48fd4525b9194df848a6915ab4dfc11c354 Mon Sep 17 00:00:00 2001 From: Masaya Tojo Date: Sun, 1 Sep 2024 22:36:21 +0900 Subject: 確認画面まで作成する MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/signUpSchema.ts | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/signUpSchema.ts (limited to 'src/signUpSchema.ts') diff --git a/src/signUpSchema.ts b/src/signUpSchema.ts new file mode 100644 index 0000000..4d8b71f --- /dev/null +++ b/src/signUpSchema.ts @@ -0,0 +1,51 @@ +import * as z from "zod" +import parsePhoneNumber from 'libphonenumber-js' + +const kanaRegex = /^[ァ-ン]+$/; + +const form1SchemaWithoutRefine = z.object({ + name: z.string().min(1, { message: '必須項目です' }), + kana: z.string().min(1, { message: '必須項目です' }).regex(kanaRegex, { message: 'カタカナを入力してください' }), + tel1: z.string(), + tel2: z.string(), + tel3: z.string(), +}) + +const addRefine = (schema: Form1) => ( + schema.refine( + ({ tel1, tel2, tel3 }) => (tel1.length > 0 && tel2.length > 0 && tel3.length > 0), + { + message: '必須項目です', + path: ['tel3'], + } + ).refine( + ({ tel1, tel2, tel3 }) => { + const phoneNumber = parsePhoneNumber('+81' + `${tel1}${tel2}${tel3}`) + console.log([`${tel1}${tel2}${tel3}`, phoneNumber?.isValid()]) + return phoneNumber?.isValid() + }, + { + message: '電話番号が不正です', + path: ['tel3'], + } + )) + + +export type Form1 = z.infer + +export const form2Schema = z.object({ + email: z.string().min(1, { message: '必須項目です' }).email({ message: 'メールアドレスを入力してください' }), + password: z.string() + .min(1, { message: '必須項目です' }) + .min(12, { message: '12文字以上で入力してください' }) + .max(128, { message: '128文字以下で入力してください' }) +}) + +export type Form2 = z.infer + +const formSchemaWithoutRefine = form1SchemaWithoutRefine.merge(form2Schema) + +export type Form = z.infer + +export const form1Schema = addRefine(form1SchemaWithoutRefine) +export const formSchema = addRefine(formSchemaWithoutRefine) -- cgit v1.2.3