From 3bdc25bf2670fecc592f9be9862133cad1ee3907 Mon Sep 17 00:00:00 2001 From: Luis Deschamps Rudge Date: Thu, 3 Oct 2019 16:19:21 -0300 Subject: [PATCH 1/3] Relaxing the email validation --- src/__tests__/field/email.test.js | 27 +++++++++++++++++++++++++++ src/field/email.js | 18 ++++++++++-------- 2 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 src/__tests__/field/email.test.js diff --git a/src/__tests__/field/email.test.js b/src/__tests__/field/email.test.js new file mode 100644 index 000000000..0007aaec8 --- /dev/null +++ b/src/__tests__/field/email.test.js @@ -0,0 +1,27 @@ +import { isEmail, emailDomain } from '../../field/email'; + +describe('field/email', () => { + describe('isEmail', () => { + it('validates correctly', () => { + expect(isEmail('test@test.com')).toBe(true); + expect(isEmail('test@test.com.br')).toBe(true); + expect(isEmail('test@移动.移动')).toBe(true); + expect(isEmail(1)).toBe(false); + expect(isEmail('test@testcom')).toBe(false); + expect(isEmail('test.test.com')).toBe(false); + }); + it('returns false when there is a white space', () => { + expect(isEmail('test@test.com ')).toBe(false); + }); + }); + describe('emailDomain', () => { + it('extracts email domain correctly', () => { + expect(emailDomain('test@test.com')).toBe('test.com'); + expect(emailDomain('test@test.com.br')).toBe('test.com.br'); + expect(emailDomain('test@移动.移动')).toBe('移动.移动'); + expect(emailDomain(1)).toBe(''); + expect(emailDomain('test@testcom')).toBe(''); + expect(emailDomain('test.test.com')).toBe(''); + }); + }); +}); diff --git a/src/field/email.js b/src/field/email.js index 31ac5003a..67536c896 100644 --- a/src/field/email.js +++ b/src/field/email.js @@ -1,18 +1,18 @@ -import trim from 'trim'; import { setField } from './index'; -import { endsWith } from '../utils/string_utils'; import { isHRDEmailValid } from '../connection/enterprise'; import * as i18n from '../i18n'; -const regExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; - export function validateEmail(str) { return isEmail(str); } export function isEmail(str) { - const result = regExp.exec(trim(str.toLowerCase())); - return !!result && result[0] !== null; + return ( + typeof str === 'string' && + str.indexOf('@') >= 0 && + str.indexOf('.') >= 0 && + str.indexOf(' ') === -1 + ); } export function setEmail(m, str) { @@ -27,8 +27,10 @@ export function setEmail(m, str) { } export function emailDomain(str) { - const result = regExp.exec(trim(str.toLowerCase())); - return result ? result.slice(-2)[0] : ''; + if (!isEmail(str)) { + return ''; + } + return str.split('@')[1].toLowerCase(); } export function emailLocalPart(str) { From c69d0486bb0bbb0b3677a86d0d4c3f5c2d1b9381 Mon Sep 17 00:00:00 2001 From: Luis Deschamps Rudge Date: Sun, 6 Oct 2019 18:16:15 -0300 Subject: [PATCH 2/3] input type text --- src/ui/input/email_input.jsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ui/input/email_input.jsx b/src/ui/input/email_input.jsx index 999fdc19f..e649b1970 100644 --- a/src/ui/input/email_input.jsx +++ b/src/ui/input/email_input.jsx @@ -37,7 +37,8 @@ export default class EmailInput extends React.Component { Date: Tue, 8 Oct 2019 10:50:19 -0300 Subject: [PATCH 3/3] readd trim --- src/__tests__/field/email.test.js | 6 ++++-- src/field/email.js | 13 +++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/__tests__/field/email.test.js b/src/__tests__/field/email.test.js index 0007aaec8..bc3186daf 100644 --- a/src/__tests__/field/email.test.js +++ b/src/__tests__/field/email.test.js @@ -6,12 +6,14 @@ describe('field/email', () => { expect(isEmail('test@test.com')).toBe(true); expect(isEmail('test@test.com.br')).toBe(true); expect(isEmail('test@移动.移动')).toBe(true); + expect(isEmail('test@test.com ')).toBe(true); + expect(isEmail(' test@test.com')).toBe(true); expect(isEmail(1)).toBe(false); expect(isEmail('test@testcom')).toBe(false); expect(isEmail('test.test.com')).toBe(false); }); - it('returns false when there is a white space', () => { - expect(isEmail('test@test.com ')).toBe(false); + it('returns false when there is a white space in the middle of the string', () => { + expect(isEmail('test@test. com')).toBe(false); }); }); describe('emailDomain', () => { diff --git a/src/field/email.js b/src/field/email.js index 67536c896..eca298c17 100644 --- a/src/field/email.js +++ b/src/field/email.js @@ -1,3 +1,5 @@ +import trim from 'trim'; + import { setField } from './index'; import { isHRDEmailValid } from '../connection/enterprise'; import * as i18n from '../i18n'; @@ -7,12 +9,11 @@ export function validateEmail(str) { } export function isEmail(str) { - return ( - typeof str === 'string' && - str.indexOf('@') >= 0 && - str.indexOf('.') >= 0 && - str.indexOf(' ') === -1 - ); + if (typeof str !== 'string') { + return false; + } + const trimmed = trim(str); + return trimmed.indexOf('@') >= 0 && trimmed.indexOf('.') >= 0 && trimmed.indexOf(' ') === -1; } export function setEmail(m, str) {