-
Notifications
You must be signed in to change notification settings - Fork 87
/
isValidAsync.spec.js
105 lines (97 loc) · 1.72 KB
/
isValidAsync.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {result} from 'lodash'
import {delay} from './delay'
import {isValidAsync} from './isValidAsync'
const simplePredicate = async x => {
await delay(100)
return x > 5
}
test('happy', async () => {
const input = {
a: 1,
b: 7,
c: 9,
additional: 'foo',
}
const invalidInput = {
a: 1,
b: 2,
c: 9,
}
const schema = {
a: Number,
b: simplePredicate,
c: simplePredicate,
}
const invalidSchema = {
a: Boolean,
b: simplePredicate,
c: simplePredicate,
}
const result = await isValidAsync({
input,
schema,
})
const invalidResult = await isValidAsync({
input,
schema: invalidSchema,
})
const withInvalidInput = await isValidAsync({
input: invalidInput,
schema,
})
expect(result).toBeTruthy()
expect(invalidResult).toBeFalsy()
expect(withInvalidInput).toBeFalsy()
})
test('without async rules', async () => {
const input = {
a: 1,
b: 7,
}
const schema = {
a: Number,
b: x => x > 2,
}
const invalidSchema = {
a: Number,
b: Boolean,
}
const result = await isValidAsync({
input,
schema,
})
const invalidResult = await isValidAsync({
input,
schema: invalidSchema,
})
expect(result).toBeTruthy()
expect(invalidResult).toBeFalsy()
})
test('readme example', async () => {
const input = {
a: 1,
b: 2,
}
const invalidInput = {
a: 1,
b: 'foo',
}
const schema = {
a: Number,
b: async x => {
await delay(100)
return typeof x === 'number'
},
}
const result = await Promise.all([
isValidAsync({
schema,
input,
}),
isValidAsync({
schema,
input: invalidInput,
}),
])
expect(result).toEqual([true, false])
})