8000 v4: Custom error message for .regex() is ignored · Issue #4098 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
v4: Custom error message for .regex() is ignored #4098
Closed
@rezarahem

Description

@rezarahem

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0