question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Schema only validating `.refine` when `object` passes.

See original GitHub issue

I’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:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
scotttrinhcommented, Nov 30, 2021

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!

0reactions
ricardo-rpcommented, Nov 26, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found