From 28f938853a60ee9d035ee66036314b93cf4d1a12 Mon Sep 17 00:00:00 2001 From: Joseph Frazer Date: Thu, 12 Sep 2019 09:08:47 -0400 Subject: [PATCH] [DEV-5657] Check for TLD in email addresses The DocuSign service rejects email addresses without a TLD, even though they are valid. --- src/validate/formatter/EmailFormatter.js | 9 ++- test/validate/formatter/EmailFormatterTest.js | 63 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 test/validate/formatter/EmailFormatterTest.js diff --git a/src/validate/formatter/EmailFormatter.js b/src/validate/formatter/EmailFormatter.js index 0900fc2..31d200f 100644 --- a/src/validate/formatter/EmailFormatter.js +++ b/src/validate/formatter/EmailFormatter.js @@ -65,7 +65,14 @@ module.exports = require( './PatternFormatter' )( // Must be a valid hostname and may contain comments in // parenthesis. IPs are technically allowed in brackets, but // we're not going to support that crap. - '[a-zA-Z0-9-.()]+' + + // begins with a letter or a number + '[a-zA-Z0-9]+' + + // then can have any other acceptable chars + '[a-zA-Z0-9-]*' + + // and optional subdomains + '(?:\\.[a-zA-Z0-9-]+)*' + + // TLD + '\\.[a-zA-Z]{2,}' + '$' ), diff --git a/test/validate/formatter/EmailFormatterTest.js b/test/validate/formatter/EmailFormatterTest.js new file mode 100644 index 0000000..c451f32 --- /dev/null +++ b/test/validate/formatter/EmailFormatterTest.js @@ -0,0 +1,63 @@ +/** + * @license + * StringFormat formatter test + * + * Copyright (C) 2010-2019 R-T Specialty, LLC. + * + * This file is part of liza. + * + * liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +var liza = require( '../../../' ), + Sut = liza.validate.formatter.EmailFormatter, + assert = require( 'assert' ); + + +describe( 'validate.formatter.StringFormat', function() +{ + // test email addresses from + // https://blogs.msdn.microsoft.com/testing123/2009/02/06/email-address-test-cases/ + [ + "email@domain.com", + "firstname.lastname@domain.com", + "email@subdomain.domain.com", + "firstname+lastname@domain.com", + 'email"@domain.com', + "1234567890@domain.com", + "email@domain-one.com", + "_______@domain.com", + "email@domain.name", + "email@domain.co.jp", + "firstname-lastname@domain.com", + ].forEach( email_address => assert.equal( Sut.parse( email_address ), email_address ) ); + + [ + "", + "plainaddress", + "#@%^%#$@#$@#.com", + "@domain.com", + "Joe Smith ", + "email.domain.com", + // "email@domain@domain.com", + ".email@domain.com", + "email.@domain.com", + "email..email@domain.com", + "あいうえお@domain.com", + "email@domain.com (Joe Smith)", + "email@domain", + "email@-domain.com", + "email@domain..com", + ].forEach( email_address => assert.throws( () => Sut.parse( email_address ), Error ) ); +} );