typestack / class-validator

Decorator-based property validation for classes.
MIT License
11.03k stars 800 forks source link

@IsEmail() - how to make it support '+' sign? #2284

Closed rajivkr closed 11 months ago

rajivkr commented 1 year ago

Recently learnt that gmail has an option to test as multiple id's by adding '+1', '+2', i.e, if you have hello@gmail.com, you can use hello+1@gmail.com, hello+2@gmail.com etc. to get emails to samehello@gmail.com inbox.

The problem: However, with this logic, i see that @IsEmail() is throwing an error for emails with '+' sign, is there a way i can get around this? Currently using this regex instead, but can this be done using isEmail() itself?

  @ValidateIf((_, value) => {
    return value === null || /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(value);
  })
eladchen commented 11 months ago

IMO the email should be validated according to RFC 2822, which should allow using the "+" in the local part

braaar commented 11 months ago

I can't reproduce your behavior. Plus sign in email passes validation. This was discussed in the underlying validation framework validatorjs 8 years ago

Here's my snippet, I get Validation success:

import { validate, IsEmail } from "class-validator";

class User {
  @IsEmail()
  email: string;
}

let user = new User();
post.user = "stephen+spam@gmail.com";

validate(user).then((errors) => {
  if (errors.length > 0) {
    console.log("Validation failed. errors: ", errors);
  } else {
    console.log("Validation success");
  }
});
github-actions[bot] commented 10 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.