From 55695dacd602814ce86c878f1fac8b9527330917 Mon Sep 17 00:00:00 2001 From: Alexandre Pacheco Date: Wed, 8 Jan 2025 10:16:00 +0000 Subject: [PATCH] Bypass email validation for deleted emails --- src/asserts/email-assert.js | 29 +++++++++++++++++++++++++---- test/asserts/email-assert.test.js | 23 ++++++++++++++++++++++- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/asserts/email-assert.js b/src/asserts/email-assert.js index 094298b..8338fd8 100644 --- a/src/asserts/email-assert.js +++ b/src/asserts/email-assert.js @@ -7,6 +7,17 @@ const { Assert: is, Validator, Violation } = require('validator.js'); let validator; +/** + * Deleted email regex. + * + * The email should contain the following pattern: + * .+@.+\.any-number\.deleted + * + * example: a@foo.com.123.deleted + */ + +const regex = /.+@.+\.\d+\.deleted$/; + /** * Optional peer dependencies. */ @@ -41,16 +52,26 @@ module.exports = function emailAssert() { throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string }); } - if (!validator.isEmail(value)) { - throw new Violation(this, value); - } - try { is.ofLength({ max: 254 }).validate(value); } catch (e) { throw new Violation(this, value); } + // We are bypassing the email validation for deleted users. + // This is needed because we have legacy users with invalid emails + // and as part of the deletion flow for expired signups we need to allow + // the deletion of that kind of users. The process of deleting an + // user changes their email from user.email to + // ${user.email}.${timestamp}.deleted so this assert runs on that update. + if (regex.test(value) === true) { + return true; + } + + if (!validator.isEmail(value)) { + throw new Violation(this, value); + } + return true; }; diff --git a/test/asserts/email-assert.test.js b/test/asserts/email-assert.test.js index 76c5ed2..d587e0a 100644 --- a/test/asserts/email-assert.test.js +++ b/test/asserts/email-assert.test.js @@ -59,8 +59,29 @@ describe('EmailAssert', () => { } }); + it('should thrown an error on invalid emails', () => { + ['foo@.com', 'føø@båz.', 'foo+bar@baz_foo.com'].forEach(choice => { + try { + Assert.prototype.Email().validate(choice); + + fail(); + } catch (e) { + expect(e.show().assert).toBe('Email'); + } + }); + }); + it('should accept valid emails', () => { - ['foo@bar.com', 'føø@båz.com', 'foo+bar@baz.com'].forEach(choice => { + [ + 'foo@bar.com', + 'foo@bar.com.123.deleted', + 'føø@båz.com', + 'føø@båz.com.123.deleted', + 'foo+.@baz.com.123.deleted', + 'foo+bar@com.12345.deleted', + 'foo+bar@baz.com', + 'foo+bar@baz.com.12345.deleted' + ].forEach(choice => { Assert.prototype.Email().validate(choice); }); });