diff --git a/src/pages/auth/SignUp.tsx b/src/pages/auth/SignUp.tsx index 8d391488..66eb7629 100644 --- a/src/pages/auth/SignUp.tsx +++ b/src/pages/auth/SignUp.tsx @@ -1,14 +1,17 @@ import Image from 'next/image'; import Link from 'next/link'; import AuthLayout from '@/pageLayout/AuthLayout/AuthLayout'; -import z from 'zod'; +import { zodResolver } from '@hookform/resolvers/zod'; +import { PostSignUpRequest, PostSignUpRequestType } from '@/schema/auth'; import { useForm } from 'react-hook-form'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'; export default function SignUp() { - const form = useForm>({ + const form = useForm({ + resolver: zodResolver(PostSignUpRequest), + mode: 'onBlur', defaultValues: { email: '', password: '', @@ -17,13 +20,6 @@ export default function SignUp() { }, }); - const formSchema = z.object({ - email: z.string(), - password: z.string(), - passwordConfirmation: z.string(), - nickname: z.string(), - }); - return (
@@ -37,11 +33,16 @@ export default function SignUp() { ( + render={({ field, fieldState }) => ( - 이메일 + 이메일 - + @@ -51,11 +52,16 @@ export default function SignUp() { ( + render={({ field, fieldState }) => ( - 비밀번호 + 비밀번호 - + @@ -64,10 +70,15 @@ export default function SignUp() { ( + render={({ field, fieldState }) => ( - + @@ -77,18 +88,27 @@ export default function SignUp() { ( + render={({ field, fieldState }) => ( - 닉네임 + 닉네임 - + )} /> - diff --git a/src/schema/auth.ts b/src/schema/auth.ts new file mode 100644 index 00000000..5adda753 --- /dev/null +++ b/src/schema/auth.ts @@ -0,0 +1,20 @@ +import z from 'zod'; + +const PWD_VALIDATION = /^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{8,}$/; +export const PostSignUpRequest = z + .object({ + email: z.string().min(1, { message: '이메일은 필수 입력입니다.' }).email({ message: '이메일 형식으로 작성해 주세요.' }), + password: z + .string() + .min(1, { message: '비밀번호는 필수 입력입니다.' }) + .min(8, { message: '비밀번호는 최소 8자 이상입니다.' }) + .regex(PWD_VALIDATION, { message: '비밀번호는 숫자, 영문, 특수문자로만 가능합니다.' }), + passwordConfirmation: z.string().min(1, { message: '비밀번호 확인을 입력해주세요.' }), + nickname: z.string().min(1, { message: '닉네임은 필수 입력입니다.' }).max(20, { message: '닉네임은 최대 20자까지 가능합니다.' }), + }) + .refine((data) => data.password === data.passwordConfirmation, { + message: '비밀번호가 일치하지 않습니다.', + path: ['passwordConfirmation'], + }); + +export type PostSignUpRequestType = z.infer;