Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:networknt/json-schema-validator …
Browse files Browse the repository at this point in the history
…into develop
  • Loading branch information
stevehu committed Nov 21, 2018
2 parents ccd9212 + 0a97700 commit 1db834b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/main/java/com/networknt/schema/AnyOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -42,9 +43,30 @@ public AnyOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
debug(logger, node, rootNode, at);

String typeValidatorName = "anyOf/type";
JsonType nodeType = TypeFactory.getValueNodeType(node);
//If schema has type validator and it doesn't match with node type then ignore it
List<JsonSchema> filteredSchemaList = new ArrayList<JsonSchema>();
List<String> expectedTypeList = new ArrayList<String>();
for (JsonSchema schema : schemas) {
if (schema.validators.containsKey(typeValidatorName)) {
JsonType schemaType = ((TypeValidator) schema.validators.get(typeValidatorName)).getSchemaType();
if (schemaType == nodeType) {
filteredSchemaList.add(schema);
}
expectedTypeList.add(schemaType.toString());
}
else {
filteredSchemaList.add(schema);
}
}
if (!schemas.isEmpty() && filteredSchemaList.isEmpty()) {
return Collections.singleton(buildValidationMessage(at, StringUtils.join(expectedTypeList)));
}

Set<ValidationMessage> allErrors = new LinkedHashSet<ValidationMessage>();

for (JsonSchema schema : schemas) {
for (JsonSchema schema : filteredSchemaList) {
Set<ValidationMessage> errors = schema.validate(node, rootNode, at);
if (errors.isEmpty()) {
return errors;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/networknt/schema/TypeValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public TypeValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSc
parseErrorCode(getValidatorType().getErrorCodeKey());
}

public JsonType getSchemaType() {
return schemaType;
}

public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
debug(logger, node, rootNode, at);

Expand Down

0 comments on commit 1db834b

Please # to comment.