@@ -128,11 +128,11 @@ describe("Test add", () => {
128128 }
129129 } ) ;
130130
131- const validFn = jest . fn ( function ( { schema, messages } , path , context ) {
131+ const validFn = jest . fn ( function ( { schema, messages } , path , context ) {
132132 return {
133133 source : `
134134 if (value % 2 != 0)
135- ${ this . makeError ( { type : "evenNumber" , actual : "value" , messages } ) }
135+ ${ this . makeError ( { type : "evenNumber" , actual : "value" , messages } ) }
136136 return value;
137137 `
138138 } ;
@@ -168,7 +168,7 @@ describe("Test add", () => {
168168 } ) ;
169169
170170 it ( "should check the new rule" , ( ) => {
171- expect ( check ( { a : 5 } ) ) . toEqual ( [ { "type" : "evenNumber" , "field" : "a" , "actual" : 5 , "message" : "The 'a' field must be an even number! Actual: 5" } ] ) ;
171+ expect ( check ( { a : 5 } ) ) . toEqual ( [ { "type" : "evenNumber" , "field" : "a" , "actual" : 5 , "message" : "The 'a' field must be an even number! Actual: 5" } ] ) ;
172172 expect ( check ( { a : 6 } ) ) . toEqual ( true ) ;
173173 } ) ;
174174
@@ -262,7 +262,7 @@ describe("Test getRuleFromSchema method", () => {
262262 } ) ;
263263
264264 expect ( res . schema ) . toEqual ( {
265- type : "object" ,
265+ type : "object" ,
266266 props : {
267267 name : { type : "string" } ,
268268 age : { type : "number" }
@@ -278,7 +278,7 @@ describe("Test getRuleFromSchema method", () => {
278278 } ) ;
279279
280280 expect ( res . schema ) . toEqual ( {
281- type : "object" ,
281+ type : "object" ,
282282 optional : true ,
283283 props : {
284284 name : { type : "string" } ,
@@ -316,7 +316,7 @@ describe("Test compile (integration test)", () => {
316316 let check = v . compile ( schema ) ;
317317
318318 it ( "should give back one errors" , ( ) => {
319- let res = check ( { id : 5 , name : "John" } ) ;
319+ let res = check ( { id : 5 , name : "John" } ) ;
320320 expect ( res ) . toBeInstanceOf ( Array ) ;
321321
322322 expect ( res . length ) . toBe ( 1 ) ;
@@ -340,31 +340,60 @@ describe("Test compile (integration test)", () => {
340340
341341 } ) ;
342342
343- describe ( "Test check generator with wrong obj and haltOnFirstError" , ( ) => {
344- const v = new Validator ( { haltOnFirstError : true } ) ;
343+ describe ( "Test check generator with wrong obj and haltOnFirstError" , ( ) => {
344+ const v = new Validator ( { haltOnFirstError : true } ) ;
345345
346- const schema = {
347- id : { type : "number" } ,
348- name : { type : "string" , min : 5 , optional : true } ,
349- password : { type : "forbidden" }
350- } ;
346+ it ( "should give back one errors" , ( ) => {
347+ const schema = {
348+ id : { type : "number" } ,
349+ name : { type : "string" , min : 5 , uppercase : true } ,
350+ password : { type : "forbidden" }
351+ } ;
351352
352- let check = v . compile ( schema ) ;
353+ let check = v . compile ( schema ) ;
354+ let obj = { id : "string" , name : "John" , password : "123456" } ;
353355
354- it ( "should give back one errors" , ( ) => {
355- let res = check ( { id : "string" , name : "John" , password : "123456" } ) ;
356+ let res = check ( obj ) ;
356357 expect ( res ) . toBeInstanceOf ( Array ) ;
357-
358358 expect ( res . length ) . toBe ( 1 ) ;
359359 expect ( res [ 0 ] ) . toEqual ( {
360360 type : "number" ,
361361 field : "id" ,
362362 message : "The 'id' field must be a number." ,
363363 actual : "string" ,
364364 } ) ;
365+ expect ( obj ) . toEqual ( { id : "string" , name : "John" , password : "123456" } ) ;
366+ } ) ;
367+
368+ it ( "should return true if no errors" , ( ) => {
369+ const schema = {
370+ id : { type : "number" } ,
371+ name : { type : "string" , min : 5 , uppercase : true } ,
372+ password : { type : "forbidden" }
373+ } ;
374+
375+ let check = v . compile ( schema ) ;
376+ let obj = { id : 5 , name : "John Doe" } ;
377+ let res = check ( obj ) ;
378+ expect ( res ) . toBe ( true ) ;
379+ expect ( obj ) . toEqual ( { id : 5 , name : "JOHN DOE" } ) ;
380+ } ) ;
381+
382+ it ( "should return true if has valid in multi rule" , ( ) => {
383+ const schema = {
384+ status : [
385+ { type : "string" , enums : [ "active" , "inactive" ] } ,
386+ { type : "number" , min : 0 }
387+ ]
388+ } ;
389+
390+ let check = v . compile ( schema ) ;
391+ expect ( check ( { status : "active" } ) ) . toBe ( true ) ;
392+ expect ( check ( { status : 1 } ) ) . toBe ( true ) ;
393+ expect ( check ( { status : false } ) ) . toEqual ( [ { "actual" : false , "field" : "status" , "message" : "The 'status' field must be a string." , "type" : "string" } , { "actual" : false , "field" : "status" , "message" : "The 'status' field must be a number." , "type" : "number" } ] ) ;
365394 } ) ;
366395 } ) ;
367-
396+
368397 /*
369398 describe("Test check generator with custom path & parent", () => {
370399 it("when schema is defined as an array, and custom path & parent are specified, they should be forwarded to validators", () => {
@@ -477,7 +506,7 @@ describe("Test custom validation v1", () => {
477506 min : 10 ,
478507 max : 15 ,
479508 integer : true ,
480- custom ( value ) {
509+ custom ( value ) {
481510 if ( value % 2 !== 0 ) return [ { type : "evenNumber" , actual : value } ] ;
482511 }
483512 }
@@ -494,20 +523,20 @@ describe("Test custom validation v1", () => {
494523 min : 10 ,
495524 max : 15 ,
496525 integer : true ,
497- custom ( value ) {
526+ custom ( value ) {
498527 fn ( this , value ) ;
499528 if ( value % 2 !== 0 ) return [ { type : "evenNumber" , actual : value } ] ;
500529 }
501530 }
502531 } ) ;
503532
504- const res = check ( { num : 12 } ) ;
533+ const res = check ( { num : 12 } ) ;
505534 expect ( res ) . toBe ( true ) ;
506535 expect ( fn ) . toBeCalledWith ( v , 12 ) ;
507536
508- expect ( check ( { num : 8 } ) [ 0 ] . type ) . toEqual ( "numberMin" ) ;
509- expect ( check ( { num : 18 } ) [ 0 ] . type ) . toEqual ( "numberMax" ) ;
510- expect ( check ( { num : 13 } ) [ 0 ] . type ) . toEqual ( "evenNumber" ) ;
537+ expect ( check ( { num : 8 } ) [ 0 ] . type ) . toEqual ( "numberMin" ) ;
538+ expect ( check ( { num : 18 } ) [ 0 ] . type ) . toEqual ( "numberMax" ) ;
539+ expect ( check ( { num : 13 } ) [ 0 ] . type ) . toEqual ( "evenNumber" ) ;
511540 } ) ;
512541
513542 it ( "should work with multiple custom validators" , ( ) => {
@@ -516,21 +545,21 @@ describe("Test custom validation v1", () => {
516545 const check = v . compile ( {
517546 a : {
518547 type : "number" ,
519- custom ( value ) {
548+ custom ( value ) {
520549 fn ( value ) ;
521550 if ( value % 2 !== 0 ) return [ { type : "evenNumber" , actual : value } ] ;
522551 }
523552 } ,
524553 b : {
525554 type : "number" ,
526- custom ( value ) {
555+ custom ( value ) {
527556 fn ( value ) ;
528557 if ( value % 2 !== 0 ) return [ { type : "evenNumber" , actual : value } ] ;
529558 }
530559 }
531560 } ) ;
532561
533- const res = check ( { a : 12 , b :10 } ) ;
562+ const res = check ( { a : 12 , b : 10 } ) ;
534563 expect ( res ) . toBe ( true ) ;
535564 expect ( fn ) . toBeCalledTimes ( 2 ) ;
536565 } ) ;
@@ -556,8 +585,8 @@ describe("Test custom validation", () => {
556585 min : 10 ,
557586 max : 15 ,
558587 integer : true ,
559- custom ( value , errors ) {
560- fn ( this , value , errors ) ;
588+ custom ( value , errors ) {
589+ fn ( this , value , errors ) ;
561590 if ( value % 2 !== 0 ) errors . push ( { type : "evenNumber" , actual : value } ) ;
562591 return value ;
563592 }
@@ -568,13 +597,13 @@ describe("Test custom validation", () => {
568597 } ) ;
569598
570599 it ( "should work correctly with custom validator" , ( ) => {
571- const res = check ( { num : 12 } ) ;
600+ const res = check ( { num : 12 } ) ;
572601 expect ( res ) . toBe ( true ) ;
573602 expect ( fn ) . toBeCalledWith ( v , 12 , [ ] ) ;
574603
575- expect ( check ( { num : 8 } ) [ 0 ] . type ) . toEqual ( "numberMin" ) ;
576- expect ( check ( { num : 18 } ) [ 0 ] . type ) . toEqual ( "numberMax" ) ;
577- expect ( check ( { num : 13 } ) [ 0 ] . type ) . toEqual ( "evenNumber" ) ;
604+ expect ( check ( { num : 8 } ) [ 0 ] . type ) . toEqual ( "numberMin" ) ;
605+ expect ( check ( { num : 18 } ) [ 0 ] . type ) . toEqual ( "numberMax" ) ;
606+ expect ( check ( { num : 13 } ) [ 0 ] . type ) . toEqual ( "evenNumber" ) ;
578607 } ) ;
579608
580609 it ( "should call checker function after build-in rule" , ( ) => {
@@ -624,7 +653,7 @@ describe("Test default values", () => {
624653 arr : {
625654 type : "array" ,
626655 items : "number" ,
627- default : [ 1 , 2 , 3 ]
656+ default : [ 1 , 2 , 3 ]
628657 } ,
629658 obj : {
630659 type : "object" ,
@@ -667,7 +696,7 @@ describe("Test default values", () => {
667696 num : 123 ,
668697 boolT : true ,
669698 boolF : false ,
670- arr : [ 1 , 2 , 3 ] ,
699+ arr : [ 1 , 2 , 3 ] ,
671700 obj : {
672701 id : 1 ,
673702 name : "abc" ,
@@ -680,7 +709,7 @@ describe("Test default values", () => {
680709 } ) ;
681710
682711 expect ( fn ) . toBeCalledTimes ( 1 ) ;
683- expect ( fn ) . toBeCalledWith ( schema . par . properties . name , "par.name" , obj , expect . any ( Object ) ) ;
712+ expect ( fn ) . toBeCalledWith ( schema . par . properties . name , "par.name" , obj , expect . any ( Object ) ) ;
684713 } ) ;
685714} ) ;
686715
@@ -801,7 +830,7 @@ describe("Test plugins", () => {
801830 } ) ;
802831} ) ;
803832
804- describe ( "Test addMessage" , ( ) => {
833+ describe ( "Test addMessage" , ( ) => {
805834 const v = new Validator ( ) ;
806835 v . addMessage ( "string" , "C" ) ;
807836 expect ( v . messages . string ) . toBe ( "C" ) ;
@@ -1002,7 +1031,7 @@ describe("Test normalize", () => {
10021031 d : {
10031032 type : "multi" ,
10041033 optional : false ,
1005- rules : [ { type : "string" } , { type : "boolean" } ]
1034+ rules : [ { type : "string" } , { type : "boolean" } ]
10061035 } ,
10071036 e : {
10081037 type : "array" ,
0 commit comments