-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bypass email validation for deleted emails
- Loading branch information
1 parent
328d659
commit 5bcdbce
Showing
2 changed files
with
53 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,17 @@ | |
const { Assert: is, Validator, Violation } = require('validator.js'); | ||
let validator; | ||
|
||
/** | ||
* Deleted email regex. | ||
* | ||
* The email should contain the following pattern: | ||
* [email protected] | ||
* | ||
* example: [email protected] | ||
*/ | ||
|
||
const regex = /.+@\/\w+\.\w+\.\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; | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,8 +59,35 @@ describe('EmailAssert', () => { | |
} | ||
}); | ||
|
||
it('should thrown an error on invalid emails', () => { | ||
[ | ||
'@foo.com.123.deleted', | ||
'[email protected]', | ||
'[email protected]', | ||
'føø@båz.', | ||
'føø@båz..123.deleted', | ||
'foo+bar@baz_foo.com', | ||
'foo+bar@baz_foo.com.12345.deleted' | ||
].forEach(choice => { | ||
try { | ||
Assert.prototype.Email().validate(choice); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e.show().assert).toBe('Email'); | ||
} | ||
}); | ||
}); | ||
|
||
it('should accept valid emails', () => { | ||
['[email protected]', 'føø@båz.com', '[email protected]'].forEach(choice => { | ||
[ | ||
'[email protected]', | ||
'[email protected]', | ||
'føø@båz.com', | ||
'føø@båz.com.123.deleted', | ||
'[email protected]', | ||
'[email protected]' | ||
].forEach(choice => { | ||
Assert.prototype.Email().validate(choice); | ||
}); | ||
}); | ||
|