Open iwannatto opened 3 months ago
I’ve encountered the same issue with z.union()
ignoring custom error messages. As a workaround, I’ve used a combination of optional
and superRefine
to ensure that my custom error message is respected. Here’s how I approached it:
import { z } from 'zod';
const schema = z.union([
kuwaitAddress,
baseAddress,
z.coerce.number({
required_error: path.addressNumber,
message: path.addressNumber,
invalid_type_error: path.addressNumber,
}),
])
.optional()
.superRefine((val, ctx) => {
if (val === undefined) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: path.address,
});
}
});
In this code, I expect that
result.error.errors
emit my custom error message. But I gotIs this specification or bug?