Hey I have a schema where I need to
Hey, I have a schema where I need to validate one field based on API request. Basically I need to check if given code has not been used already. The issue is that for some reason if I use await in superRefine it makes other fields act weird. Like if they are validaiting twice/waiting for validation of the API based field, and it sometimes leads to situation where it shows field as invalid even though it has valid value.
8 Replies
I'm talking about form, using Formik and this https://github.com/robertLichtnow/zod-formik-adapter to integrate them
GitHub
GitHub - robertLichtnow/zod-formik-adapter: An adapter of zod objec...
An adapter of zod object validation to Formik validation schema - GitHub - robertLichtnow/zod-formik-adapter: An adapter of zod object validation to Formik validation schema
Can you make a minimum reproduction of just the schema (without the Formik) part on something like CodeSandbox or StackBlitz?
I see that the adapter does run
safeParseAsync
, so hopefully it's not a bug in the adapter, but trying to troubleshoot code outside of Zod is quite a time sink, and we'd rather focus on identifying any issues that Zod itself might have.Does that work for you? https://codesandbox.io/s/typescript-playground-export-forked-yr67qy?file=/index.ts
Gludek
CodeSandbox
TypeScript Playground Export (forked) - CodeSandbox
TypeScript playground exported Sandbox
Yeah, That was also a thing I checked, I'm not sure whether I'm using this incorrectly or what
Can you give me a valid value that passes the schema and something that doesn't? Sorry, I should've pointed you at our pinned messages which have some structure built out a bit more to easily test success/failure cases (like https://codesandbox.io/p/sandbox/zod-template-r67dv)
CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Copied over what you had to here: https://codesandbox.io/p/sandbox/peaceful-leftpad-wcncvr?file=%2Fyarn.lock%3A1%2C1
peaceful-leftpad-wcncvr
CodeSandbox is an online editor tailored for web applications.
Sorry for late response, I had storm later and then went to sleep, since I'm from Poland and it was like 8 pm. It's not really the issue that it some data doesn't pass, but it looks like it tries to first run validation on value before change and then after change.
like let's say I'm going from
to something like this
technically both are valid, but when changing field value it would flash with error
oooh I actually might have idea why it behaved like that, but no idea how to prevent it. If i'm correct then it wouldn't be error on any part
Formik shows error only when there's error (and since fields don't have values at start there is most likely error there) AND the fields have been touched. I think it runs validation, and sets the field to touched, but due to await there's delay before schema gets validated and that's why other fields flash
Does that sound plausible?
Yeah that sounds plausible. I guess they're missing a state where the field has been touched but it's waiting on asynchronous validation?