Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[장문원] week14 #451

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor: schema 유효성 검사 유틸 분리
  • Loading branch information
jangmoonwon committed May 19, 2024
commit 78b5907e50eb6affd97953985750aba0e447c8d1
64 changes: 64 additions & 0 deletions lib/schema.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jangmoonwon

음 각 필드 요소에 대해 스키마를 짜기보단,

user가 지녀야 하는 정보에 대한 스키마라면 userSchema로,
로그인 인증을 위해 필요한 데이터 스키마라면 loginAuthSchema로,
회원가입을 위해 필요한 스키마라면 registerAuthSchema로 구분짓는게 좋겠어요

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { z } from "zod";

const PWD_VALIDATION = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/;

function emailSchema() {
return z
.string()
.min(1, { message: "이메일을 입력해 주세요." })
.email({ message: "올바른 이메일 주소가 아닙니다." });
}

function emailCheckSchema() {
return z
.string()
.min(1, { message: "이메일을 입력해 주세요." })
.email({ message: "올바른 이메일 주소가 아닙니다." })
.refine((data) => data !== "test@codeit.com", {
message: "이미 사용 중인 이메일입니다.",
path: ["email"],
});
}

function passwordSchema() {
return z
.string()
.min(1, { message: "비밀번호를 입력해 주세요." })
.max(16, { message: "최대 16자리입니다." });
}

function passwordCheckSchema() {
return z
.string()
.min(1, { message: "비밀번호를 입력해 주세요." })
.max(16, { message: "최대 16자리입니다." })
.regex(PWD_VALIDATION, {
message: "영문, 숫자를 조합해 8자 이상 입력해 주세요.",
});
}

//signin schema
export type FormFields = z.infer<typeof schema>;

export const schema = z.object({
email: emailSchema(),
password: passwordSchema(),
});

//signup schema
export type FormFieldsCheck = z.infer<typeof schemaCheck>;

export const schemaCheck = z
.object({
email: emailCheckSchema(),
password: passwordCheckSchema(),
passwordCheck: passwordCheckSchema(),
})
.refine((data) => data.email !== "test@codeit.com", {
message: "이미 사용 중인 이메일입니다.",
path: ["email"],
})
.refine((data) => data.password === data.passwordCheck, {
message: "비밀번호가 일치하지 않아요.",
path: ["passwordCheck"],
});