-
-
Notifications
You must be signed in to change notification settings - Fork 174
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
zod's superRefine not working properly #538
Comments
I doubt that the problem is linked to Zod's superRefine since the same issue arises with other resolvers like Yup, as I have tested. Although formState.isValid is true without a resolver, it is initially false when used with a resolver. @bluebill1049 , could it be possible that there is something related to the usage of resolvers on the react-hook-form's end? |
Have you tried debug with the following:
|
@bluebill1049 I did it as I said in my previous reply |
Having the same issue when using the The |
I'm facing the same issue using |
Having the same issue as @GuhPires. |
Facing the same issues with |
@bluebill1049 Any thoughts? |
I will take a look at it 👍 |
Describe the bug To Reproduce Go to https://codesandbox.io/p/sandbox/elated-mendeleev-v92vvs Expected behavior Desktop (please complete the following information): OS: macOS Ventura 13 Please let me know if the way of implementation is wrong !! |
@bluebill1049 I noticed when trying to use the It would be nice if it could follow how |
hey @stramel 👋 I think the whole scheme validation is based on registered input, not sure if it's a straightforward implementation switch and |
Hey @bluebill1049 👋🏼 I'm currently using the Zod schema validation pretty easily but there are a couple of cases that aren't working well for me. I have:
The |
A separate issue, hook form is field level based for validation and consistent validation strategy. I don't have a solution in my head without re-render the entre form on each form state. |
That's fair, I will come up with a workaround for that. I will note though, that the |
Also facing this issue |
This only happens on first load but if you have a form and you submit or have it re-validate on change it will give you the error object. |
Any update on this? |
1 similar comment
Any update on this? |
Hi! Any update on this issue? I'm manually triggering the validation if isValid = false and all the inputs are filled. But it will be nice to have a native solution. |
I had same issue and I have tried adding proper path to .superRefine((data, ctx) => {
if (data.table === "linear" && data.dimension < 0.5) {
ctx.addIssue({
code: z.ZodIssueCode.too_small,
minimum: 0.5,
type: "string",
inclusive: true,
message: "Value cannot be smaller than 0.5.",
path: ["dimension"],
});
}} |
@FoiDot Would you mind sharing the code for your solution? I tried the following, but useEffect(() => {
if (!isValid) trigger('batches')
}, [isValid]) |
@stefan-girlich I did something like this: useEffect(() => {
// I have some inputs working with Controller and setState.
// That means mode = 'all' | 'onChange' | etc doesn't work
// So I need to clean the errors manually with the clearErrors from useForm.
if (isValid) clearErrors()
// Check if all your inputs are filled
else if (
!isValid &&
watch('bank') &&
watch('accountType') &&
watch('accountNumber')
)
trigger()
}, [isValid]) Make sure to have your path in your schema if (accountNumber.startsWith('10') && bank === '01')
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['accountNumber'],
message: `Your Account number cannot start with 10`,
fatal: true,
})
return z.NEVER |
following |
This fix worked for me. - path: [...ctx.path, idx, 'myProperty'],
+ path: [`[${idx}].myProperty`], Kudos @bluebill1049 for the debug code; I wouldn't have found this issue without it. 💯 |
I'm running into the same issue. In my case the superRefine is needing to flag an error on a field which has not been edited. This field is only required if another field has a given value. When this happens, RHF is recognizing that the form is invalid but doesn't add the error to the field or form. I can get around this, mostly, by using the trigger function on the form to tell RHF to validate the fields that were never edited. You can see this in the Codesandbox link in the original error report. When you run the sandbox as-is, the form is invalid with no errors. But if you add the following at line 24 then the error message appears on the form:
This is not quite optimal as I need to remember to call trigger to force RHF to fully recognize the validation error. Bottom line, if superRefine is flagging an error on a field which is not dirty (not touched?), somehow add a call to trigger tied to an event on the form which tells the form to check if the field is valid. For example:
|
Same issue here, I have an object where you need to fill in a start and end time, i have a refine on the object that checks if the start_time is before the end_time, i can throw the error on the start_time like this. export const CreateWorkActivityRequest = object({
start_time: date(),
end_time: date(),
}).refine(
(val) => {
if (val.type !== 'travel') {
return !isAfter(val.start_time, val.end_time)
}
return true
},
{
message: 'Start tijd is later dan eind tijd',
path: ['start_time']
}
) But i want to make use of the 'root' property in the errors so i can show the error at the end of the form like we do with server errors setError("root.serverError", {
type: "400",
}) but throwing this error will make the form pass but the object its sending is empty .refine(
(val) => {
if (val.type !== 'travel') {
return !isAfter(val.start_time, val.end_time)
}
return true
},
{
message: 'Start tijd is later dan eind tijd',
path: ['root', 'start_end_time_mismatch']
}
) with .superRefine(
(val, ctx) => {
if (val.type !== 'travel' && isAfter(val.start_time, val.end_time)) {
console.log('validation failed')
ctx.addIssue({
code: ZodIssueCode.invalid_date,
message: 'Start tijd is later dan eind tijd',
path: ['start_time'],
});
return NEVER
}
},
) but this doenst .superRefine(
(val, ctx) => {
if (val.type !== 'travel' && isAfter(val.start_time, val.end_time)) {
console.log('validation failed')
ctx.addIssue({
code: ZodIssueCode.invalid_date,
message: 'Start tijd is later dan eind tijd',
path: ['root', 'start_end_time_mismatch'],
});
return NEVER
}
},
) |
I'm having the same error |
This is still happening 😭 |
Same problem. Any resolution? |
Describe the bug
I want to use zod's
superRefine
validation function. hook form shows that form is invalid, buterrors
object is emptyTo Reproduce
Steps to reproduce the behavior:
isValid
isfalse
buterrors
object is emptyCodesandbox link (Required)
https://stackblitz.com/edit/vitejs-vite-dwvjn3?file=src/App.jsx
Expected behavior
error
object should haveBlock is too short
error messageDesktop (please complete the following information):
The text was updated successfully, but these errors were encountered: