Skip to content

Commit

Permalink
Use email-validator to validate email
Browse files Browse the repository at this point in the history
  • Loading branch information
stcheng committed Jan 10, 2025
1 parent 748faa0 commit dac0f75
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 @@ -7,6 +7,7 @@
* @flow
*/

import validator from 'email-validator';
import DeliveryCategory from './delivery-category.js';

const sha256 = require('js-sha256');
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 dac0f75

Please sign in to comment.