hapijs / joi

The most powerful data validation library for JS
Other
20.94k stars 1.51k forks source link

Are `{}@gmail.com` and `%@gmail.com` valid email addresses? #3025

Closed mertssmnoglu closed 8 months ago

mertssmnoglu commented 8 months ago

Runtime

nodejs

Runtime version

18.19.1

Module version

17.12.2

Used with

standalone, typescript

Any other relevant information

No response

How can we help?

An email including curly brackets really allowed?

I created a standalone nodejs application to check validations in different libraries. I use joi and zod at the same time to see the differences.

import z from "zod"
import Joi from "joi"

// Here are my test mails
const mailMatrix = ["()@gmail.com", "{}@gmail.com", "%@gmail.com"]

const joiSchema = Joi.object({
  email: Joi.string().email(),
})

const zodSchema = z.object({
  email: z.string().email(),
})

Here are my validate functions to joi and zod.

const validateZod = (inputs: unknown) => {
  let isValidForZod: boolean
  try {
    zodSchema.parse(inputs)
    isValidForZod = true
  } catch (error) {
    isValidForZod = false
  }

  return isValidForZod
}

const validateJoi = (inputs: unknown) => {
  const res = joiSchema.validate(inputs)
  return res.error ? false : true
}

When i want to see are them valid or not with this map

mailMatrix.map((mail) => {
  const data = {
    email: mail,
  }
  const results = {
    joi: validateJoi(data),
    zod: validateZod(data),
  }

  console.log(`JOI | ${mail} ->`, results.joi)
  console.log(`ZOD | ${mail} ->`, results.zod)
  console.log("-------------------------------------------------")
})

Results: true -> valid false -> invalid

20240326_19h22m00s_grim

How is it possible for someone to have a curly brackets email or percent sign. Is it a bug or something like that?

Marsup commented 8 months ago

Those adresses are following the RFC, this doesn't mean that gmail will accept those, but they are valid.

mertssmnoglu commented 8 months ago

OK. Thanks for the answer.