From d80f623a176ce296e9ffffef0a6c6b91ea744544 Mon Sep 17 00:00:00 2001 From: Leonard Bogdonoff Date: Fri, 23 Jun 2017 12:30:46 -0400 Subject: [PATCH 1/4] Correct validator to use rfc822 email validation --- static_src/util/validators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static_src/util/validators.js b/static_src/util/validators.js index 4bc87dad..a428854b 100644 --- a/static_src/util/validators.js +++ b/static_src/util/validators.js @@ -41,7 +41,7 @@ export function validateString() { export function validateEmail() { return function _validateEmail(value, name) { - if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value))) { + if (!(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/).test(value)) { const nameString = (name ? `in ${name} ` : ''); return { message: `The value entered ${nameString}is not a valid e-mail address` From 7744f6cdcea74b4ade363bdb39e5cbd8aface4c9 Mon Sep 17 00:00:00 2001 From: Leonard Bogdonoff Date: Fri, 23 Jun 2017 16:19:04 -0400 Subject: [PATCH 2/4] Update the kinds of emails that fail validation --- static_src/test/unit/util/validators.spec.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/static_src/test/unit/util/validators.spec.js b/static_src/test/unit/util/validators.spec.js index 333c3e0c..355ca78e 100644 --- a/static_src/test/unit/util/validators.spec.js +++ b/static_src/test/unit/util/validators.spec.js @@ -31,13 +31,23 @@ describe('validateEmail', function () { }); it('fails for none emails string@', function () { - const result = validator('domain@place'); + const result = validator('domain@'); + expect(result).toEqual({ message: 'The value entered is not a valid e-mail address' }); + }); + + it('fails for none emails string@domain.', function () { + const result = validator('domain@domain.'); expect(result).toEqual({ message: 'The value entered is not a valid e-mail address' }); }); it('fails for none emails string@string', function () { const result = validator('domain@place'); - expect(result).toEqual({ message: 'The value entered is not a valid e-mail address' }); + expect(result).toEqual(null); + }); + + it('fails for none emails string@string.com.co', function () { + const result = validator('domain@place'); + expect(result).toEqual(null); }); it('succeeds for email', function () { From 3701c8e0ce0095c7c035bdde7df76ded2616cfc1 Mon Sep 17 00:00:00 2001 From: Leonard Bogdonoff Date: Fri, 23 Jun 2017 16:28:39 -0400 Subject: [PATCH 3/4] Save the email regex as a variable --- static_src/util/validators.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/static_src/util/validators.js b/static_src/util/validators.js index a428854b..7d3775f7 100644 --- a/static_src/util/validators.js +++ b/static_src/util/validators.js @@ -41,7 +41,8 @@ export function validateString() { export function validateEmail() { return function _validateEmail(value, name) { - if (!(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/).test(value)) { + const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/; + if (!(emailRegex).test(value)) { const nameString = (name ? `in ${name} ` : ''); return { message: `The value entered ${nameString}is not a valid e-mail address` From 41addeeab36873644e20abf59af367b4b06f7443 Mon Sep 17 00:00:00 2001 From: Leonard Bogdonoff Date: Fri, 23 Jun 2017 16:32:07 -0400 Subject: [PATCH 4/4] Set regex as const outside of the function --- static_src/util/validators.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/static_src/util/validators.js b/static_src/util/validators.js index 7d3775f7..379b5257 100644 --- a/static_src/util/validators.js +++ b/static_src/util/validators.js @@ -1,3 +1,5 @@ +const EMAIL_REGEX = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/; + export function validateInteger(options) { const { max, min } = options || {}; return function _validateInteger(text) { @@ -41,8 +43,7 @@ export function validateString() { export function validateEmail() { return function _validateEmail(value, name) { - const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/; - if (!(emailRegex).test(value)) { + if (!(EMAIL_REGEX).test(value)) { const nameString = (name ? `in ${name} ` : ''); return { message: `The value entered ${nameString}is not a valid e-mail address`