-
Notifications
You must be signed in to change notification settings - Fork 0
/
validateFactory-test.js
229 lines (203 loc) · 6.86 KB
/
validateFactory-test.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"use strict"
import {ValueManager, loaderFactory, validators} from "Subschema";
import {validateFactory} from "subschema-server-utils";
import expect from "expect";
const loader = loaderFactory();
loader.addValidator(validators);
function all(...args) {
return Promise.all(args);
}
describe("validateFactory", function () {
this.timeout(50000);
it('should validate required', function () {
const validate = validateFactory({
schema: {
test: {
validators: ["required"]
}
}
}, loader);
return all(
validate(ValueManager({
test: null
})).then((r)=>expect(r.test[0].message).toBe('Required')),
validate({
test: 1
}).then(r=>expect(r).toNotExist()),
validate({
test: null
}).then(r=>expect(r.test[0].message).toBe('Required'))
);
});
it('should validate match', function () {
const validate = validateFactory({
schema: {
test: 'Text',
'test2': {
validators: [{"type": "match", field: "test", message: "Match"}]
}
}
}, loader);
return all(
validate(ValueManager({
test: 'stuff',
test2: 'notstuff'
})).then((r)=>expect(r.test2[0].message).toBe('Match')),
validate({
test: 1,
test2: 1
}).then(r=>expect(r).toNotExist()),
);
});
it('should validate nested with errors', function () {
const validate = validateFactory({
schema: {
test: {
subSchema: {
schema: {
deep: {
validators: ["required"]
}
}
}
}
}
}, loader);
return all(
validate(ValueManager({
test: 'abc'
})).then(r=> expect(r['test.deep'][0].message).toBe('Required')),
validate({
test: {deep: 'abc'}
}).then(r=>expect(r).toNotExist()),
validate(ValueManager({})).then(r=>expect(r['test.deep'][0].message).toBe('Required'))
);
});
it('should not validate hidden conditional fields', function () {
const validate = validateFactory({
schema: {
test: {},
cond: {
validators: ['required'],
conditional: {
listen: "test"
}
}
}
}, loader);
return all(
validate({
test: false
}).then(r=>expect(r).toNotExist()),
validate({
test: true
}).then(r=>expect(r.cond[0].message).toBe('Required'))
);
});
it('should validate lists of objects', function () {
const validate = validateFactory({
schema: {
test: {
itemType: {
type: 'Object',
subSchema: {
answer: {
type: 'Text',
validators: ['required']
},
feel: {
type: 'Radio',
options: ['Good', 'Bad', 'Indifferent']
}
}
}
}
}
}, loader);
return all(
validate({
test: [{
feel: 'Good'
}]
}).then(r=> expect(r['test.0.answer'][0].message).toBe('Required')),
validate({
test: [{
feel: 'Good',
answer: 'fine'
}]
}).then(r=>expect(r).toNotExist()),
validate({
test: [{
feel: 'Good'
}]
}).then(r=>expect(r['test.0.answer'][0].message).toBe('Required'))
);
});
it('should resolve asyncly', function () {
var loader = loaderFactory();
loader.addValidator('async', function asyncly(options) {
options = {timeout: 300, message: 'Not Right', ...options};
return function async$config(val) {
return new Promise((result, reject)=> {
setTimeout(()=> {
if (val !== options.value) {
return result({message: options.message});
}
result();
}, options.timeout);
});
}
});
const validate = validateFactory({
schema: {
test: {
validators: [{type: "async", value: 1, message: 'Not Right'}]
}
}
}, loader);
return all(validate({}).then(r=> {
expect(r.test[0].message).toBe('Not Right')
}), validate({test: 1}).then(r=> {
expect(r).toNotExist();
}));
});
it('should deep resolve asyncly', function () {
var loader = loaderFactory();
loader.addValidator('async', function asyncly(options) {
options = {timeout: 300, message: 'Not Right', ...options};
return function async$config(val) {
return new Promise((result, reject)=> {
setTimeout(()=> {
if (val !== options.value) {
return result({message: options.message});
}
result();
}, options.timeout);
});
}
});
const validate = validateFactory({
schema: {
test: {
validators: [{type: "async", value: 1, message: 'Not Right'}]
},
other: {
subSchema: {
more: {
validators: [{type: "async", value: 2, timeout: 400, message: 'Still Not Right'}]
}
}
}
}
}, loader);
return all(validate({}).then(r=> {
expect(r.test[0].message).toBe('Not Right');
expect(r['other.more'][0].message).toBe('Still Not Right');
}), validate({test: 1}).then(r=> {
expect(r.test).toNotExist();
expect(r['other.more'][0].message).toBe('Still Not Right');
}), validate({test: 1, other: {more: 2}}).then(r=> {
expect(r).toNotExist();
}));
});
});