Closed
Description
In Zod v4 beta, custom error messages passed to .regex() are not working as expected.
import { z } from 'zod';
export const PhoneNumberSchema = z
.string()
.trim()
.regex(/^09\d{9}$/, {
error: 'Phone number must be 11 digits and start with 09',
// also tried using `message` — neither works
});
Expected behavior
Zod should return this custom message:
Phone number must be 11 digits and start with 09
Actual behavior
{
"issues": [
{
"origin": "string",
"code": "invalid_format",
"format": "regex",
"pattern": "/^09\\d{9}$/",
"path": [],
"message": "Invalid string: must match pattern /^09\\d{9}$/"
}
]
}
This happens with both error
and message
keys — neither overrides the default error.
Thanks
Metadata
Metadata
Assignees
Labels
No labels