Skip to content

Commit 933fd22

Browse files
committed
Bypass email validation for deleted emails
1 parent 328d659 commit 933fd22

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/asserts/email-assert.js

+25-4
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-word.any-word.any-number.deleted
15+
*
16+
* example: a@foo.com.123.deleted
17+
*/
18+
19+
const regex = /.+@.+\.\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 an
65+
// user changes their email from user.email to
66+
// ${user.email}.${timestamp}.deleted so this assert runs on that update.
67+
if (regex.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

+28-1
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,35 @@ describe('EmailAssert', () => {
5959
}
6060
});
6161

62+
it('should thrown an error on invalid emails', () => {
63+
[
64+
'@foo.com.123.deleted',
65+
'foo@.com',
66+
'foo@.com.123.deleted',
67+
'føø@båz.',
68+
'føø@båz..123.deleted',
69+
'foo+bar@baz_foo.com',
70+
'foo+bar@baz_foo.com.12345.deleted'
71+
].forEach(choice => {
72+
try {
73+
Assert.prototype.Email().validate(choice);
74+
75+
fail();
76+
} catch (e) {
77+
expect(e.show().assert).toBe('Email');
78+
}
79+
});
80+
});
81+
6282
it('should accept valid emails', () => {
63-
['foo@bar.com', 'føø@båz.com', 'foo+bar@baz.com'].forEach(choice => {
83+
[
84+
'foo@bar.com',
85+
'foo@bar.com.123.deleted',
86+
'føø@båz.com',
87+
'føø@båz.com.123.deleted',
88+
'foo+bar@baz.com',
89+
'foo+bar@baz.com.12345.deleted'
90+
].forEach(choice => {
6491
Assert.prototype.Email().validate(choice);
6592
});
6693
});

0 commit comments

Comments
 (0)