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

Error when validating an existing email with hyphen (-) symbols when having a subdomain #2540

Open
rafaell-lycan opened this issue Jun 30, 2023 · 9 comments

Comments

@rafaell-lycan
Copy link

rafaell-lycan commented Jun 30, 2023

Hi,

I'm getting a strange error when parsing an email that contains a hyphen (-) when having a subdomain:

Example of a valid email:

me@.main-domain.com # works
me@subdomain.maindomain.com # works
me@subdomain.main-domain.com # error

Error:

Invalid data: ZodError: [
  {
    "validation": "email",
    "code": "invalid_string",
    "message": "Invalid email",
    "path": [
      "email"
    ]
  }
]

This started happening when upgrading from 3.20.x to 3.21.x

The only I could think about was to use a Regex instead:

z.string().regex(/^[a-zA-Z0-9_!#$%&’*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$/)

Any ideas or workaround on it?

@rafaell-lycan rafaell-lycan changed the title Error when validating an existing email with hyphen (-) symbols. Error when validating an existing email with hyphen (-) symbols when having a subdomain Jun 30, 2023
@askkaz
Copy link

askkaz commented Jun 30, 2023

seeing this as well

@AllanSimoyi
Copy link

@fvckDesa perhaps PR #1979 could be where hyphens got negated. Regex's not my strong suit, perhaps you could confirm.

@fvckDesa
Copy link
Contributor

fvckDesa commented Jul 4, 2023

@AllanSimoyi the regex that I had wrote in PR #1982 was changed with another

@phil-loops
Copy link

I've inspected the latest released package 3.21.4, and it's using the regex /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\])|(\[IPv6:(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))\])|([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])*(\.[A-Za-z]{2,})+))$/

However, I've noticed that this has been switched out in the current codebase with the regex /^([A-Z0-9_+-]+\.?)*[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i; which appears to be an improvement (or at least a bit more accepting).

I dunno when the next version rolls out, but on paper, this change should address the issue we're seeing

@rafaell-lycan
Copy link
Author

@phil-loops the current regex you send doesn't validate the presence of hyphens (-) on all scenarios after the first dot (.)

Is there anything we can do to fix it?

@phil-loops
Copy link

@rafaell-lycan to get the zod regex modified, you'd have to submit a PR and wait for the next package to come out.

Otherwise, if you want to roll your own email validation, you could do something like z.string().regex(/..../) with whatever rules you want

@rafaell-lycan
Copy link
Author

@phil-loops for now I'm using a custom Regex which is the previous one used at v3.20.6 after avoiding updating to v3.21.x which we saw some performance gains.

For anyone else looking for a fix with previous email regex:

const EMAIL_REGEX = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|([^-]([a-zA-Z0-9-]*\.)+[a-zA-Z]{2,}))$/;

z.string().regex(EMAIL_REGEX)

@mgdigital
Copy link

I'm also seeing this- any progress on a fix?

@benln
Copy link

benln commented Mar 25, 2024

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

7 participants