Skip to content
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

Does not support errors coming from zodSchema.refine() #15

Open
shobhanbiswas11 opened this issue Oct 28, 2022 · 7 comments
Open

Does not support errors coming from zodSchema.refine() #15

shobhanbiswas11 opened this issue Oct 28, 2022 · 7 comments

Comments

@shobhanbiswas11
Copy link

Describe the bug
Does not parse properly if the error is coming from refine or superRefine

To Reproduce

const formSchema = z
  .object({
    username: z.string().email(),
    firstName: z.string(),
    middlerName: z.string().optional(),
    lastName: z.string(),
    password: z.string(),
    confirmPassword: z.string(),
    status: z.enum(['active', 'inactive']),
  })
  .refine((data) => data.password !== data.confirmPassword, {
    message: "Passwords don't match",
    path: ['confirmPassword'], // path of error
  });

if the values are these

{
      username: 'name',
      firstName: 'fname',
      lastName: 'lname',
      middlerName: 'mname',
      password: 'ss',
      confirmPassword: 'ssad',
      status: 'active',
}

It does not add any error for confirmPassword field with message "Passwords don't match"

Node version:
v16.15.0

Zod, Formik and zod-formik-adapter versions:

  • Zod: 3.19.1
  • Formik: 2.2.9
  • zod-formik-adapter: 1.1.1
@iest
Copy link

iest commented Nov 14, 2022

I ran into this issue — it actually does work; however the refine & superRefine methods don't get called if there are validation errors coming from inside that object — so only if there are no other errors will the errors from refine/superRefine actually surface.

This is an issue with how Zod works, rather than this library.

Edit: Actually you're right; the errors don't surface at all with this package. I copy-pasted the main index.ts into my codebase and got it working like I detailed above. Super weird

@robertLichtnow
Copy link
Owner

@iest can you add which versions of Zod and Formik you were using when testing?

@iest
Copy link

iest commented Jan 5, 2023

@robertLichtnow not 100% sure as it's been a few months, but my yarn.lock says formik: ^2.2.9 and zod: ^3.19.1

@alexkuc
Copy link

alexkuc commented Mar 9, 2023

I observed the same behaviour and noticed the following… In my case, I have custom input control driven by Formik's useField() hook. It's a custom combobox with ability to specify custom value. Here are my observations where .refine is defined for all conditions:

  • when I specify custom error message via helpers.setError(), .refine() is ignored
  • when I specify custom error value via helpers.setValue(), .refine overrides with its own validation
  • when I specify both, .refine() overrides with its own validation
  • when I specify no custom error message and no custom error value, .refine() is ignored

Edit: added 4th bullet point

@alexkuc
Copy link

alexkuc commented Mar 10, 2023

Another observation I noticed today. If you define .refine() on schema itself, not individual field, it is also ignored.

@veksen
Copy link

veksen commented Sep 7, 2023

Is there a workaround people are using for this? I'd specifically like to do password confirmation

@sendyputra
Copy link

try assign validate to formik validate={toFormikValidate(Schema)} and its work

Screen Shot 2023-09-12 at 16 50 02 Screen Shot 2023-09-12 at 16 54 05 Screen Shot 2023-09-12 at 16 51 51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants