Skip to content

Commit

Permalink
Use email-validator to validate email (#303)
Browse files Browse the repository at this point in the history
Summary:
Address the issue #302

Pull Request resolved: #303

Reviewed By: liliarizona

Differential Revision: D67997652

Pulled By: stcheng

fbshipit-source-id: 4f5e1b206b5780042d8b08bffdcb606d594eb979
  • Loading branch information
stcheng authored and facebook-github-bot committed Jan 10, 2025
1 parent 748faa0 commit 75bd12d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dependencies": {
"axios": "^1.4.0",
"currency-codes": "^1.5.1",
"email-validator": "^2.0.4",
"iso-3166-1": "^2.1.1",
"js-sha256": "^0.9.0",
"mixwith": "~0.1.1"
Expand Down
8 changes: 3 additions & 5 deletions src/objects/serverside/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import DeliveryCategory from './delivery-category.js';
const sha256 = require('js-sha256');
const currency_codes = require('currency-codes');
const country_codes = require('iso-3166-1');
const validator = require('email-validator');

const PHONE_NUMBER_IGNORE_CHAR_SET = /[\-@#<>'",; ]|\(|\)|\+|[a-z]/g;
const PHONE_NUMBER_DROP_PREFIX_ZEROS = /^\+?0{0,2}/;
Expand Down Expand Up @@ -153,15 +154,12 @@ export default class ServerSideUtils {
}

/**
* Normalizes the given email to RFC 822 standard and returns acceptable email value
* @param {String} [email] email value to be normalized.
* @return {String} Normalized email value.
*/
static normalizeEmail (email: string) {
// RFC 2822 REGEX approximation
const EMAIL_RE = /^[\w!#\$%&'\*\+\/\=\?\^`\{\|\}~\-]+(:?\.[\w!#\$%&'\*\+\/\=\?\^`\{\|\}~\-]+)*@(?:[a-z0-9](?:[a-z0-9\-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9\-]*[a-z0-9])?$/i;

if (!EMAIL_RE.test(email)) {
// Use email-validator to validate the email format
if (!validator.validate(email)) {
throw new Error("Invalid email format for the passed email:'" + email + "'.Please check the passed email format.");
}

Expand Down

0 comments on commit 75bd12d

Please sign in to comment.