-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: password validation error message
- Loading branch information
1 parent
58df65e
commit b99fbce
Showing
7 changed files
with
115 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,40 @@ | ||
import { z } from "zod"; | ||
import { z, ZodIssueCode } from "zod"; | ||
|
||
const nameSchema = z | ||
.string() | ||
.min(2, { message: "Name must be at least 2 characters long." }) | ||
.max(40, { message: "Name must be at most 40 characters long." }); | ||
|
||
const passwordSchema = z | ||
.string() | ||
.min(4, { message: "Password must be at least 4 characters long." }) | ||
.max(40, { message: "Password must be at most 40 characters long." }) | ||
.refine((v) => !/\s/.test(v), { | ||
message: "Password cannot contain whitespace.", | ||
}) | ||
.refine((v) => /[A-Z]/.test(v), { | ||
message: "Password must contain at least one uppercase letter.", | ||
}) | ||
.refine((v) => /\d/.test(v), { | ||
message: "Password must contain at least one digit.", | ||
}) | ||
.refine((v) => /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/.test(v), { | ||
message: "Password must contain at least one special character.", | ||
}) | ||
.refine((v) => /[a-z]/.test(v), { | ||
message: "Password must contain at least one lowercase letter.", | ||
}); | ||
const passwordSchema = z.string().superRefine((val, ctx): val is string => { | ||
const issues: string[] = []; | ||
if (val.length < 4) { | ||
issues.push("at least 4 characters"); | ||
} | ||
if (val.length > 40) { | ||
issues.push("less then 40 characters"); | ||
} | ||
if (/\s/.test(val)) { | ||
issues.push("no whitespace"); | ||
} | ||
if (!/[A-Z]/.test(val)) { | ||
issues.push("at least one uppercase letter"); | ||
} | ||
if (!/\d/.test(val)) { | ||
issues.push("at least one digit"); | ||
} | ||
if (!/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/.test(val)) { | ||
issues.push("at least one special character"); | ||
} | ||
if (!/[a-z]/.test(val)) { | ||
issues.push("at least one lowercase letter"); | ||
} | ||
if (issues.length) { | ||
ctx.addIssue({ | ||
code: ZodIssueCode.custom, | ||
message: `Password must contain ${issues.join(", ")}.`, | ||
}); | ||
} | ||
return false; | ||
}); | ||
|
||
export { passwordSchema, nameSchema }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters