1717package com .networknt .schema ;
1818
1919import com .fasterxml .jackson .databind .JsonNode ;
20+ import org .apache .commons .lang3 .StringUtils ;
2021import org .slf4j .Logger ;
2122import org .slf4j .LoggerFactory ;
2223
@@ -42,9 +43,30 @@ public AnyOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
4243 public Set <ValidationMessage > validate (JsonNode node , JsonNode rootNode , String at ) {
4344 debug (logger , node , rootNode , at );
4445
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+
4567 Set <ValidationMessage > allErrors = new LinkedHashSet <ValidationMessage >();
4668
47- for (JsonSchema schema : schemas ) {
69+ for (JsonSchema schema : filteredSchemaList ) {
4870 Set <ValidationMessage > errors = schema .validate (node , rootNode , at );
4971 if (errors .isEmpty ()) {
5072 return errors ;
0 commit comments