17
17
package com .networknt .schema ;
18
18
19
19
import com .fasterxml .jackson .databind .JsonNode ;
20
+ import org .apache .commons .lang3 .StringUtils ;
20
21
import org .slf4j .Logger ;
21
22
import org .slf4j .LoggerFactory ;
22
23
@@ -42,9 +43,30 @@ public AnyOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
42
43
public Set <ValidationMessage > validate (JsonNode node , JsonNode rootNode , String at ) {
43
44
debug (logger , node , rootNode , at );
44
45
46
+ String typeValidatorName = "anyOf/type" ;
47
+ JsonType nodeType = TypeFactory .getValueNodeType (node );
48
+ //If schema has type validator and it doesn't match with node type then ignore it
49
+ List <JsonSchema > filteredSchemaList = new ArrayList <JsonSchema >();
50
+ List <String > expectedTypeList = new ArrayList <String >();
51
+ for (JsonSchema schema : schemas ) {
52
+ if (schema .validators .containsKey (typeValidatorName )) {
53
+ JsonType schemaType = ((TypeValidator ) schema .validators .get (typeValidatorName )).getSchemaType ();
54
+ if (schemaType == nodeType ) {
55
+ filteredSchemaList .add (schema );
56
+ }
57
+ expectedTypeList .add (schemaType .toString ());
58
+ }
59
+ else {
60
+ filteredSchemaList .add (schema );
61
+ }
62
+ }
63
+ if (!schemas .isEmpty () && filteredSchemaList .isEmpty ()) {
64
+ return Collections .singleton (buildValidationMessage (at , StringUtils .join (expectedTypeList )));
65
+ }
66
+
45
67
Set <ValidationMessage > allErrors = new LinkedHashSet <ValidationMessage >();
46
68
47
- for (JsonSchema schema : schemas ) {
69
+ for (JsonSchema schema : filteredSchemaList ) {
48
70
Set <ValidationMessage > errors = schema .validate (node , rootNode , at );
49
71
if (errors .isEmpty ()) {
50
72
return errors ;
0 commit comments