zod icon indicating copy to clipboard operation
zod copied to clipboard

Update email regex to use W3C's one?

Open timojuonoja opened this issue 2 years ago • 3 comments

There is no 100% bullet proof email regex, but should you update it to use W3C's one? Currently I'm facing a bug that a\[email protected] is passed, when W3C's regex wouldn't pass it.

Current zod regex for email is:

(/^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i)

When W3C suggests:

/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/

timojuonoja avatar Oct 25 '22 04:10 timojuonoja

See also: https://github.com/colinhacks/zod/pull/1435

Tyler-Murphy avatar Nov 16 '22 17:11 Tyler-Murphy

@Tyler-Murphy PR #1435 does not fix this issue. With those changes a\[email protected] is still marked as "valid"... yeah, I know there are no bullet-proof regex, but your changes doesn't help this issue.

timojuonoja avatar Nov 17 '22 06:11 timojuonoja

Sorry, I should have been clearer. I'm just pointing out its existence, since the W3C regex appears not to work with some of the test cases in #1435.

Tyler-Murphy avatar Nov 17 '22 20:11 Tyler-Murphy

This would also allow + in addresses like [email protected], which would be good.

mpartel avatar Jan 16 '23 07:01 mpartel

@timojuonoja "\" is not valid email character, the only way to use it is with the double quotes around the local-part of email

fvckDesa avatar Feb 03 '23 19:02 fvckDesa

@timojuonoja "" is not valid email character, the only way to use it is with the double quotes around the local-part of email

Yes, you are absolutely right by the way. We seem to have an error in our test cases after all... I'm closing this due to incorrect assumptions.

timojuonoja avatar Feb 04 '23 05:02 timojuonoja