Skip to content

Commit ceb1368

Browse files
Megamind51vitorcoxta
authored andcommitted
Bypass email validation for deleted emails
1 parent 328d659 commit ceb1368

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/asserts/email-assert.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
const { Assert: is, Validator, Violation } = require('validator.js');
88
let validator;
99

10+
/**
11+
* Deleted email regex.
12+
*
13+
* The email should contain the following pattern:
14+
* .+@.+\.any-number\.deleted
15+
*
16+
* example: [email protected]
17+
*/
18+
19+
const deletedUserEmailRegex = /.+@.+\.\d+\.deleted$/;
20+
1021
/**
1122
* Optional peer dependencies.
1223
*/
@@ -41,16 +52,26 @@ module.exports = function emailAssert() {
4152
throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string });
4253
}
4354

44-
if (!validator.isEmail(value)) {
45-
throw new Violation(this, value);
46-
}
47-
4855
try {
4956
is.ofLength({ max: 254 }).validate(value);
5057
} catch (e) {
5158
throw new Violation(this, value);
5259
}
5360

61+
// We are bypassing the email validation for deleted users.
62+
// This is needed because we have legacy users with invalid emails
63+
// and as part of the deletion flow for expired signups we need to allow
64+
// the deletion of that kind of users. The process of deleting a
65+
// user updates their email from `${user.email}` to
66+
// `${user.email}.${timestamp}.deleted`, so this assert runs on that update.
67+
if (deletedUserEmailRegex.test(value) === true) {
68+
return true;
69+
}
70+
71+
if (!validator.isEmail(value)) {
72+
throw new Violation(this, value);
73+
}
74+
5475
return true;
5576
};
5677

test/asserts/email-assert.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,29 @@ describe('EmailAssert', () => {
5959
}
6060
});
6161

62+
it('should throw an error on invalid emails', () => {
63+
['[email protected]', 'føø@båz.', 'foo+bar@baz_foo.com'].forEach(choice => {
64+
try {
65+
Assert.prototype.Email().validate(choice);
66+
67+
fail();
68+
} catch (e) {
69+
expect(e.show().assert).toBe('Email');
70+
}
71+
});
72+
});
73+
6274
it('should accept valid emails', () => {
63-
['[email protected]', 'føø@båz.com', '[email protected]'].forEach(choice => {
75+
[
76+
77+
78+
'føø@båz.com',
79+
'føø@båz.com.123.deleted',
80+
81+
82+
83+
84+
].forEach(choice => {
6485
Assert.prototype.Email().validate(choice);
6586
});
6687
});

0 commit comments

Comments
 (0)