.superRefine should have some information about previous errors
Opened this issue · 1 comments
Hi, so most of the time we don't want to run refine or superRefine if any previous condition has errors. To control this behaviour, there should be a way to at least get if there are any previous errors. Then we can return a fatal error from superRefine.
Otherwise such as in the case below, I will have to call the api even when I could simple prevent it for not having appropriate data.
This can be checking if email already exists in the database, we must know if the fields which can be checked offline are error free, only then we will call api, which will reduce load on server, also faster feedback to users, otherwise users will have to wait for api everytime they clicks submit even when there is no need (eg: when there are errors with fields that can be checked offline)
`
const formSchema = z
.object({
email: z.string().email({ message: "Please enter a valid email" }),
password: z
.string()
.min(8, { message: "Password must be at least 8 characters" })
.max(16, { message: "Password must be at most 16 characters" }),
})
.superRefine(async (data, ctx) => {
const { success, message, status } = await validateLogin(
data.email,
data.password
);
if (!success) {
ctx.addIssue({
path: ["email"],
code: z.ZodIssueCode.custom,
message: message || "Failed to login, try again",
});
}
});
`
@Codename-404 see this example that conform validation library uses to solve the problem.
They are using z.pipe()
to achieve something similar.