@@ -3,60 +3,10 @@ import fs from 'fs';
33import { format } from 'util' ;
44import vm from 'vm' ;
55import validator from '../src/index' ;
6+ import test from './testFunctions' ;
67
78let validator_js = fs . readFileSync ( require . resolve ( '../validator.js' ) ) . toString ( ) ;
89
9- function test ( options ) {
10- let args = options . args || [ ] ;
11- args . unshift ( null ) ;
12- if ( options . error ) {
13- options . error . forEach ( ( error ) => {
14- args [ 0 ] = error ;
15- try {
16- assert . throws ( ( ) => validator [ options . validator ] ( ...args ) ) ;
17- } catch ( err ) {
18- let warning = format (
19- 'validator.%s(%s) passed but should error' ,
20- options . validator , args . join ( ', ' )
21- ) ;
22- throw new Error ( warning ) ;
23- }
24- } ) ;
25- }
26- if ( options . valid ) {
27- options . valid . forEach ( ( valid ) => {
28- args [ 0 ] = valid ;
29- if ( validator [ options . validator ] ( ...args ) !== true ) {
30- let warning = format (
31- 'validator.%s(%s) failed but should have passed' ,
32- options . validator , args . join ( ', ' )
33- ) ;
34- throw new Error ( warning ) ;
35- }
36- } ) ;
37- }
38- if ( options . invalid ) {
39- options . invalid . forEach ( ( invalid ) => {
40- args [ 0 ] = invalid ;
41- if ( validator [ options . validator ] ( ...args ) !== false ) {
42- let warning = format (
43- 'validator.%s(%s) passed but should have failed' ,
44- options . validator , args . join ( ', ' )
45- ) ;
46- throw new Error ( warning ) ;
47- }
48- } ) ;
49- }
50- }
51-
52- function repeat ( str , count ) {
53- let result = '' ;
54- for ( ; count ; count -- ) {
55- result += str ;
56- }
57- return result ;
58- }
59-
6010describe ( 'Validators' , ( ) => {
6111 it ( 'should validate email addresses' , ( ) => {
6212 test ( {
@@ -74,9 +24,9 @@ describe('Validators', () => {
7424 '"foobar"@example.com' ,
7525 '" foo m端ller "@example.com' ,
7626 '"foo\\@bar"@example.com' ,
77- `${ repeat ( 'a' , 64 ) } @${ repeat ( 'a' , 63 ) } .com` ,
78- `${ repeat ( 'a' , 64 ) } @${ repeat ( 'a' , 63 ) } .com` ,
79- `${ repeat ( 'a' , 31 ) } @gmail.com` ,
27+ `${ 'a' . repeat ( 64 ) } @${ 'a' . repeat ( 63 ) } .com` ,
28+ `${ 'a' . repeat ( 64 ) } @${ 'a' . repeat ( 63 ) } .com` ,
29+ `${ 'a' . repeat ( 31 ) } @gmail.com` ,
8030 'test@gmail.com' ,
8131 'test.1@gmail.com' ,
8232 'test@1337.com' ,
@@ -90,10 +40,10 @@ describe('Validators', () => {
9040 'foo@bar.co.uk.' ,
9141 'z@co.c' ,
9242 'gmailgmailgmailgmailgmail@gmail.com' ,
93- `${ repeat ( 'a' , 64 ) } @${ repeat ( 'a' , 251 ) } .com` ,
94- `${ repeat ( 'a' , 65 ) } @${ repeat ( 'a' , 250 ) } .com` ,
95- `${ repeat ( 'a' , 64 ) } @${ repeat ( 'a' , 64 ) } .com` ,
96- `${ repeat ( 'a' , 64 ) } @${ repeat ( 'a' , 63 ) } .${ repeat ( 'a' , 63 ) } .${ repeat ( 'a' , 63 ) } .${ repeat ( 'a' , 58 ) } .com` ,
43+ `${ 'a' . repeat ( 64 ) } @${ 'a' . repeat ( 251 ) } .com` ,
44+ `${ 'a' . repeat ( 65 ) } @${ 'a' . repeat ( 250 ) } .com` ,
45+ `${ 'a' . repeat ( 64 ) } @${ 'a' . repeat ( 64 ) } .com` ,
46+ `${ 'a' . repeat ( 64 ) } @${ 'a' . repeat ( 63 ) } .${ 'a' . repeat ( 63 ) } .${ 'a' . repeat ( 63 ) } .${ 'a' . repeat ( 58 ) } .com` ,
9747 'test1@invalid.co m' ,
9848 'test2@invalid.co m' ,
9949 'test3@invalid.co m' ,
@@ -128,10 +78,10 @@ describe('Validators', () => {
12878 'foobar@gmail.com' ,
12979 'foo.bar@gmail.com' ,
13080 'foo.bar@googlemail.com' ,
131- `${ repeat ( 'a' , 30 ) } @gmail.com` ,
81+ `${ 'a' . repeat ( 30 ) } @gmail.com` ,
13282 ] ,
13383 invalid : [
134- `${ repeat ( 'a' , 31 ) } @gmail.com` ,
84+ `${ 'a' . repeat ( 31 ) } @gmail.com` ,
13585 'test@gmail.com' ,
13686 'test.1@gmail.com' ,
13787 '.foobar@gmail.com' ,
0 commit comments