@@ -39,70 +39,97 @@ test("failing validations", () => {
39
39
} ) ;
40
40
41
41
test ( "email validations" , ( ) => {
42
- const email = z . string ( ) . email ( ) ;
43
- email . parse ( "mojojojo@example.com" ) ;
44
- expect ( ( ) => email . parse ( "asdf" ) ) . toThrow ( ) ;
45
- expect ( ( ) => email . parse ( "@lkjasdf.com" ) ) . toThrow ( ) ;
46
- expect ( ( ) => email . parse ( "asdf@sdf." ) ) . toThrow ( ) ;
47
- expect ( ( ) => email . parse ( "asdf@asdf.com-" ) ) . toThrow ( ) ;
48
- expect ( ( ) => email . parse ( "asdf@-asdf.com" ) ) . toThrow ( ) ;
49
- expect ( ( ) => email . parse ( "asdf@-a(sdf.com" ) ) . toThrow ( ) ;
50
- expect ( ( ) => email . parse ( "asdf@-asdf.com(" ) ) . toThrow ( ) ;
51
- expect ( ( ) =>
52
- email . parse ( "pawan.anand@%9y83&#$%R&#$%R&%#$R%%^^%5rw3ewe.d.d.aaaa.wef.co" )
53
- ) . toThrow ( ) ;
54
- } ) ;
55
-
56
- test ( "more email validations" , ( ) => {
57
42
const validEmails = [
43
+ `email@domain.com` ,
44
+ `firstname.lastname@domain.com` ,
45
+ `email@subdomain.domain.com` ,
46
+ `firstname+lastname@domain.com` ,
47
+ `1234567890@domain.com` ,
48
+ `email@domain-one.com` ,
49
+ `_______@domain.com` ,
50
+ `email@domain.name` ,
51
+ `email@domain.co.jp` ,
52
+ `firstname-lastname@domain.com` ,
58
53
`very.common@example.com` ,
59
54
`disposable.style.email.with+symbol@example.com` ,
60
55
`other.email-with-hyphen@example.com` ,
61
56
`fully-qualified-domain@example.com` ,
62
57
`user.name+tag+sorting@example.com` ,
63
58
`x@example.com` ,
59
+ `mojojojo@asdf.example.com` ,
64
60
`example-indeed@strange-example.com` ,
65
- `test/test@test.com` ,
66
61
`example@s.example` ,
67
- `" "@example.org` ,
68
- `"john..doe"@example.org` ,
69
- `mailhost!username@example.org` ,
70
- `"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com` ,
71
- `user%example.com@example.org` ,
72
62
`user-@example.org` ,
73
- `postmaster@[123.123.123.123]` ,
74
63
`user@my-example.com` ,
75
64
`a@b.cd` ,
76
65
`work+user@mail.com` ,
66
+ `tom@test.te-st.com` ,
67
+ `something@subdomain.domain-with-hyphens.tld` ,
68
+ `francois@etu.inp-n7.fr` ,
69
+ ] ;
70
+ const invalidEmails = [
71
+ // no "printable characters"
72
+ // `user%example.com@example.org`,
73
+ // `mailhost!username@example.org`,
74
+ // `test/test@test.com`,
75
+
76
+ // double @
77
+ `francois@@etu.inp-n7.fr` ,
78
+ // do not support quotes
79
+ `"email"@domain.com` ,
80
+ `"e asdf sadf ?<>ail"@domain.com` ,
81
+ `" "@example.org` ,
82
+ `"john..doe"@example.org` ,
83
+ `"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com` ,
84
+
85
+ // do not support IPv4
86
+ `email@123.123.123.123` ,
87
+ `email@[123.123.123.123]` ,
88
+ `postmaster@123.123.123.123` ,
77
89
`user@[68.185.127.196]` ,
78
90
`ipv4@[85.129.96.247]` ,
79
91
`valid@[79.208.229.53]` ,
80
92
`valid@[255.255.255.255]` ,
81
93
`valid@[255.0.55.2]` ,
82
94
`valid@[255.0.55.2]` ,
95
+
96
+ // do not support ipv6
83
97
`hgrebert0@[IPv6:4dc8:ac7:ce79:8878:1290:6098:5c50:1f25]` ,
84
98
`bshapiro4@[IPv6:3669:c709:e981:4884:59a3:75d1:166b:9ae]` ,
85
99
`jsmith@[IPv6:2001:db8::1]` ,
86
100
`postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]` ,
87
101
`postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:192.168.1.1]` ,
88
- `test@any.th1ng.com` ,
89
- `me@y.z.com` ,
90
- `me@y.z.co.jp` ,
91
- `example@subdomain.hyphenated-domain.com` ,
92
- `example@atlanta.k12.ga.us` ,
93
- `a.b@c.d` ,
94
- ] ;
95
- const invalidEmails = [
102
+
103
+ // microsoft test cases
104
+ `plainaddress` ,
105
+ `#@%^%#$@#$@#.com` ,
106
+ `@domain.com` ,
107
+ `Joe Smith <email@domain.com>` ,
108
+ `email.domain.com` ,
109
+ `email@domain@domain.com` ,
110
+ `.email@domain.com` ,
111
+ `email.@domain.com` ,
112
+ `email..email@domain.com` ,
113
+ `あいうえお@domain.com` ,
114
+ `email@domain.com (Joe Smith)` ,
115
+ `email@domain` ,
116
+ `email@-domain.com` ,
117
+ `email@111.222.333.44444` ,
118
+ `email@domain..com` ,
96
119
`Abc.example.com` ,
97
120
`A@b@c@example.com` ,
121
+ `colin..hacks@domain.com` ,
98
122
`a"b(c)d,e:f;g<h>i[j\k]l@example.com` ,
99
123
`just"not"right@example.com` ,
100
124
`this is"not\allowed@example.com` ,
101
125
`this\ still\"not\\allowed@example.com` ,
126
+
127
+ // random
102
128
`i_like_underscore@but_its_not_allowed_in_this_part.example.com` ,
103
129
`QA[icon]CHOCOLATE[icon]@test.com` ,
104
130
`invalid@-start.com` ,
105
131
`invalid@end.com-` ,
132
+ `a.b@c.d` ,
106
133
`invalid@[1.1.1.-1]` ,
107
134
`invalid@[68.185.127.196.55]` ,
108
135
`temp@[192.168.1]` ,
@@ -121,13 +148,16 @@ test("more email validations", () => {
121
148
`test@.com` ,
122
149
] ;
123
150
const emailSchema = z . string ( ) . email ( ) ;
151
+
124
152
expect (
125
- validEmails . every ( ( email ) => emailSchema . safeParse ( email ) . success )
153
+ validEmails . every ( ( email ) => {
154
+ return emailSchema . safeParse ( email ) . success ;
155
+ } )
126
156
) . toBe ( true ) ;
127
157
expect (
128
- invalidEmails . every (
129
- ( email ) => emailSchema . safeParse ( email ) . success === false
130
- )
158
+ invalidEmails . every ( ( email ) => {
159
+ return emailSchema . safeParse ( email ) . success === false ;
160
+ } )
131
161
) . toBe ( true ) ;
132
162
} ) ;
133
163
0 commit comments