@@ -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,6 +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 } ) ;
345+
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+ } ;
352+
353+ let check = v . compile ( schema ) ;
354+ let obj = { id : "string" , name : "John" , password : "123456" } ;
355+
356+ let res = check ( obj ) ;
357+ expect ( res ) . toBeInstanceOf ( Array ) ;
358+ expect ( res . length ) . toBe ( 1 ) ;
359+ expect ( res [ 0 ] ) . toEqual ( {
360+ type : "number" ,
361+ field : "id" ,
362+ message : "The 'id' field must be a number." ,
363+ actual : "string" ,
364+ } ) ;
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" } ] ) ;
394+ } ) ;
395+ } ) ;
396+
343397 /*
344398 describe("Test check generator with custom path & parent", () => {
345399 it("when schema is defined as an array, and custom path & parent are specified, they should be forwarded to validators", () => {
@@ -452,7 +506,7 @@ describe("Test custom validation v1", () => {
452506 min : 10 ,
453507 max : 15 ,
454508 integer : true ,
455- custom ( value ) {
509+ custom ( value ) {
456510 if ( value % 2 !== 0 ) return [ { type : "evenNumber" , actual : value } ] ;
457511 }
458512 }
@@ -469,20 +523,20 @@ describe("Test custom validation v1", () => {
469523 min : 10 ,
470524 max : 15 ,
471525 integer : true ,
472- custom ( value ) {
526+ custom ( value ) {
473527 fn ( this , value ) ;
474528 if ( value % 2 !== 0 ) return [ { type : "evenNumber" , actual : value } ] ;
475529 }
476530 }
477531 } ) ;
478532
479- const res = check ( { num : 12 } ) ;
533+ const res = check ( { num : 12 } ) ;
480534 expect ( res ) . toBe ( true ) ;
481535 expect ( fn ) . toBeCalledWith ( v , 12 ) ;
482536
483- expect ( check ( { num : 8 } ) [ 0 ] . type ) . toEqual ( "numberMin" ) ;
484- expect ( check ( { num : 18 } ) [ 0 ] . type ) . toEqual ( "numberMax" ) ;
485- 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" ) ;
486540 } ) ;
487541
488542 it ( "should work with multiple custom validators" , ( ) => {
@@ -491,21 +545,21 @@ describe("Test custom validation v1", () => {
491545 const check = v . compile ( {
492546 a : {
493547 type : "number" ,
494- custom ( value ) {
548+ custom ( value ) {
495549 fn ( value ) ;
496550 if ( value % 2 !== 0 ) return [ { type : "evenNumber" , actual : value } ] ;
497551 }
498552 } ,
499553 b : {
500554 type : "number" ,
501- custom ( value ) {
555+ custom ( value ) {
502556 fn ( value ) ;
503557 if ( value % 2 !== 0 ) return [ { type : "evenNumber" , actual : value } ] ;
504558 }
505559 }
506560 } ) ;
507561
508- const res = check ( { a : 12 , b :10 } ) ;
562+ const res = check ( { a : 12 , b : 10 } ) ;
509563 expect ( res ) . toBe ( true ) ;
510564 expect ( fn ) . toBeCalledTimes ( 2 ) ;
511565 } ) ;
@@ -531,8 +585,8 @@ describe("Test custom validation", () => {
531585 min : 10 ,
532586 max : 15 ,
533587 integer : true ,
534- custom ( value , errors ) {
535- fn ( this , value , errors ) ;
588+ custom ( value , errors ) {
589+ fn ( this , value , errors ) ;
536590 if ( value % 2 !== 0 ) errors . push ( { type : "evenNumber" , actual : value } ) ;
537591 return value ;
538592 }
@@ -543,13 +597,13 @@ describe("Test custom validation", () => {
543597 } ) ;
544598
545599 it ( "should work correctly with custom validator" , ( ) => {
546- const res = check ( { num : 12 } ) ;
600+ const res = check ( { num : 12 } ) ;
547601 expect ( res ) . toBe ( true ) ;
548602 expect ( fn ) . toBeCalledWith ( v , 12 , [ ] ) ;
549603
550- expect ( check ( { num : 8 } ) [ 0 ] . type ) . toEqual ( "numberMin" ) ;
551- expect ( check ( { num : 18 } ) [ 0 ] . type ) . toEqual ( "numberMax" ) ;
552- 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" ) ;
553607 } ) ;
554608
555609 it ( "should call checker function after build-in rule" , ( ) => {
@@ -599,7 +653,7 @@ describe("Test default values", () => {
599653 arr : {
600654 type : "array" ,
601655 items : "number" ,
602- default : [ 1 , 2 , 3 ]
656+ default : [ 1 , 2 , 3 ]
603657 } ,
604658 obj : {
605659 type : "object" ,
@@ -642,7 +696,7 @@ describe("Test default values", () => {
642696 num : 123 ,
643697 boolT : true ,
644698 boolF : false ,
645- arr : [ 1 , 2 , 3 ] ,
699+ arr : [ 1 , 2 , 3 ] ,
646700 obj : {
647701 id : 1 ,
648702 name : "abc" ,
@@ -655,7 +709,7 @@ describe("Test default values", () => {
655709 } ) ;
656710
657711 expect ( fn ) . toBeCalledTimes ( 1 ) ;
658- 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 ) ) ;
659713 } ) ;
660714} ) ;
661715
@@ -776,7 +830,7 @@ describe("Test plugins", () => {
776830 } ) ;
777831} ) ;
778832
779- describe ( "Test addMessage" , ( ) => {
833+ describe ( "Test addMessage" , ( ) => {
780834 const v = new Validator ( ) ;
781835 v . addMessage ( "string" , "C" ) ;
782836 expect ( v . messages . string ) . toBe ( "C" ) ;
@@ -977,7 +1031,7 @@ describe("Test normalize", () => {
9771031 d : {
9781032 type : "multi" ,
9791033 optional : false ,
980- rules : [ { type : "string" } , { type : "boolean" } ]
1034+ rules : [ { type : "string" } , { type : "boolean" } ]
9811035 } ,
9821036 e : {
9831037 type : "array" ,
0 commit comments