Z
Zod11mo ago
Gludek

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
Gludek
Gludek11mo ago
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
Scott Trinh
Scott Trinh11mo ago
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.
Gludek
Gludek11mo ago
Yeah, That was also a thing I checked, I'm not sure whether I'm using this incorrectly or what
Scott Trinh
Scott Trinh11mo ago
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.
Scott Trinh
Scott Trinh11mo ago
peaceful-leftpad-wcncvr
CodeSandbox is an online editor tailored for web applications.
Gludek
Gludek11mo ago
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
{
prefixIndex: 196,
patientBasicData: {
patientCode: "G00001", // If code like this would exsit then validation should fail
devicesIssued: false,
group: 0,
localization: 1,
age: 120,
sex: "male",
height: 150,
weight: 50,
inclusionState: 0,
},
}
{
prefixIndex: 196,
patientBasicData: {
patientCode: "G00001", // If code like this would exsit then validation should fail
devicesIssued: false,
group: 0,
localization: 1,
age: 120,
sex: "male",
height: 150,
weight: 50,
inclusionState: 0,
},
}
to something like this
{
prefixIndex: 196,
patientBasicData: {
patientCode: "G00001", // If code like this would exsit then validation should fail
devicesIssued: true,
group: 0,
localization: 1,
age: 120,
sex: "male",
height: 150,
weight: 50,
inclusionState: 0,
},
}
{
prefixIndex: 196,
patientBasicData: {
patientCode: "G00001", // If code like this would exsit then validation should fail
devicesIssued: true,
group: 0,
localization: 1,
age: 120,
sex: "male",
height: 150,
weight: 50,
inclusionState: 0,
},
}
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?
Scott Trinh
Scott Trinh11mo ago
Yeah that sounds plausible. I guess they're missing a state where the field has been touched but it's waiting on asynchronous validation?