@@ -223,6 +223,28 @@ function getArticle(type) {
223
223
return 'a' ;
224
224
}
225
225
226
+ function getSchemaNonTypes ( schema ) {
227
+ if ( ! schema . type ) {
228
+ if ( likeNumber ( schema ) || likeInteger ( schema ) ) {
229
+ return ' | should be any non-number' ;
230
+ }
231
+
232
+ if ( likeString ( schema ) ) {
233
+ return ' | should be any non-string' ;
234
+ }
235
+
236
+ if ( likeArray ( schema ) ) {
237
+ return ' | should be any non-array' ;
238
+ }
239
+
240
+ if ( likeObject ( schema ) ) {
241
+ return ' | should be any non-object' ;
242
+ }
243
+ }
244
+
245
+ return '' ;
246
+ }
247
+
226
248
class ValidationError extends Error {
227
249
constructor ( errors , schema , configuration = { } ) {
228
250
super ( ) ;
@@ -280,12 +302,18 @@ class ValidationError extends Error {
280
302
return `non ${ formatInnerSchema ( schema . not ) } ` ;
281
303
}
282
304
283
- // eslint-disable-next-line default-case
284
- switch ( schema . instanceof ) {
285
- case 'Function' :
286
- return 'function' ;
287
- case 'RegExp' :
288
- return 'RegExp' ;
305
+ if ( schema . instanceof ) {
306
+ if ( Array . isArray ( schema . instanceof ) ) {
307
+ return schema . instanceof . map ( formatInnerSchema ) . join ( ' | ' ) ;
308
+ }
309
+
310
+ // eslint-disable-next-line default-case
311
+ switch ( schema . instanceof ) {
312
+ case 'Function' :
313
+ return 'function' ;
314
+ case 'RegExp' :
315
+ return 'RegExp' ;
316
+ }
289
317
}
290
318
291
319
if ( schema . enum ) {
@@ -687,101 +715,129 @@ class ValidationError extends Error {
687
715
case 'pattern' :
688
716
return `${ dataPath } should match pattern ${ JSON . stringify (
689
717
error . params . pattern
718
+ ) } ${ getSchemaNonTypes (
719
+ error . parentSchema
690
720
) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
691
721
case 'format' :
692
722
return `${ dataPath } should match format ${ JSON . stringify (
693
723
error . params . format
724
+ ) } ${ getSchemaNonTypes (
725
+ error . parentSchema
694
726
) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
695
727
case 'formatMinimum' :
696
728
case 'formatMaximum' :
697
729
return `${ dataPath } should be ${
698
730
error . params . comparison
699
- } ${ JSON . stringify ( error . params . limit ) } . ${ this . getSchemaPartDescription (
731
+ } ${ JSON . stringify ( error . params . limit ) } ${ getSchemaNonTypes (
700
732
error . parentSchema
701
- ) } `;
733
+ ) } . ${ this . getSchemaPartDescription ( error . parentSchema ) } `;
702
734
case 'minimum' :
703
735
case 'maximum' :
704
736
case 'exclusiveMinimum' :
705
737
case 'exclusiveMaximum' :
706
738
return `${ dataPath } should be ${ error . params . comparison } ${
707
739
error . params . limit
708
- } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
740
+ } ${ getSchemaNonTypes (
741
+ error . parentSchema
742
+ ) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
709
743
case 'multipleOf' :
710
744
return `${ dataPath } should be multiple of ${
711
745
error . params . multipleOf
712
- } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
746
+ } ${ getSchemaNonTypes (
747
+ error . parentSchema
748
+ ) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
713
749
case 'patternRequired' :
714
750
return `${ dataPath } should have property matching pattern ${ JSON . stringify (
715
751
error . params . missingPattern
752
+ ) } ${ getSchemaNonTypes (
753
+ error . parentSchema
716
754
) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
717
755
case 'minLength' : {
718
756
if ( error . params . limit === 1 ) {
719
- return `${ dataPath } should be an non-empty string. ${ this . getSchemaPartDescription (
757
+ return `${ dataPath } should be an non-empty string${ getSchemaNonTypes (
720
758
error . parentSchema
721
- ) } `;
759
+ ) } . ${ this . getSchemaPartDescription ( error . parentSchema ) } `;
722
760
}
723
761
724
762
return `${ dataPath } should not be shorter than ${
725
763
error . params . limit
726
- } characters.${ this . getSchemaPartDescription ( error . parentSchema ) } `;
764
+ } characters${ getSchemaNonTypes (
765
+ error . parentSchema
766
+ ) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
727
767
}
728
768
case 'minItems' : {
729
769
if ( error . params . limit === 1 ) {
730
- return `${ dataPath } should be an non-empty array. ${ this . getSchemaPartDescription (
770
+ return `${ dataPath } should be an non-empty array${ getSchemaNonTypes (
731
771
error . parentSchema
732
- ) } `;
772
+ ) } . ${ this . getSchemaPartDescription ( error . parentSchema ) } `;
733
773
}
734
774
735
775
return `${ dataPath } should not have fewer than ${
736
776
error . params . limit
737
- } items.${ this . getSchemaPartDescription ( error . parentSchema ) } `;
777
+ } items${ getSchemaNonTypes (
778
+ error . parentSchema
779
+ ) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
738
780
}
739
781
case 'minProperties' : {
740
782
if ( error . params . limit === 1 ) {
741
- return `${ dataPath } should be an non-empty object. ${ this . getSchemaPartDescription (
783
+ return `${ dataPath } should be an non-empty object${ getSchemaNonTypes (
742
784
error . parentSchema
743
- ) } `;
785
+ ) } . ${ this . getSchemaPartDescription ( error . parentSchema ) } `;
744
786
}
745
787
746
788
return `${ dataPath } should not have fewer than ${
747
789
error . params . limit
748
- } properties.${ this . getSchemaPartDescription ( error . parentSchema ) } `;
790
+ } properties${ getSchemaNonTypes (
791
+ error . parentSchema
792
+ ) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
749
793
}
750
794
case 'maxLength' :
751
795
return `${ dataPath } should not be longer than ${
752
796
error . params . limit
753
- } characters.${ this . getSchemaPartDescription ( error . parentSchema ) } `;
797
+ } characters${ getSchemaNonTypes (
798
+ error . parentSchema
799
+ ) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
754
800
case 'maxItems' :
755
801
return `${ dataPath } should not have more than ${
756
802
error . params . limit
757
- } items.${ this . getSchemaPartDescription ( error . parentSchema ) } `;
803
+ } items${ getSchemaNonTypes (
804
+ error . parentSchema
805
+ ) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
758
806
case 'maxProperties' :
759
807
return `${ dataPath } should not have more than ${
760
808
error . params . limit
761
- } properties.${ this . getSchemaPartDescription ( error . parentSchema ) } `;
809
+ } properties${ getSchemaNonTypes (
810
+ error . parentSchema
811
+ ) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
762
812
case 'uniqueItems' :
763
813
return `${ dataPath } should not contain the item '${
764
814
error . data [ error . params . i ]
765
- } ' twice.${ this . getSchemaPartDescription ( error . parentSchema ) } `;
815
+ } ' twice${ getSchemaNonTypes (
816
+ error . parentSchema
817
+ ) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
766
818
case 'additionalItems' :
767
819
return `${ dataPath } should not have more than ${
768
820
error . params . limit
769
- } items. These items are valid:\n${ this . getSchemaPartText (
821
+ } items${ getSchemaNonTypes (
822
+ error . parentSchema
823
+ ) } . These items are valid:\n${ this . getSchemaPartText (
770
824
error . parentSchema
771
825
) } `;
772
826
case 'contains' :
773
827
return `${ dataPath } should contains at least one ${ this . getSchemaPartText (
774
828
error . parentSchema ,
775
829
[ 'contains' ]
776
- ) } item.`;
830
+ ) } item${ getSchemaNonTypes ( error . parentSchema ) } .`;
777
831
case 'required' : {
778
832
const missingProperty = error . params . missingProperty . replace ( / ^ \. / , '' ) ;
779
833
const hasProperty = Boolean (
780
834
error . parentSchema . properties &&
781
835
error . parentSchema . properties [ missingProperty ]
782
836
) ;
783
837
784
- return `${ dataPath } misses the property '${ missingProperty } '.${
838
+ return `${ dataPath } misses the property '${ missingProperty } '${ getSchemaNonTypes (
839
+ error . parentSchema
840
+ ) } .${
785
841
hasProperty
786
842
? ` Should be:\n${ this . getSchemaPartText ( error . parentSchema , [
787
843
'properties' ,
@@ -793,7 +849,9 @@ class ValidationError extends Error {
793
849
case 'additionalProperties' :
794
850
return `${ dataPath } has an unknown property '${
795
851
error . params . additionalProperty
796
- } '. These properties are valid:\n${ this . getSchemaPartText (
852
+ } '${ getSchemaNonTypes (
853
+ error . parentSchema
854
+ ) } . These properties are valid:\n${ this . getSchemaPartText (
797
855
error . parentSchema
798
856
) } `;
799
857
case 'dependencies' : {
@@ -804,12 +862,16 @@ class ValidationError extends Error {
804
862
805
863
return `${ dataPath } should have properties ${ dependencies } when property '${
806
864
error . params . property
807
- } ' is present.${ this . getSchemaPartDescription ( error . parentSchema ) } `;
865
+ } ' is present${ getSchemaNonTypes (
866
+ error . parentSchema
867
+ ) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
808
868
}
809
869
case 'propertyNames' : {
810
- const invalidProperty = error . params . propertyName ;
811
-
812
- return `${ dataPath } property name '${ invalidProperty } ' is invalid. Property names should be match format ${ JSON . stringify (
870
+ return `${ dataPath } property name '${
871
+ error . params . propertyName
872
+ } ' is invalid${ getSchemaNonTypes (
873
+ error . parentSchema
874
+ ) } . Property names should be match format ${ JSON . stringify (
813
875
error . schema . format
814
876
) } .${ this . getSchemaPartDescription ( error . parentSchema ) } `;
815
877
}
0 commit comments