@@ -105,10 +105,44 @@ describe('validator', () => {
105
105
}
106
106
) ;
107
107
} ) ;
108
+
109
+ it ( 'passes value on resolve' , done => {
110
+ new Schema ( {
111
+ v : [
112
+ {
113
+ validator ( rule , value , callback ) {
114
+ callback ( new Error ( 'e1' ) ) ;
115
+ } ,
116
+ } ,
117
+ {
118
+ validator ( rule , value , callback ) {
119
+ callback ( new Error ( 'e2' ) ) ;
120
+ } ,
121
+ } ,
122
+ ] ,
123
+ v2 : [
124
+ {
125
+ validator ( rule , value , callback ) {
126
+ callback ( new Error ( 'e3' ) ) ;
127
+ } ,
128
+ } ,
129
+ ] ,
130
+ } ) . validate (
131
+ {
132
+ v : 2 ,
133
+ } ,
134
+ errors => {
135
+ assert ( errors . length === 2 ) ;
136
+ assert ( errors [ 0 ] . message === 'e1' ) ;
137
+ assert ( errors [ 1 ] . message === 'e3' ) ;
138
+ done ( ) ;
139
+ }
140
+ ) ;
141
+ } ) ;
108
142
} ) ;
109
143
110
144
describe ( 'promise validator' , ( ) => {
111
- it ( 'works' , done => {
145
+ it ( 'works with reject ' , done => {
112
146
new Schema ( {
113
147
v : [
114
148
{
@@ -142,6 +176,40 @@ describe('promise validator', () => {
142
176
) ;
143
177
} ) ;
144
178
179
+ it ( 'works with resolve' , done => {
180
+ new Schema ( {
181
+ v : [
182
+ {
183
+ validator ( ) {
184
+ return Promise . resolve ( new Error ( 'e1' ) ) ;
185
+ } ,
186
+ } ,
187
+ {
188
+ validator ( ) {
189
+ return Promise . resolve ( new Error ( 'e2' ) ) ;
190
+ } ,
191
+ } ,
192
+ ] ,
193
+ v2 : [
194
+ {
195
+ validator ( ) {
196
+ return Promise . resolve ( new Error ( 'e3' ) ) ;
197
+ } ,
198
+ } ,
199
+ ] ,
200
+ } ) . validate (
201
+ {
202
+ v : 2 ,
203
+ } ,
204
+ errors => {
205
+ assert ( errors . length === 2 ) ;
206
+ assert ( errors [ 0 ] . message === 'e1' ) ;
207
+ assert ( errors [ 1 ] . message === 'e3' ) ;
208
+ done ( ) ;
209
+ }
210
+ ) ;
211
+ } ) ;
212
+
145
213
it ( 'should return null when no errors' , done => {
146
214
new Schema ( {
147
215
v : [
@@ -256,7 +324,7 @@ describe('promise validator with promise callaback', () => {
256
324
v : [
257
325
{
258
326
validator ( ) {
259
- return Promise . resolve ( ) ; ;
327
+ return Promise . resolve ( ) ;
260
328
} ,
261
329
} ,
262
330
] ,
@@ -270,7 +338,7 @@ describe('promise validator with promise callaback', () => {
270
338
v3 : [
271
339
{
272
340
validator ( ) {
273
- return Promise . resolve ( ) ; ;
341
+ return Promise . resolve ( ) ;
274
342
} ,
275
343
} ,
276
344
] ,
@@ -284,7 +352,7 @@ describe('promise validator with promise callaback', () => {
284
352
assert . deepEqual ( errors , null ) ;
285
353
} ) ;
286
354
287
- it ( 'should resolve with errors and fields when rules fail' , async ( ) => {
355
+ it ( 'should resolve with errors and fields when rules fail with reject ' , async ( ) => {
288
356
const validator = new Schema ( {
289
357
v : [
290
358
{
@@ -320,6 +388,42 @@ describe('promise validator with promise callaback', () => {
320
388
assert . equal ( errors [ 1 ] . message , 'e3' ) ;
321
389
} ) ;
322
390
391
+ it ( 'should resolve with errors and fields when rules resolve with value' , async ( ) => {
392
+ const validator = new Schema ( {
393
+ v : [
394
+ {
395
+ validator ( ) {
396
+ return Promise . resolve ( 'e1' ) ;
397
+ } ,
398
+ } ,
399
+ {
400
+ validator ( ) {
401
+ return Promise . resolve ( 'e2' ) ;
402
+ } ,
403
+ } ,
404
+ ] ,
405
+ v2 : [
406
+ {
407
+ validator ( ) {
408
+ return Promise . resolve ( 'e3' ) ;
409
+ } ,
410
+ } ,
411
+ ] ,
412
+ } )
413
+
414
+ const { errors, fields } = await validator . validate (
415
+ {
416
+ v : 2 ,
417
+ }
418
+ ) ;
419
+
420
+
421
+ assert ( errors . length === 2 ) ;
422
+ assert ( Object . keys ( fields ) . length === 2 ) ;
423
+ assert . equal ( errors [ 0 ] . message , 'e1' ) ;
424
+ assert . equal ( errors [ 1 ] . message , 'e3' ) ;
425
+ } ) ;
426
+
323
427
it ( 'should resolve with one error and field when rules fail `options.first`' , async ( ) => {
324
428
const validator = new Schema (
325
429
{
0 commit comments