|
| 1 | +import { type FormValues, type FormFieldMap, type FormValidators } from "../src"; |
| 2 | +import { REGEX, regexValidation } from "./utils"; |
| 3 | + |
| 4 | +export const fields = { |
| 5 | + email: { |
| 6 | + type: "input", |
| 7 | + props: { |
| 8 | + label: "Email Address", |
| 9 | + placeholder: "john-doe@aaa.com", |
| 10 | + description: "Your work email address", |
| 11 | + }, |
| 12 | + }, |
| 13 | + password: { |
| 14 | + type: "input", |
| 15 | + props: { |
| 16 | + type: "password", |
| 17 | + label: "Password", |
| 18 | + placeholder: "Strong password", |
| 19 | + description: REGEX.password.description, |
| 20 | + }, |
| 21 | + }, |
| 22 | + passwordRepeat: { |
| 23 | + type: "input", |
| 24 | + props: { |
| 25 | + type: "password", |
| 26 | + label: "Repeat Password", |
| 27 | + placeholder: "Repeat password", |
| 28 | + }, |
| 29 | + }, |
| 30 | +} satisfies FormFieldMap; |
| 31 | + |
| 32 | +export type Values = FormValues<typeof fields>; |
| 33 | + |
| 34 | +export const validators = { |
| 35 | + email: (values) => { |
| 36 | + if (!values.email?.toString().trim().length) return "Required field"; |
| 37 | + return regexValidation(String(values.email), "email"); |
| 38 | + }, |
| 39 | + password: (values) => { |
| 40 | + if (!values.password?.toString().trim().length) return "Required field"; |
| 41 | + return regexValidation(String(values.password), "password"); |
| 42 | + }, |
| 43 | + passwordRepeat: (values) => { |
| 44 | + if (!values.passwordRepeat?.toString().trim().length) return "Required field"; |
| 45 | + if (values.password !== values.passwordRepeat) return "Passwords must be equal"; |
| 46 | + return regexValidation(String(values.passwordRepeat), "password"); |
| 47 | + }, |
| 48 | +} satisfies FormValidators<typeof fields>; |
| 49 | + |
| 50 | +export const values = { |
| 51 | + email: "", |
| 52 | + password: "", |
| 53 | + passwordRepeat: "", |
| 54 | +} satisfies FormValues<typeof fields>; |
0 commit comments