Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email validation fails on ampersand "&" sign #3411

Closed
cbosman-docksters opened this issue Apr 17, 2024 · 2 comments
Closed

Email validation fails on ampersand "&" sign #3411

cbosman-docksters opened this issue Apr 17, 2024 · 2 comments

Comments

@cbosman-docksters
Copy link

cbosman-docksters commented Apr 17, 2024

Hi there,

I noticed the z.string().email() fails when the email contains an ampersand sign. This should be allowed.

Example:
z.string().email().parse("test&[email protected]")

@hkbertoson
Copy link

It looks like this was supported at one point. But it was removed.

@colinhacks
Copy link
Owner

Per #2157 Zod implements "Gmail rules" by default for email address validation.

You can use .superRefine for custom behavior:

const emailRegex =
  /^(?!\.)(?!.*\.\.)([A-Z0-9_'+-\.]*)[A-Z0-9_'+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;

const emailSchema = z.string().superRefine((data, ctx) => {
  if (!emailRegex.test(data)) {
    ctx.addIssue({
      code: z.ZodIssueCode.invalid_string,
      message: "Invalid email address",
      validation: "email",
    });
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants