1
1
import { validate } from '../validate' ;
2
2
import { hasProperties } from './has-properties' ;
3
3
import { greaterThan , required , string } from '../rules' ;
4
- import { assertType , Dictionary , returns } from '../type-assertions ' ;
4
+ import { assertType , Dictionary , returns } from '../utils ' ;
5
5
import { mapValues } from 'lodash-es' ;
6
6
7
7
describe ( 'object' , ( ) => {
8
8
const requiredString = [ required ( ) , string ( ) ] ;
9
9
10
- it ( 'should return messages in order' , ( ) => {
10
+ it ( 'should return messages in order' , async ( ) => {
11
11
const test = { firstName : 1 } ;
12
12
const rules = { firstName : [ string ( ) , greaterThan ( 2 ) ] } ;
13
- const result = validate ( test , hasProperties ( rules ) ) ;
13
+ const result = await validate ( test , hasProperties ( rules ) ) ;
14
14
const messages = result . flattened ( ) ;
15
15
expect ( messages ) . toHaveLength ( 2 ) ;
16
16
expect ( messages [ 0 ] ) . toMatch ( / m u s t b e a s t r i n g / ) ;
17
17
expect ( messages [ 1 ] ) . toMatch ( / m u s t b e g r e a t e r t h a n / ) ;
18
18
} ) ;
19
19
20
- it ( 'should not return properties that have passed validation' , ( ) => {
20
+ it ( 'should not return properties that have passed validation' , async ( ) => {
21
21
const test = { firstName : 'tim' } ;
22
22
const rules = { firstName : requiredString , lastName : requiredString } ;
23
- const result = validate ( test , hasProperties ( rules ) ) . all ( ) ;
23
+ const result = ( await validate ( test , hasProperties ( rules ) ) ) . all ( ) ;
24
24
if ( result ) {
25
25
expect ( result . firstName ) . toBe ( undefined ) ;
26
26
expect ( result . hasOwnProperty ( 'firstName' ) ) . toBe ( false ) ;
27
27
expect ( 'firstName' in result ) . toBe ( false ) ;
28
28
}
29
29
} ) ;
30
30
31
- it ( 'should retain the shape of the input in the message object' , ( ) => {
31
+ it ( 'should retain the shape of the input in the message object' , async ( ) => {
32
32
const test = { firstName : 'tim' } ;
33
33
const rules = { firstName : requiredString , lastName : requiredString } ;
34
- const result = validate ( test , hasProperties ( rules ) ) . all ( ) ;
34
+ const result = ( await validate ( test , hasProperties ( rules ) ) ) . all ( ) ;
35
35
if ( result ) {
36
36
assertType < string [ ] | undefined > ( result . firstName ) ;
37
37
assertType < typeof result . firstName > ( returns < string [ ] | undefined > ( ) ) ;
@@ -41,11 +41,11 @@ describe('object', () => {
41
41
}
42
42
} ) ;
43
43
44
- it ( 'should retain the shape of child objects of the input in the message object' , ( ) => {
44
+ it ( 'should retain the shape of child objects of the input in the message object' , async ( ) => {
45
45
const test = { parent : { firstName : 'tim' } } ;
46
- const result = validate ( test , hasProperties ( {
46
+ const result = ( await validate ( test , hasProperties ( {
47
47
parent : hasProperties ( { firstName : requiredString } ) ,
48
- } ) ) . all ( ) ;
48
+ } ) ) ) . all ( ) ;
49
49
50
50
if ( result ) {
51
51
assertType < { parent ?: { firstName ?: string [ ] } } > ( result ) ;
@@ -63,8 +63,8 @@ describe('object', () => {
63
63
}
64
64
} ) ;
65
65
66
- it ( 'should work' , ( ) => {
67
- const result = validate (
66
+ it ( 'should work' , async ( ) => {
67
+ const result = await validate (
68
68
{ firstName : 'tim' } ,
69
69
hasProperties ( {
70
70
firstName : [ required ( ) ] ,
@@ -78,32 +78,32 @@ describe('object', () => {
78
78
expect ( messages [ 0 ] ) . toMatch ( / r e q u i r e d / ) ;
79
79
} ) ;
80
80
81
- it ( 'should correctly pass the key option' , ( ) => {
81
+ it ( 'should correctly pass the key option' , async ( ) => {
82
82
const constraint = jest . fn ( ( ) => undefined ) ;
83
83
const parent : Dictionary < number > = { a : 1 , b : 2 } ;
84
- validate ( parent , hasProperties ( mapValues ( parent , ( ) => constraint ) ) ) ;
84
+ await validate ( parent , hasProperties ( mapValues ( parent , ( ) => constraint ) ) ) ;
85
85
Object . keys ( parent ) . forEach ( ( key , index ) => {
86
86
const options = expect . objectContaining ( { key } ) ;
87
87
expect ( constraint ) . nthCalledWith ( index + 1 , parent [ key ] , options ) ;
88
88
} ) ;
89
89
} ) ;
90
90
91
- it ( 'should correctly pass the key path option' , ( ) => {
91
+ it ( 'should correctly pass the key path option' , async ( ) => {
92
92
const constraint = jest . fn ( ( ) => undefined ) ;
93
93
const parent : Dictionary < number > = { a : 1 , b : 2 } ;
94
- validate ( parent , hasProperties ( mapValues ( parent , ( ) => constraint ) ) ) ;
94
+ await validate ( parent , hasProperties ( mapValues ( parent , ( ) => constraint ) ) ) ;
95
95
Object . keys ( parent ) . forEach ( ( key , index ) => {
96
96
const options = expect . objectContaining ( { keyPath : [ key ] } ) ;
97
97
expect ( constraint ) . nthCalledWith ( index + 1 , parent [ key ] , options ) ;
98
98
} ) ;
99
99
} ) ;
100
100
101
- it ( 'should correctly pass the key path option when nested' , ( ) => {
101
+ it ( 'should correctly pass the key path option when nested' , async ( ) => {
102
102
const constraint = jest . fn ( ( ) => undefined ) ;
103
103
const child : Dictionary < number > = { a : 1 , b : 2 } ;
104
104
const parent : Dictionary < Dictionary < number > > = { c : child , d : child } ;
105
105
const childConstraints = hasProperties ( mapValues ( child , ( ) => constraint ) ) ;
106
- validate ( parent , hasProperties ( mapValues ( parent , ( ) => childConstraints ) ) ) ;
106
+ await validate ( parent , hasProperties ( mapValues ( parent , ( ) => childConstraints ) ) ) ;
107
107
Object . keys ( parent ) . forEach ( ( parentKey , parentIndex ) => {
108
108
Object . keys ( child ) . forEach ( ( childKey , childIndex ) => {
109
109
const index = parentIndex * Object . keys ( child ) . length + childIndex ;
@@ -113,10 +113,10 @@ describe('object', () => {
113
113
} ) ;
114
114
} ) ;
115
115
116
- it ( 'should correctly pass the parent option' , ( ) => {
116
+ it ( 'should correctly pass the parent option' , async ( ) => {
117
117
const constraint = jest . fn ( ( ) => undefined ) ;
118
118
const parent : Dictionary < number > = { a : 1 , b : 2 } ;
119
- validate ( parent , hasProperties ( mapValues ( parent , ( ) => constraint ) ) ) ;
119
+ await validate ( parent , hasProperties ( mapValues ( parent , ( ) => constraint ) ) ) ;
120
120
Object . keys ( parent ) . forEach ( ( key , index ) => {
121
121
const options = expect . objectContaining ( { parent } ) ;
122
122
expect ( constraint ) . nthCalledWith ( index + 1 , parent [ key ] , options ) ;
0 commit comments