Zod validator calls refine and ignoring other validators #3060
-
Hi, So I tried to use refine(). validate: zodResolver(
z.object({
url: z.string()
.min(1, "This is a required field")
.refine((value) => {
try {
new URL(value);
return true;
} catch {
return false;
}
}, "Invalid URL")
})
) for some reason zod ignores '.min' validator and calls always refine(). Is this an issue or I just don't understand something? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
Zod tries to run all of the validation "chain", so that you would get as many of the errors as possible. Therefore, it will run a refine even if the prior checks failed. |
Beta Was this translation helpful? Give feedback.
-
Is there any way to make it stop on the first failure? |
Beta Was this translation helpful? Give feedback.
-
not that I know of, but perhaps someone else would know. |
Beta Was this translation helpful? Give feedback.
-
However this kind of validation works as expected:
Chained validation, showed first appeared error. it is like something wrong with refine chaining. |
Beta Was this translation helpful? Give feedback.
Zod tries to run all of the validation "chain", so that you would get as many of the errors as possible. Therefore, it will run a refine even if the prior checks failed.