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

Able to validate invalid email #1979

Open
M0hammedImran opened this issue Feb 3, 2023 · 7 comments
Open

Able to validate invalid email #1979

M0hammedImran opened this issue Feb 3, 2023 · 7 comments
Labels
enhancement New feature or request

Comments

@M0hammedImran
Copy link

Currently, I'm able to pass this as email and get no issues.

imran@345934898929^&^&*&**#$%*&%#imran.com
pawan.anand@%9y8325&%#%$&#$R%&#$%R&#$%R&%#$R%%^^%5rw3ewe.r.ef.d.d.d.d.d.aaaa.wef.co
@JacobWeisenburger
Copy link
Collaborator

Are you able to give a code example?

@fvckDesa
Copy link
Contributor

fvckDesa commented Feb 3, 2023

const scheme = z.string().email();
const email = "pawan.anand@%9y8325&%#%$&#$R%&#$%R&#$%R&%#$R%%^^%5rw3ewe.r.ef.d.d.d.d.d.aaaa.wef.co";
scheme.parse(email); //don't throw an error

According to this site (link) the domain can contains:

  • max 63 characters
  • uppercase and lowercase Latin letters
  • numbers 0-9 (not only numbers)
  • one "-" not in start or end

@JacobWeisenburger
Copy link
Collaborator

Behind the scenes zod uses this regex to validate email addresses:

zod/src/types.ts

Lines 523 to 524 in a57fc2d

const emailRegex =
/^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{1,})[^-<>()[\].,;:\s@"]$/i;

If you would like a different regex to do this, you can use something like these:

// completely custom regex
const schema = z.string().regex( /some regex/ )

// custom regex that must match the zod email regex
const schema = z.string().email().regex( /some regex/ )

@JacobWeisenburger JacobWeisenburger added enhancement New feature or request question Further information is requested labels Feb 3, 2023
@fvckDesa
Copy link
Contributor

fvckDesa commented Feb 3, 2023

I created a pull request and for me this is a valid change because can prevent some issues

@JacobWeisenburger JacobWeisenburger removed the question Further information is requested label Feb 3, 2023
colinhacks added a commit that referenced this issue Feb 8, 2023
* Increment email validation

Supported:
   - local-part -> Dot-string / Quoted-string
   - domain -> latin letters, numbers, "-" / IPv4 address
Unsupported:
   - domain -> IPv6

* Fix import type in some test files

The error generated is:
   TS2307: Cannot find module '@jest/globals' or
	its corresponding type declarations.

* Format code with prettier config

* Add more IPv4 test

* Add Invalid IPv4 tests

* Fix tests

* Lint fix

---------

Co-authored-by: Colin McDonnell <colinmcd94@gmail.com>
Co-authored-by: Colin McDonnell <colinmcd@alum.mit.edu>
@florent-martineau
Copy link

Another example of email that works but, I believe, is invalid:
invalid@foo.....com

@M0hammedImran
Copy link
Author

Another example of email that works but, I believe, is invalid: invalid@foo.....com

This doesn't pass the test for the default regex.

@JacobWeisenburger
Copy link
Collaborator

This doesn't pass the test for the default regex.

I disagree.

const schema = z.string().email()
console.log( schema.safeParse( 'invalid@foo.....com' ).success ) // true

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

No branches or pull requests

4 participants