-
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.
feat: add async email validation WIP
- Loading branch information
1 parent
b99fbce
commit 3c29c3d
Showing
7 changed files
with
134 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function debounce< | ||
ParamsT, | ||
ReturnT, | ||
F extends (...args: ParamsT[]) => Promise<ReturnT>, | ||
>(func: F, wait: number) { | ||
let timeoutId: NodeJS.Timeout | null = null; | ||
return (...args: Parameters<F>): Promise<ReturnType<F>> => { | ||
return new Promise((resolve, reject) => { | ||
if (timeoutId) { | ||
clearTimeout(timeoutId); | ||
} | ||
timeoutId = setTimeout(async () => { | ||
try { | ||
const result = (await func(...args)) as ReturnType<F>; | ||
resolve(result); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}, wait); | ||
}); | ||
}; | ||
} | ||
|
||
export { debounce }; |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { emailValidation } from "@/httpClient"; | ||
import { debounce } from "./debounce"; | ||
import type { | ||
EmailValidationRequest, | ||
EmailValidationResponse, | ||
} from "../../httpClient"; | ||
|
||
const DEBOUNCE_TIME_MS = 2000; | ||
|
||
const debouncedValidateEmail = debounce< | ||
EmailValidationRequest, | ||
EmailValidationResponse, | ||
typeof emailValidation | ||
>(async (request: EmailValidationRequest) => { | ||
return await emailValidation(request); | ||
}, DEBOUNCE_TIME_MS); | ||
|
||
export { debouncedValidateEmail }; |
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,40 +1,40 @@ | ||
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." }); | ||
.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().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; | ||
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 }; |