Skip to content

Commit 1db834b

Browse files
committed
Merge branch 'develop' of github.com:networknt/json-schema-validator into develop
2 parents ccd9212 + 0a97700 commit 1db834b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/main/java/com/networknt/schema/AnyOfValidator.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.networknt.schema;
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
20+
import org.apache.commons.lang3.StringUtils;
2021
import org.slf4j.Logger;
2122
import 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;

src/main/java/com/networknt/schema/TypeValidator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public TypeValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSc
4040
parseErrorCode(getValidatorType().getErrorCodeKey());
4141
}
4242

43+
public JsonType getSchemaType() {
44+
return schemaType;
45+
}
46+
4347
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
4448
debug(logger, node, rootNode, at);
4549

0 commit comments

Comments
 (0)