@@ -3,31 +3,67 @@ import { joiResolver } from '..';
3
3
4
4
const schema = Joi . object ( {
5
5
username : Joi . string ( ) . alphanum ( ) . min ( 3 ) . max ( 30 ) . required ( ) ,
6
-
7
- password : Joi . string ( ) . pattern ( new RegExp ( '^[a-zA-Z0-9]{3,30}$' ) ) ,
8
-
6
+ password : Joi . string ( ) . pattern ( new RegExp ( '^[a-zA-Z0-9]{3,30}$' ) ) . required ( ) ,
9
7
repeatPassword : Joi . ref ( 'password' ) ,
10
-
11
8
accessToken : [ Joi . string ( ) , Joi . number ( ) ] ,
12
-
13
9
birthYear : Joi . number ( ) . integer ( ) . min ( 1900 ) . max ( 2013 ) ,
14
-
15
10
email : Joi . string ( ) . email ( {
16
11
minDomainSegments : 2 ,
17
12
tlds : { allow : [ 'com' , 'net' ] } ,
18
13
} ) ,
14
+ tags : Joi . array ( ) . items ( Joi . string ( ) ) . required ( ) ,
15
+ enabled : Joi . boolean ( ) . required ( ) ,
19
16
} ) ;
20
17
18
+ interface Data {
19
+ username : string ;
20
+ password : string ;
21
+ repeatPassword : string ;
22
+ accessToken ?: number | string ;
23
+ birthYear ?: number ;
24
+ email ?: string ;
25
+ tags : string [ ] ;
26
+ enabled : boolean ;
27
+ }
28
+
21
29
describe ( 'joiResolver' , ( ) => {
22
- it ( 'should return correct value' , async ( ) => {
23
- const data = { username : 'abc' , birthYear : 1994 } ;
24
- expect ( await joiResolver ( schema ) ( data ) ) . toEqual ( {
25
- values : data ,
26
- errors : { } ,
27
- } ) ;
30
+ it ( 'should return values from joiResolver when validation pass' , async ( ) => {
31
+ const data : Data = {
32
+ username : 'Doe' ,
33
+ password : 'Password123' ,
34
+ repeatPassword : 'Password123' ,
35
+ birthYear : 2000 ,
36
+ email : 'john@doe.com' ,
37
+ tags : [ 'tag1' , 'tag2' ] ,
38
+ enabled : true ,
39
+ } ;
40
+
41
+ const result = await joiResolver ( schema ) ( data ) ;
42
+
43
+ expect ( result ) . toEqual ( { errors : { } , values : data } ) ;
28
44
} ) ;
29
45
30
- it ( 'should return errors' , async ( ) => {
31
- expect ( await joiResolver ( schema ) ( { } ) ) . toMatchSnapshot ( ) ;
46
+ it ( 'should return a single error from joiResolver when validation fails' , async ( ) => {
47
+ const data = {
48
+ password : '___' ,
49
+ email : '' ,
50
+ birthYear : 'birthYear' ,
51
+ } ;
52
+
53
+ const result = await joiResolver ( schema ) ( data ) ;
54
+
55
+ expect ( result ) . toMatchSnapshot ( ) ;
56
+ } ) ;
57
+
58
+ it ( 'should return all the errors from joiResolver when validation fails with `validateAllFieldCriteria` set to true' , async ( ) => {
59
+ const data = {
60
+ password : '___' ,
61
+ email : '' ,
62
+ birthYear : 'birthYear' ,
63
+ } ;
64
+
65
+ const result = await joiResolver ( schema ) ( data , undefined , true ) ;
66
+
67
+ expect ( result ) . toMatchSnapshot ( ) ;
32
68
} ) ;
33
69
} ) ;
0 commit comments