@@ -3,31 +3,67 @@ import { joiResolver } from '..';
33
44const schema = Joi . object ( {
55 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 ( ) ,
97 repeatPassword : Joi . ref ( 'password' ) ,
10-
118 accessToken : [ Joi . string ( ) , Joi . number ( ) ] ,
12-
139 birthYear : Joi . number ( ) . integer ( ) . min ( 1900 ) . max ( 2013 ) ,
14-
1510 email : Joi . string ( ) . email ( {
1611 minDomainSegments : 2 ,
1712 tlds : { allow : [ 'com' , 'net' ] } ,
1813 } ) ,
14+ tags : Joi . array ( ) . items ( Joi . string ( ) ) . required ( ) ,
15+ enabled : Joi . boolean ( ) . required ( ) ,
1916} ) ;
2017
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+
2129describe ( '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+ 37+ tags : [ 'tag1' , 'tag2' ] ,
38+ enabled : true ,
39+ } ;
40+
41+ const result = await joiResolver ( schema ) ( data ) ;
42+
43+ expect ( result ) . toEqual ( { errors : { } , values : data } ) ;
2844 } ) ;
2945
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 ( ) ;
3268 } ) ;
3369} ) ;
0 commit comments