-
Notifications
You must be signed in to change notification settings - Fork 6
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
RFC: add the possibility to validate with Zod schema #21
Comments
After thinking about validation with Zod I thought about partial validation. const validationSchema = z.object({
firstName: z.string(),
lastName: z.string(),
})
const result = await validationSchema.shape.firstName.safeParseAsync(values.firstName) But there is one limitation: if we use At the moment the only idea I had is:
|
@Epimodev I'm not sure about the Maybe we could replace this: schema: z
.object({
firstName: z.string(),
lastName: z.string(),
})
.superRefine(({ firstName, lastName }, context) => {
// Makes last name required only if first name is provided
if (firstName.length > 0 && lastName.length === 0) {
context.addIssue({
code: z.ZodIssueCode.custom,
message: "Last name is required",
});
}
}) With an API like this: // values is a Proxy object (or it could be a function to get the value)
schema: values =>
z.object({
firstName: z.string(),
lastName: z
.string()
.refine(
value => values.firstName.length > 0 && value.length === 0,
"Last name is required",
),
}) |
Yes I agree revalidate all fields will be expensive and might make inputs laggy. |
@Epimodev Hmmm, indeed this does not solve the issue either. |
I checked Zod returns different type of object if we add
|
According to last discussion we had about validation depending on other values, maybe the best solution will be adding a new param const {} = useForm({
firstName: {
initialValue: "",
validationDependency: ["lastName"],
validate: ({ lastName }) => z.string().refine(
value => values.firstName.length > 0 && value.length === 0,
"First name is required", // only if last name is empty
),
},
lastName: {
initialValue: "",
}
}) The challenge here is making What do you think? |
The goal of this issue is finding the best way to add form validation with Zod.
Why using Zod for form validation
Zod is schema declaration and validation library containing a lot of built in validators.
It makes also combining several validators easily and we can setup generic error message and set specific error messages if needed.
Suggested update
I think we can add Zod schema validation without any breaking change by:
useForm
, add a second parametervalidationSchema
of typeZodType<Values>
It let the user use the old validation API by field. ButvalidationSchema
will overwritevalidate
set by field.Edit: To make the API opinionated and avoid user to think about different way to validate and have inconsistencies in the codebase, we'll remove the current API (and create a breaking change).
How to handle different validation use case we have at Swan:
Basic validation
Custom validation
Edit:
To be able to use reusable custom validation without rewrite the error message each time, we'll use
superRefine
like this:Validation of a field depending on another field
source: https://medium.com/@mobile_44538/conditional-form-validation-with-react-hook-form-and-zod-46b0b29080a3
Async validation of a field
Error messages
By default Zod generate error messages depending on the schema declaration, but they are only in english and might not be user friendly.
First way (not recommanded): set error message in schema declaration for each field
The technically easiest way to fix this is setting custom message in all validators but it will be very painful to maintain and if we miss one case, the user will see error messages with a bad ux-writting.
Better way but not idea: Global error map
https://zod.dev/ERROR_HANDLING?id=global-error-map
Zod gives the possibility to set
ErrorMap
to transform default error message to translated messages with a custom ux-writting.We can set it globally but if we use Zod for other kind of validation this isn't ideal.
Even better solution: add the possibility to set error map in react-ux-form
https://zod.dev/ERROR_HANDLING?id=contextual-error-map
Zod gives the possibiliy to set error map when we parse the input. As parsing will be done in react-ux-form, I think we could expose a function like
setZodErrorMap
giving the possibility to set error map for all our forms (we could call this function inutils/i18n.ts
for example)The text was updated successfully, but these errors were encountered: