form.errors in Conform is empty when returning falsy value from zod string().transform() method #348
-
I've noticed that when I return a falsy value from a Here's a codesandbox that I put together to troubleshoot this:This is basically what I'm facing in my code, so I'm wondering if I'm doing something wrong, or if there's an actual issue with either Zod or Conform. This is the same situation I have on my project: Check line 63 to see the field And check line 175 where I have @edmundhung I would really appreciate your input :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @waldothedeveloper, thanks for preparing a sandbox :) I notice one problem with the issue path when you are transforming the const schema = z.object({
addOn: z.string().transform((val, ctx) => {
if (val !== "bananas") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "The value is not equal to bananas.",
// The path below make it refers to `addOn.addOn` instead of `addon`, remove it will fix the problem
path: ["addOn"],
});
return null;
}
return val;
}),
}); The problem above is that both zod const schema = z
.object({
addOn: z.string(),
})
.transform((val, ctx) => {
if (val.addOn !== "bananas") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "The value is not equal to bananas.",
// This will work
path: ["addOn"],
});
return null;
}
return val;
}); This change will get you the error you expected on const schema = z
.object({
addOn: z.string(),
})
.transform((val, ctx) => {
if (val.addOn !== "bananas") {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "The value is not equal to bananas.",
// We didn't set any path here, so it use the root path
});
return null;
}
return val;
}); With this setup, you will get the error on |
Beta Was this translation helpful? Give feedback.
Hi @waldothedeveloper, thanks for preparing a sandbox :)
I notice one problem with the issue path when you are transforming the
addons
:The problem above is that both zod
transform
orrefine
has a concept of base path. As you are transforming at theaddOn
level, it usesaddOn
as a base p…