colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.83k stars 1.17k forks source link

Able to validate invalid email #1979

Open M0hammedImran opened 1 year ago

M0hammedImran commented 1 year ago

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 commented 1 year ago

Are you able to give a code example?

fvckDesa commented 1 year ago
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:

JacobWeisenburger commented 1 year ago

Behind the scenes zod uses this regex to validate email addresses: https://github.com/colinhacks/zod/blob/a57fc2da3c48a5a2270e9684017d8bc056af8a40/src/types.ts#L523-L524

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/ )
fvckDesa commented 1 year ago

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

florent-martineau commented 1 year ago

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

M0hammedImran commented 1 year ago

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 commented 1 year ago

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