From c8a87dac1a4cc6988b5418f30411a8669bef204e Mon Sep 17 00:00:00 2001 From: Darcy Clarke Date: Thu, 15 Oct 2020 17:59:42 -0400 Subject: [PATCH] fix: update email validation PR-URL: https://github.com/isaacs/npm-user-validate/pull/15 Credit: @ Close: #15 Reviewed-by: @isaacs --- npm-user-validate.js | 6 +++++- test/email.test.js | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/npm-user-validate.js b/npm-user-validate.js index 9250ce3..ffd8791 100644 --- a/npm-user-validate.js +++ b/npm-user-validate.js @@ -11,6 +11,7 @@ var requirements = exports.requirements = { }, password: {}, email: { + length: 'Email length must be less then or equal to 254 characters long', valid: 'Email must be an email address' } } @@ -45,7 +46,10 @@ function username (un) { } function email (em) { - if (!em.match(/^.+@.+\..+$/)) { + if (em.length > 254) { + return new Error(requirements.email.length) + } + if (!em.match(/^[^@]+@.+\..+$/)) { return new Error(requirements.email.valid) } diff --git a/test/email.test.js b/test/email.test.js index c2e58ae..7cda76e 100644 --- a/test/email.test.js +++ b/test/email.test.js @@ -7,6 +7,13 @@ test('email misses an @', function (t) { t.end() }) +test('email is longer then 254 characters', function (t) { + var str = '@'.repeat(255) + var err = v(str) + t.type(err, 'object') + t.end() +}) + test('email misses a dot', function (t) { var err = v('name@domain') t.type(err, 'object')