Skip to content

Commit

Permalink
Improved handling for unknown keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
Klas Kalaß committed Nov 22, 2017
1 parent 79acd4d commit e4a7af3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/main/java/com/networknt/schema/JsonMetaSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand All @@ -36,7 +38,7 @@ public class JsonMetaSchema {

private static final Logger logger = LoggerFactory
.getLogger(JsonMetaSchema.class);

private static Map<String, String> UNKNOWN_KEYWORDS = new ConcurrentHashMap<String, String>();

private static class DraftV4 {
private static String URI = "http://json-schema.org/draft-04/schema#";
Expand Down Expand Up @@ -76,6 +78,15 @@ public static JsonMetaSchema getInstance() {
.idKeyword(DRAFT_4_ID)
.addFormats(BUILTIN_FORMATS)
.addKeywords(ValidatorTypeCode.getNonFormatKeywords())
// keywords that may validly exist, but have no validation aspect to them
.addKeywords(Arrays.asList(
new NonValidationKeyword("$schema"),
new NonValidationKeyword("id"),
new NonValidationKeyword("title"),
new NonValidationKeyword("description"),
new NonValidationKeyword("default"),
new NonValidationKeyword("definitions")
))
.build();
}
}
Expand Down Expand Up @@ -221,7 +232,10 @@ public Optional<JsonValidator> newValidator(ValidationContext validationContext,
try {
Keyword kw = keywords.get(keyword);
if (kw == null) {
logger.warn("Unknown keyword " + keyword);
if (!UNKNOWN_KEYWORDS.containsKey(keyword)) {
UNKNOWN_KEYWORDS.put(keyword, keyword);
logger.warn("Unknown keyword " + keyword + " - you should define your own Meta Schema. If the keyword is irrelevant for validation, just use a NonValidationKeyword");
}
return Optional.empty();
}
return Optional.of(kw.newValidator(schemaPath, schemaNode, parentSchema, validationContext));
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/networknt/schema/JsonSchemaFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,11 @@ private boolean idMatchesSourceUrl(JsonMetaSchema metaSchema, JsonNode schema, U
if (id == null || id.isEmpty()) {
return false;
}
logger.info("Matching " + id + " to " + schemaUrl.toString());
return id.equals(schemaUrl.toString());
boolean result = id.equals(schemaUrl.toString());
if (logger.isDebugEnabled()) {
logger.debug("Matching " + id + " to " + schemaUrl.toString() + ": " + result);
}
return result;

}

Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/networknt/schema/NonValidationKeyword.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.networknt.schema;

import java.util.Collections;
import java.util.Set;

import com.fasterxml.jackson.databind.JsonNode;

/**
* Used for Keywords that have no validation aspect, but are part of the metaschema.
*/
public class NonValidationKeyword extends AbstractKeyword {

private static final class Validator extends AbstractJsonValidator {
private Validator(String keyword) {
super(keyword);
}

@Override
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
return Collections.emptySet();
}
}

public NonValidationKeyword(String keyword) {
super(keyword);
}

@Override
public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
ValidationContext validationContext) throws JsonSchemaException, Exception {
return new Validator(getValue());
}
}

0 comments on commit e4a7af3

Please sign in to comment.