Schema only validating `.refine` when `object` passes.
See original GitHub issueI’m using zod together with react-hook-form to create a user signup form. The schema is as follows: I am unsure as to whether this issue goes in react-hook-form/resolvers or here, so I am asking for support in both repos.
const schema = z
.object({
name: z.string().nonempty('message'),
email: z.string().nonempty('message').email('message'),
password: edukPassword({ nonEmpty: 'message' }),
confirmPassword: z.string(),
cellphone: brazilianCellphoneNumber().nonempty('message'),
privacyTermsAccepted: z.literal(true, {invalid_type_error: 'message',}),
})
.refine(({ password, confirmPassword }) => password === confirmPassword, {
message: 'Passwords must match',
path: ['confirmPassword'],
})
export const registerFormResolver = zodResolver(registerFormSchema)
The problem is, I am only getting an error on .refine
when .object
has passed. This results in the user not knowing they have entered an incorrect password confirmation until after they filled out the whole form.
Have I incorrectly built the schema for this useCase, or is the resolver incorrectly returning me formErrors?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
Refine validations on object definitions don't get triggered until ...
My point is still that refine does not get triggered until all the fields in the object definition is passed in.
Read more >Only allow properties that are declared in JSON schema
I am using json-schema and wanting to only allow properties that are declared in this file to pass validation. For instance if a...
Read more >It all starts with applicability - JSON Schema Fundamentals ...
Validation still passes. This is because subschemas in properties are applied to the instance values ONLY when the keys match.
Read more >Modifying data during validation - Ajv JSON schema validator
It is not possible to modify the root data instance passed to the validation function, only data properties can be modified.
Read more >How To Use Schema Validation in MongoDB - DigitalOcean
For the database document itself, the expected type is object . This means that you can only add objects — in other words,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
FWIW, Zod is intentionally designed to parse-first and then validate, so this is working as expected. Going to close, but there is definitely a need for a tool (or set of tools) that sit between Zod and common IO boundaries that can help with some of the boilerplate associated with things like form validation, and hopefully someone with some experience or need for this in the community will come forward with something there!
I managed to solve it by replacing
.literal
with.boolean
and using.refine
to check wether the boolean is true. Here’s a fork with the solution. Still, it would be nice to know whether this is a bug or I was just improperly using.literal