@@ -363,6 +363,22 @@ describe('fingerprint for different keywords', () => {
363363 data : { email : 'INVALID@EMAIL.COM' } ,
364364 fingerprint : 'Contact.email pattern:^[a-z]+$' ,
365365 } ,
366+ {
367+ inputName : 'Contact' ,
368+ schema : {
369+ type : 'object' ,
370+ properties : {
371+ email : {
372+ type : 'string' ,
373+ pattern : '^[a-z]+$' ,
374+ errorMessages : { pattern : 'must be lowercase' } ,
375+ } ,
376+ } ,
377+ required : [ 'email' ] ,
378+ } ,
379+ data : { email : 'INVALID@EMAIL.COM' } ,
380+ fingerprint : 'Contact.email pattern:must be lowercase' ,
381+ } ,
366382 {
367383 inputName : 'Record' ,
368384 schema : {
@@ -373,6 +389,22 @@ describe('fingerprint for different keywords', () => {
373389 data : { status : 'unknown' } ,
374390 fingerprint : 'Record.status enum:active,inactive' ,
375391 } ,
392+ {
393+ inputName : 'Record' ,
394+ schema : {
395+ type : 'object' ,
396+ properties : {
397+ status : {
398+ type : 'string' ,
399+ enum : [ 'active' , 'inactive' ] ,
400+ errorMessages : { enum : 'must be active or inactive' } ,
401+ } ,
402+ } ,
403+ required : [ 'status' ] ,
404+ } ,
405+ data : { status : 'unknown' } ,
406+ fingerprint : 'Record.status enum:must be active or inactive' ,
407+ } ,
376408 {
377409 inputName : 'Person' ,
378410 schema : {
@@ -437,3 +469,108 @@ describe('fingerprint for different keywords', () => {
437469 } ,
438470 )
439471} )
472+
473+ describe ( 'error params with custom msg' , ( ) => {
474+ test ( 'should drop params when a custom msg overrides the rule' , ( ) => {
475+ const schemaWithMsg = j . string ( ) . regex ( / ^ [ 0 - 9 ] { 2 } $ / , { msg : 'is not a valid Oompa-loompa' } )
476+ const [ err ] = schemaWithMsg . getValidationResult ( 'abc' )
477+ expect ( err ! . data ) . toMatchInlineSnapshot ( `
478+ {
479+ "errors": [
480+ {
481+ "instancePath": "",
482+ "keyword": "pattern",
483+ "message": "is not a valid Oompa-loompa",
484+ "params": {},
485+ "schemaPath": "#/pattern",
486+ },
487+ ],
488+ "fingerprint": "Object pattern:is not a valid Oompa-loompa",
489+ "inputName": "Object",
490+ }
491+ ` )
492+ expect ( err ! . data . errors [ 0 ] ! . params ) . toEqual ( { } )
493+ } )
494+
495+ test ( 'should preserve params when no custom msg is provided' , ( ) => {
496+ const plainSchema = j . string ( ) . regex ( / ^ [ 0 - 9 ] { 2 } $ / )
497+
498+ const [ err ] = plainSchema . getValidationResult ( 'abc' )
499+
500+ expect ( err ) . toMatchInlineSnapshot ( `
501+ [AjvValidationError: Object must match pattern "^[0-9]{2}$"
502+ Input: abc]
503+ ` )
504+ expect ( err ! . data ) . toMatchInlineSnapshot ( `
505+ {
506+ "errors": [
507+ {
508+ "instancePath": "",
509+ "keyword": "pattern",
510+ "message": "must match pattern "^[0-9]{2}$"",
511+ "params": {
512+ "pattern": "^[0-9]{2}$",
513+ },
514+ "schemaPath": "#/pattern",
515+ },
516+ ],
517+ "fingerprint": "Object pattern:^[0-9]{2}$",
518+ "inputName": "Object",
519+ }
520+ ` )
521+ } )
522+
523+ test ( 'should drop params for nested property with custom msg' , ( ) => {
524+ const schema = j . object < { foo : string } > ( {
525+ foo : j . string ( ) . pattern ( '^abc$' , { msg : 'must equal "abc"' } ) ,
526+ } )
527+
528+ const [ err ] = schema . getValidationResult ( { foo : 'def' } )
529+
530+ expect ( err ) . toMatchInlineSnapshot ( `
531+ [AjvValidationError: Object.foo must equal "abc"
532+ Input: { foo: 'def' }]
533+ ` )
534+ expect ( err ! . data ) . toMatchInlineSnapshot ( `
535+ {
536+ "errors": [
537+ {
538+ "instancePath": ".foo",
539+ "keyword": "pattern",
540+ "message": "must equal "abc"",
541+ "params": {},
542+ "schemaPath": "#/properties/foo/pattern",
543+ },
544+ ],
545+ "fingerprint": "Object.foo pattern:must equal "abc"",
546+ "inputName": "Object",
547+ }
548+ ` )
549+ } )
550+
551+ test ( 'should drop params for enum with custom msg' , ( ) => {
552+ const schema = j . enum ( [ 'foo' , 'bar' ] , { msg : 'must be foo or bar' } )
553+
554+ const [ err ] = schema . getValidationResult ( 'baz' )
555+
556+ expect ( err ) . toMatchInlineSnapshot ( `
557+ [AjvValidationError: Object must be foo or bar
558+ Input: baz]
559+ ` )
560+ expect ( err ! . data ) . toMatchInlineSnapshot ( `
561+ {
562+ "errors": [
563+ {
564+ "instancePath": "",
565+ "keyword": "enum",
566+ "message": "must be foo or bar",
567+ "params": {},
568+ "schemaPath": "#/enum",
569+ },
570+ ],
571+ "fingerprint": "Object enum:must be foo or bar",
572+ "inputName": "Object",
573+ }
574+ ` )
575+ } )
576+ } )
0 commit comments