Skip to content

Commit 49c02fd

Browse files
committed
update post
1 parent d057d68 commit 49c02fd

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

content/posts/프로젝트/당근마켓 클론코딩/당근마켓 클론코딩으로 Zod 배우기.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tags:
55
- study
66
- zod
77
createdAt: 2025-06-30 21:21:49
8-
modifiedAt: 2025-07-02 18:42:59
8+
modifiedAt: 2025-07-02 18:55:04
99
publish: 프로젝트/당근마켓 클론코딩
1010
related:
1111
- "[[당근마켓 클론코딩]]"
@@ -344,6 +344,45 @@ const formSchema = z
344344
})
345345
.refine((form) => form.password == form.conformPassword, {
346346
message: "비밀번호가 일치하지 않음",
347-
path: ["confirmPassword"],
347+
path: ["confirmPassword"], // 특정 필드에 에러 전가
348348
});
349349
```
350+
351+
### regex 검증
352+
353+
regex를 활용해 검증할수도 있다.
354+
355+
```typescript
356+
const passwordRegex = new RegExp(
357+
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*?[$?!@$%^&*-]).+$/,
358+
);
359+
360+
const formSchema = z.object({
361+
username: z.string().min(3).max(10),
362+
email: z.string().email(),
363+
password: z
364+
.string()
365+
.min(10)
366+
.regex(
367+
passwordRegex,
368+
"비밀번호는 소문자, 대문자, 숫자, 특수문자를 포함해야한다.",
369+
),
370+
confirmPassword: z.string().min(10),
371+
});
372+
```
373+
374+
### 데이터 변환
375+
376+
입력받은 데이터를 검증하는 기능 외에도 변환하는 기능도 존재한다.
377+
378+
```typescript
379+
const formSchema = z.object({
380+
username: z.string().min(3).max(10).trim().toLowerCase(),
381+
email: z.string().email(),
382+
password: z.string().min(10),
383+
confirmPassword: z.string().min(10),
384+
});
385+
```
386+
387+
- `toLowerCase`메소드: 입력받은 데이터를 소문자로 변환
388+
- `trim`메소드: 입력받은 데이터의 시작과 끝의 빈 문자열을 삭제해줌

public/meta-data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@
579579
"tags": ["project", "carrot-market", "study", "zod"],
580580
"series": "",
581581
"createdAt": "2025-06-30 21:21:49",
582-
"modifiedAt": "2025-07-02 18:42:59",
582+
"modifiedAt": "2025-07-02 18:55:04",
583583
"publish": "프로젝트/당근마켓 클론코딩"
584584
},
585585
{

0 commit comments

Comments
 (0)