File tree 2 files changed +53
-5
lines changed
2 files changed +53
-5
lines changed Original file line number Diff line number Diff line change 7
7
const { Assert : is , Validator, Violation } = require ( 'validator.js' ) ;
8
8
let validator ;
9
9
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 + \. d e l e t e d $ / ;
20
+
10
21
/**
11
22
* Optional peer dependencies.
12
23
*/
@@ -41,16 +52,26 @@ module.exports = function emailAssert() {
41
52
throw new Violation ( this , value , { value : Validator . errorCode . must_be_a_string } ) ;
42
53
}
43
54
44
- if ( ! validator . isEmail ( value ) ) {
45
- throw new Violation ( this , value ) ;
46
- }
47
-
48
55
try {
49
56
is . ofLength ( { max : 254 } ) . validate ( value ) ;
50
57
} catch ( e ) {
51
58
throw new Violation ( this , value ) ;
52
59
}
53
60
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
+
54
75
return true ;
55
76
} ;
56
77
Original file line number Diff line number Diff line change @@ -59,8 +59,35 @@ describe('EmailAssert', () => {
59
59
}
60
60
} ) ;
61
61
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
+
62
82
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 => {
64
91
Assert . prototype . Email ( ) . validate ( choice ) ;
65
92
} ) ;
66
93
} ) ;
You can’t perform that action at this time.
0 commit comments