colinhacks / zod

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

Email validation fails on "=" equals sign #3707

Closed j-christoffersen closed 1 month ago

j-christoffersen commented 1 month ago

z.string().email() incorrectly marks emails using a = as invalid. = should be valid for the local-part of an email address.

Version: 3.23.8

Testing with the following code:

  // Define email validation schema with Zod
  const zodEmailSchema = z.string().email()

  // Function to validate email using Zod
  function validateWithZod(email: string) {
    console.log(`Testing email: ${email}`)
    try {
      zodEmailSchema.parse(email)
      console.log("Zod: Email is valid")
    } catch (error) {
      console.log("Zod: Email is invalid", error.errors)
    }
  }

  const testEmails = ["something@example.com", "some=thing@example.com"]

  for (const testEmail of testEmails) {
    validateWithZod(testEmail)
  }

Which outputs:

Testing email: something@example.com
Zod: Email is valid
Testing email: some=thing@example.com
Zod: Email is invalid [
  {
    validation: 'email',
    code: 'invalid_string',
    message: 'Invalid email',
    path: []
  }
]
colinhacks commented 1 month ago

Email validation has been much discussed in the issues, and this is the current policy: https://github.com/colinhacks/zod/pull/2157

I'd recommend just using z.string().regex() if you're looking for something more permissive.