diff --git a/src/main/java/com/networknt/schema/ItemsValidator.java b/src/main/java/com/networknt/schema/ItemsValidator.java index 3a81f5607..69abc8bab 100644 --- a/src/main/java/com/networknt/schema/ItemsValidator.java +++ b/src/main/java/com/networknt/schema/ItemsValidator.java @@ -153,16 +153,16 @@ private void doWalk(HashSet validationMessages, int i, JsonNo } private void walkSchema(JsonSchema walkSchema, JsonNode node, JsonNode rootNode, String at, - boolean shouldValidateSchema, Set validationMessages) { + boolean shouldValidateSchema, Set validationMessages) { boolean executeWalk = arrayItemWalkListenerRunner.runPreWalkListeners(ValidatorTypeCode.ITEMS.getValue(), node, rootNode, at, walkSchema.getSchemaPath(), walkSchema.getSchemaNode(), walkSchema.getParentSchema(), - validationContext.getJsonSchemaFactory()); + validationContext, validationContext.getJsonSchemaFactory()); if (executeWalk) { validationMessages.addAll(walkSchema.walk(node, rootNode, at, shouldValidateSchema)); } arrayItemWalkListenerRunner.runPostWalkListeners(ValidatorTypeCode.ITEMS.getValue(), node, rootNode, at, walkSchema.getSchemaPath(), walkSchema.getSchemaNode(), walkSchema.getParentSchema(), - validationContext.getJsonSchemaFactory(), validationMessages); + validationContext, validationContext.getJsonSchemaFactory(), validationMessages); } diff --git a/src/main/java/com/networknt/schema/JsonSchema.java b/src/main/java/com/networknt/schema/JsonSchema.java index 6799e4d40..a4de67329 100644 --- a/src/main/java/com/networknt/schema/JsonSchema.java +++ b/src/main/java/com/networknt/schema/JsonSchema.java @@ -80,7 +80,7 @@ public JsonSchema(ValidationContext validationContext, URI baseUri, JsonNode sch private JsonSchema(ValidationContext validationContext, String schemaPath, URI currentUri, JsonNode schemaNode, JsonSchema parent, boolean suppressSubSchemaRetrieval) { super(schemaPath, schemaNode, parent, null, suppressSubSchemaRetrieval, - validationContext.getConfig() != null && validationContext.getConfig().isFailFast()); + validationContext.getConfig() != null && validationContext.getConfig().isFailFast()); this.validationContext = validationContext; this.idKeyword = validationContext.getMetaSchema().getIdKeyword(); this.currentUri = this.combineCurrentUriWithIds(currentUri, schemaNode); @@ -109,9 +109,9 @@ private URI combineCurrentUriWithIds(URI currentUri, JsonNode schemaNode) { return this.validationContext.getURIFactory().create(currentUri, id); } catch (IllegalArgumentException e) { throw new JsonSchemaException(ValidationMessage.of(ValidatorTypeCode.ID.getValue(), - ValidatorTypeCode.ID, - id, - currentUri == null ? "null" : currentUri.toString())); + ValidatorTypeCode.ID, + id, + currentUri == null ? "null" : currentUri.toString())); } } } @@ -242,7 +242,7 @@ private Map read(JsonNode schemaNode) { private String getCustomMessage(JsonNode schemaNode, String pname) { final JsonSchema parentSchema = getParentSchema(); final JsonNode message = getMessageNode(schemaNode, parentSchema); - if(message != null && message.get(pname) != null) { + if (message != null && message.get(pname) != null) { return message.get(pname).asText(); } return null; @@ -251,7 +251,7 @@ private String getCustomMessage(JsonNode schemaNode, String pname) { private JsonNode getMessageNode(JsonNode schemaNode, JsonSchema parentSchema) { JsonNode nodeContainingMessage; if (parentSchema == null) - nodeContainingMessage = schemaNode; + nodeContainingMessage = schemaNode; else nodeContainingMessage = parentSchema.schemaNode; return nodeContainingMessage.get("message"); @@ -291,9 +291,9 @@ public Set validate(JsonNode jsonNode, JsonNode rootNode, Str final JsonNode discriminatorNode = jsonNode.get(discriminatorPropertyName); final String discriminatorPropertyValue = discriminatorNode == null ? null : discriminatorNode.asText(); checkDiscriminatorMatch(discriminatorContext, - discriminatorToUse, - discriminatorPropertyValue, - this); + discriminatorToUse, + discriminatorPropertyValue, + this); } } } @@ -316,12 +316,17 @@ public ValidationResult validateAndCollect(JsonNode node) { */ protected ValidationResult validateAndCollect(JsonNode jsonNode, JsonNode rootNode, String at) { try { + // Get the config. + SchemaValidatorsConfig config = validationContext.getConfig(); // Get the collector context from the thread local. CollectorContext collectorContext = getCollectorContext(); // Valdiate. Set errors = validate(jsonNode, rootNode, at); - // Load all the data from collectors into the context. - collectorContext.loadCollectors(); + // When walk is called in series of nested call we don't want to load the collectors every time. Leave to the API to decide when to call collectors. + if (config.doLoadCollectors()) { + // Load all the data from collectors into the context. + collectorContext.loadCollectors(); + } // Collect errors and collector context into validation result. ValidationResult validationResult = new ValidationResult(errors, collectorContext); return validationResult; @@ -336,19 +341,25 @@ protected ValidationResult validateAndCollect(JsonNode jsonNode, JsonNode rootNo /** * Walk the JSON node - * @param node JsonNode + * + * @param node JsonNode * @param shouldValidateSchema indicator on validation * @return result of ValidationResult */ public ValidationResult walk(JsonNode node, boolean shouldValidateSchema) { + // Get the config. + SchemaValidatorsConfig config = validationContext.getConfig(); // Get the collector context from the thread local. CollectorContext collectorContext = getCollectorContext(); // Set the walkEnabled flag in internal validator state. setValidatorState(true, shouldValidateSchema); // Walk through the schema. Set errors = walk(node, node, AT_ROOT, shouldValidateSchema); - // Load all the data from collectors into the context. - collectorContext.loadCollectors(); + // When walk is called in series of nested call we don't want to load the collectors every time. Leave to the API to decide when to call collectors. + if (config.doLoadCollectors()) { + // Load all the data from collectors into the context. + collectorContext.loadCollectors(); + } // Collect errors and collector context into validation result. ValidationResult validationResult = new ValidationResult(errors, collectorContext); return validationResult; @@ -366,26 +377,28 @@ public Set walk(JsonNode node, JsonNode rootNode, String at, // Call all the pre-walk listeners. If atleast one of the pre walk listeners // returns SKIP, then skip the walk. if (keywordWalkListenerRunner.runPreWalkListeners(schemaPathWithKeyword, - node, - rootNode, - at, - schemaPath, - schemaNode, - parentSchema, - validationContext.getJsonSchemaFactory())) { + node, + rootNode, + at, + schemaPath, + schemaNode, + parentSchema, + validationContext, + validationContext.getJsonSchemaFactory())) { validationMessages.addAll(jsonWalker.walk(node, rootNode, at, shouldValidateSchema)); } } finally { // Call all the post-walk listeners. keywordWalkListenerRunner.runPostWalkListeners(schemaPathWithKeyword, - node, - rootNode, - at, - schemaPath, - schemaNode, - parentSchema, - validationContext.getJsonSchemaFactory(), - validationMessages); + node, + rootNode, + at, + schemaPath, + schemaNode, + parentSchema, + validationContext, + validationContext.getJsonSchemaFactory(), + validationMessages); } } return validationMessages; diff --git a/src/main/java/com/networknt/schema/PropertiesValidator.java b/src/main/java/com/networknt/schema/PropertiesValidator.java index 17c201664..b0e1154a0 100644 --- a/src/main/java/com/networknt/schema/PropertiesValidator.java +++ b/src/main/java/com/networknt/schema/PropertiesValidator.java @@ -119,7 +119,7 @@ private void walkSchema(Map.Entry entry, JsonNode node, Json JsonNode propertyNode = (node == null ? null : node.get(entry.getKey())); boolean executeWalk = propertyWalkListenerRunner.runPreWalkListeners(ValidatorTypeCode.PROPERTIES.getValue(), propertyNode, rootNode, at + "." + entry.getKey(), propertySchema.getSchemaPath(), - propertySchema.getSchemaNode(), propertySchema.getParentSchema(), + propertySchema.getSchemaNode(), propertySchema.getParentSchema(), validationContext, validationContext.getJsonSchemaFactory()); if (executeWalk) { validationMessages.addAll( @@ -127,7 +127,7 @@ private void walkSchema(Map.Entry entry, JsonNode node, Json } propertyWalkListenerRunner.runPostWalkListeners(ValidatorTypeCode.PROPERTIES.getValue(), propertyNode, rootNode, at + "." + entry.getKey(), propertySchema.getSchemaPath(), propertySchema.getSchemaNode(), - propertySchema.getParentSchema(), validationContext.getJsonSchemaFactory(), validationMessages); + propertySchema.getParentSchema(), validationContext, validationContext.getJsonSchemaFactory(), validationMessages); } diff --git a/src/main/java/com/networknt/schema/SchemaValidatorsConfig.java b/src/main/java/com/networknt/schema/SchemaValidatorsConfig.java index 6d96cad34..bb935ee35 100644 --- a/src/main/java/com/networknt/schema/SchemaValidatorsConfig.java +++ b/src/main/java/com/networknt/schema/SchemaValidatorsConfig.java @@ -87,6 +87,8 @@ public class SchemaValidatorsConfig { private CollectorContext collectorContext; + private boolean loadCollectors = true; + public boolean isTypeLoose() { return typeLoose; } @@ -257,4 +259,12 @@ public boolean isOpenAPI3StyleDiscriminators() { public void setOpenAPI3StyleDiscriminators(boolean openAPI3StyleDiscriminators) { this.openAPI3StyleDiscriminators = openAPI3StyleDiscriminators; } + + public void setLoadCollectors(boolean loadCollectors) { + this.loadCollectors = loadCollectors; + } + + public boolean doLoadCollectors() { + return loadCollectors; + } } diff --git a/src/main/java/com/networknt/schema/walk/AbstractWalkListenerRunner.java b/src/main/java/com/networknt/schema/walk/AbstractWalkListenerRunner.java index e21db44b0..9d0a94337 100644 --- a/src/main/java/com/networknt/schema/walk/AbstractWalkListenerRunner.java +++ b/src/main/java/com/networknt/schema/walk/AbstractWalkListenerRunner.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.networknt.schema.JsonSchema; import com.networknt.schema.JsonSchemaFactory; +import com.networknt.schema.ValidationContext; import com.networknt.schema.ValidationMessage; import java.util.List; @@ -10,40 +11,40 @@ public abstract class AbstractWalkListenerRunner implements WalkListenerRunner { - protected String getKeywordName(String keyWordPath) { - return keyWordPath.substring(keyWordPath.lastIndexOf('/') + 1); - } + protected String getKeywordName(String keyWordPath) { + return keyWordPath.substring(keyWordPath.lastIndexOf('/') + 1); + } - protected WalkEvent constructWalkEvent(String keyWordName, JsonNode node, JsonNode rootNode, String at, - String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, - JsonSchemaFactory currentJsonSchemaFactory) { - return WalkEvent.builder().at(at).keyWordName(keyWordName).node(node).parentSchema(parentSchema) - .rootNode(rootNode).schemaNode(schemaNode).schemaPath(schemaPath) - .currentJsonSchemaFactory(currentJsonSchemaFactory).build(); - } + protected WalkEvent constructWalkEvent(String keyWordName, JsonNode node, JsonNode rootNode, String at, + String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, + JsonSchemaFactory currentJsonSchemaFactory) { + return WalkEvent.builder().at(at).keyWordName(keyWordName).node(node).parentSchema(parentSchema) + .rootNode(rootNode).schemaNode(schemaNode).schemaPath(schemaPath) + .currentJsonSchemaFactory(currentJsonSchemaFactory).validationContext(validationContext).build(); + } - protected boolean runPreWalkListeners(List walkListeners, WalkEvent walkEvent) { - boolean continueToWalkMethod = true; - if (walkListeners != null) { - for (JsonSchemaWalkListener walkListener : walkListeners) { - WalkFlow walkFlow = walkListener.onWalkStart(walkEvent); - if (WalkFlow.SKIP.equals(walkFlow) || WalkFlow.ABORT.equals(walkFlow)) { - continueToWalkMethod = false; - if (WalkFlow.ABORT.equals(walkFlow)) { - break; - } - } - } - } - return continueToWalkMethod; - } + protected boolean runPreWalkListeners(List walkListeners, WalkEvent walkEvent) { + boolean continueToWalkMethod = true; + if (walkListeners != null) { + for (JsonSchemaWalkListener walkListener : walkListeners) { + WalkFlow walkFlow = walkListener.onWalkStart(walkEvent); + if (WalkFlow.SKIP.equals(walkFlow) || WalkFlow.ABORT.equals(walkFlow)) { + continueToWalkMethod = false; + if (WalkFlow.ABORT.equals(walkFlow)) { + break; + } + } + } + } + return continueToWalkMethod; + } - protected void runPostWalkListeners(List walkListeners, WalkEvent walkEvent, - Set validationMessages) { - if (walkListeners != null) { - for (JsonSchemaWalkListener walkListener : walkListeners) { - walkListener.onWalkEnd(walkEvent, validationMessages); - } - } - } + protected void runPostWalkListeners(List walkListeners, WalkEvent walkEvent, + Set validationMessages) { + if (walkListeners != null) { + for (JsonSchemaWalkListener walkListener : walkListeners) { + walkListener.onWalkEnd(walkEvent, validationMessages); + } + } + } } diff --git a/src/main/java/com/networknt/schema/walk/DefaultItemWalkListenerRunner.java b/src/main/java/com/networknt/schema/walk/DefaultItemWalkListenerRunner.java index bc5df0577..76b33ef26 100644 --- a/src/main/java/com/networknt/schema/walk/DefaultItemWalkListenerRunner.java +++ b/src/main/java/com/networknt/schema/walk/DefaultItemWalkListenerRunner.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.networknt.schema.JsonSchema; import com.networknt.schema.JsonSchemaFactory; +import com.networknt.schema.ValidationContext; import com.networknt.schema.ValidationMessage; import java.util.List; @@ -10,28 +11,28 @@ public class DefaultItemWalkListenerRunner extends AbstractWalkListenerRunner { - private List itemWalkListeners; - - public DefaultItemWalkListenerRunner(List itemWalkListeners) { - this.itemWalkListeners = itemWalkListeners; - } - - @Override - public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, - String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, - JsonSchemaFactory currentJsonSchemaFactory) { - WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema, - currentJsonSchemaFactory); - return runPreWalkListeners(itemWalkListeners, walkEvent); - } - - @Override - public void runPostWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath, - JsonNode schemaNode, JsonSchema parentSchema, JsonSchemaFactory currentJsonSchemaFactory, - Set validationMessages) { - WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema, - currentJsonSchemaFactory); - runPostWalkListeners(itemWalkListeners, walkEvent, validationMessages); - } + private List itemWalkListeners; + + public DefaultItemWalkListenerRunner(List itemWalkListeners) { + this.itemWalkListeners = itemWalkListeners; + } + + @Override + public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, + String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, + JsonSchemaFactory currentJsonSchemaFactory) { + WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema, validationContext, + currentJsonSchemaFactory); + return runPreWalkListeners(itemWalkListeners, walkEvent); + } + + @Override + public void runPostWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath, + JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, JsonSchemaFactory currentJsonSchemaFactory, + Set validationMessages) { + WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema, + validationContext, currentJsonSchemaFactory); + runPostWalkListeners(itemWalkListeners, walkEvent, validationMessages); + } } \ No newline at end of file diff --git a/src/main/java/com/networknt/schema/walk/DefaultKeywordWalkListenerRunner.java b/src/main/java/com/networknt/schema/walk/DefaultKeywordWalkListenerRunner.java index 103e5ff6a..79e7c8896 100644 --- a/src/main/java/com/networknt/schema/walk/DefaultKeywordWalkListenerRunner.java +++ b/src/main/java/com/networknt/schema/walk/DefaultKeywordWalkListenerRunner.java @@ -1,10 +1,7 @@ package com.networknt.schema.walk; import com.fasterxml.jackson.databind.JsonNode; -import com.networknt.schema.JsonSchema; -import com.networknt.schema.JsonSchemaFactory; -import com.networknt.schema.SchemaValidatorsConfig; -import com.networknt.schema.ValidationMessage; +import com.networknt.schema.*; import java.util.List; import java.util.Map; @@ -12,46 +9,49 @@ public class DefaultKeywordWalkListenerRunner extends AbstractWalkListenerRunner { - private Map> keywordWalkListenersMap; - - public DefaultKeywordWalkListenerRunner(Map> keywordWalkListenersMap) { - this.keywordWalkListenersMap = keywordWalkListenersMap; - } - - @Override - public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, - String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, - JsonSchemaFactory currentJsonSchemaFactory) { - String keyword = getKeywordName(keyWordPath); - boolean continueRunningListenersAndWalk = true; - WalkEvent keywordWalkEvent = constructWalkEvent(keyword, node, rootNode, at, schemaPath, schemaNode, - parentSchema, currentJsonSchemaFactory); - // Run Listeners that are setup only for this keyword. - List currentKeywordListeners = keywordWalkListenersMap.get(keyword); - continueRunningListenersAndWalk = runPreWalkListeners(currentKeywordListeners, keywordWalkEvent); - if (continueRunningListenersAndWalk) { - // Run Listeners that are setup for all keywords. - List allKeywordListeners = keywordWalkListenersMap - .get(SchemaValidatorsConfig.ALL_KEYWORD_WALK_LISTENER_KEY); - runPreWalkListeners(allKeywordListeners, keywordWalkEvent); - } - return continueRunningListenersAndWalk; - } - - @Override - public void runPostWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath, - JsonNode schemaNode, JsonSchema parentSchema, JsonSchemaFactory currentJsonSchemaFactory, - Set validationMessages) { - String keyword = getKeywordName(keyWordPath); - WalkEvent keywordWalkEvent = constructWalkEvent(keyword, node, rootNode, at, schemaPath, schemaNode, - parentSchema, currentJsonSchemaFactory); - // Run Listeners that are setup only for this keyword. - List currentKeywordListeners = keywordWalkListenersMap.get(keyword); - runPostWalkListeners(currentKeywordListeners, keywordWalkEvent, validationMessages); - // Run Listeners that are setup for all keywords. - List allKeywordListeners = keywordWalkListenersMap - .get(SchemaValidatorsConfig.ALL_KEYWORD_WALK_LISTENER_KEY); - runPostWalkListeners(allKeywordListeners, keywordWalkEvent, validationMessages); - } + private Map> keywordWalkListenersMap; + + public DefaultKeywordWalkListenerRunner(Map> keywordWalkListenersMap) { + this.keywordWalkListenersMap = keywordWalkListenersMap; + } + + @Override + public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, + String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, + ValidationContext validationContext, + JsonSchemaFactory currentJsonSchemaFactory) { + String keyword = getKeywordName(keyWordPath); + boolean continueRunningListenersAndWalk = true; + WalkEvent keywordWalkEvent = constructWalkEvent(keyword, node, rootNode, at, schemaPath, schemaNode, + parentSchema, validationContext, currentJsonSchemaFactory); + // Run Listeners that are setup only for this keyword. + List currentKeywordListeners = keywordWalkListenersMap.get(keyword); + continueRunningListenersAndWalk = runPreWalkListeners(currentKeywordListeners, keywordWalkEvent); + if (continueRunningListenersAndWalk) { + // Run Listeners that are setup for all keywords. + List allKeywordListeners = keywordWalkListenersMap + .get(SchemaValidatorsConfig.ALL_KEYWORD_WALK_LISTENER_KEY); + runPreWalkListeners(allKeywordListeners, keywordWalkEvent); + } + return continueRunningListenersAndWalk; + } + + @Override + public void runPostWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath, + JsonNode schemaNode, JsonSchema parentSchema, + ValidationContext validationContext, + JsonSchemaFactory currentJsonSchemaFactory, + Set validationMessages) { + String keyword = getKeywordName(keyWordPath); + WalkEvent keywordWalkEvent = constructWalkEvent(keyword, node, rootNode, at, schemaPath, schemaNode, + parentSchema, validationContext, currentJsonSchemaFactory); + // Run Listeners that are setup only for this keyword. + List currentKeywordListeners = keywordWalkListenersMap.get(keyword); + runPostWalkListeners(currentKeywordListeners, keywordWalkEvent, validationMessages); + // Run Listeners that are setup for all keywords. + List allKeywordListeners = keywordWalkListenersMap + .get(SchemaValidatorsConfig.ALL_KEYWORD_WALK_LISTENER_KEY); + runPostWalkListeners(allKeywordListeners, keywordWalkEvent, validationMessages); + } } diff --git a/src/main/java/com/networknt/schema/walk/DefaultPropertyWalkListenerRunner.java b/src/main/java/com/networknt/schema/walk/DefaultPropertyWalkListenerRunner.java index 64006d6fc..58b78cb3f 100644 --- a/src/main/java/com/networknt/schema/walk/DefaultPropertyWalkListenerRunner.java +++ b/src/main/java/com/networknt/schema/walk/DefaultPropertyWalkListenerRunner.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.networknt.schema.JsonSchema; import com.networknt.schema.JsonSchemaFactory; +import com.networknt.schema.ValidationContext; import com.networknt.schema.ValidationMessage; import java.util.List; @@ -10,29 +11,29 @@ public class DefaultPropertyWalkListenerRunner extends AbstractWalkListenerRunner { - private List propertyWalkListeners; - - public DefaultPropertyWalkListenerRunner(List propertyWalkListeners) { - this.propertyWalkListeners = propertyWalkListeners; - } - - @Override - public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, - String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, - JsonSchemaFactory currentJsonSchemaFactory) { - WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema, - currentJsonSchemaFactory); - return runPreWalkListeners(propertyWalkListeners, walkEvent); - } - - @Override - public void runPostWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath, - JsonNode schemaNode, JsonSchema parentSchema, JsonSchemaFactory currentJsonSchemaFactory, - Set validationMessages) { - WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema, - currentJsonSchemaFactory); - runPostWalkListeners(propertyWalkListeners, walkEvent, validationMessages); - - } + private List propertyWalkListeners; + + public DefaultPropertyWalkListenerRunner(List propertyWalkListeners) { + this.propertyWalkListeners = propertyWalkListeners; + } + + @Override + public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, + String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, + JsonSchemaFactory currentJsonSchemaFactory) { + WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema, + validationContext, currentJsonSchemaFactory); + return runPreWalkListeners(propertyWalkListeners, walkEvent); + } + + @Override + public void runPostWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath, + JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, JsonSchemaFactory currentJsonSchemaFactory, + Set validationMessages) { + WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema, + validationContext, currentJsonSchemaFactory); + runPostWalkListeners(propertyWalkListeners, walkEvent, validationMessages); + + } } diff --git a/src/main/java/com/networknt/schema/walk/WalkEvent.java b/src/main/java/com/networknt/schema/walk/WalkEvent.java index cd950d6a7..6543e1591 100644 --- a/src/main/java/com/networknt/schema/walk/WalkEvent.java +++ b/src/main/java/com/networknt/schema/walk/WalkEvent.java @@ -3,116 +3,131 @@ import com.fasterxml.jackson.databind.JsonNode; import com.networknt.schema.JsonSchema; import com.networknt.schema.JsonSchemaFactory; +import com.networknt.schema.SchemaValidatorsConfig; +import com.networknt.schema.ValidationContext; import java.net.URI; /** - * * Encapsulation of Walk data that is passed into the {@link JsonSchemaWalkListener}. - * */ public class WalkEvent { - private String schemaPath; - private JsonNode schemaNode; - private JsonSchema parentSchema; - private String keyWordName; - private JsonNode node; - private JsonNode rootNode; - private String at; - private JsonSchemaFactory currentJsonSchemaFactory; - - public String getSchemaPath() { - return schemaPath; - } - - public JsonNode getSchemaNode() { - return schemaNode; - } - - public JsonSchema getParentSchema() { - return parentSchema; - } - - public String getKeyWordName() { - return keyWordName; - } - - public JsonNode getNode() { - return node; - } - - public JsonNode getRootNode() { - return rootNode; - } - - public String getAt() { - return at; - } - - public JsonSchema getRefSchema(URI schemaUri) { - return currentJsonSchemaFactory.getSchema(schemaUri); - } - - public JsonSchemaFactory getCurrentJsonSchemaFactory() { - return currentJsonSchemaFactory; - } - - static class WalkEventBuilder { - private WalkEvent keywordWalkEvent = null; - - WalkEventBuilder() { - keywordWalkEvent = new WalkEvent(); - } - - public WalkEventBuilder schemaPath(String schemaPath) { - keywordWalkEvent.schemaPath = schemaPath; - return this; - } - - public WalkEventBuilder schemaNode(JsonNode schemaNode) { - keywordWalkEvent.schemaNode = schemaNode; - return this; - } - - public WalkEventBuilder parentSchema(JsonSchema parentSchema) { - keywordWalkEvent.parentSchema = parentSchema; - return this; - } - - public WalkEventBuilder keyWordName(String keyWordName) { - keywordWalkEvent.keyWordName = keyWordName; - return this; - } - - public WalkEventBuilder node(JsonNode node) { - keywordWalkEvent.node = node; - return this; - } - - public WalkEventBuilder rootNode(JsonNode rootNode) { - keywordWalkEvent.rootNode = rootNode; - return this; - } - - public WalkEventBuilder at(String at) { - keywordWalkEvent.at = at; - return this; - } - - public WalkEventBuilder currentJsonSchemaFactory(JsonSchemaFactory currentJsonSchemaFactory) { - keywordWalkEvent.currentJsonSchemaFactory = currentJsonSchemaFactory; - return this; - } - - public WalkEvent build() { - return keywordWalkEvent; - } - - } - - public static WalkEventBuilder builder() { - return new WalkEventBuilder(); - } + private String schemaPath; + private JsonNode schemaNode; + private JsonSchema parentSchema; + private String keyWordName; + private JsonNode node; + private JsonNode rootNode; + private String at; + private JsonSchemaFactory currentJsonSchemaFactory; + private ValidationContext validationContext; + + public String getSchemaPath() { + return schemaPath; + } + + public JsonNode getSchemaNode() { + return schemaNode; + } + + public JsonSchema getParentSchema() { + return parentSchema; + } + + public String getKeyWordName() { + return keyWordName; + } + + public JsonNode getNode() { + return node; + } + + public JsonNode getRootNode() { + return rootNode; + } + + public String getAt() { + return at; + } + + public JsonSchema getRefSchema(URI schemaUri) { + return currentJsonSchemaFactory.getSchema(schemaUri, validationContext.getConfig()); + } + + public JsonSchema getRefSchema(URI schemaUri, SchemaValidatorsConfig schemaValidatorsConfig) { + if (schemaValidatorsConfig != null) { + return currentJsonSchemaFactory.getSchema(schemaUri, schemaValidatorsConfig); + } else { + return getRefSchema(schemaUri); + } + } + + public JsonSchemaFactory getCurrentJsonSchemaFactory() { + return currentJsonSchemaFactory; + } + + static class WalkEventBuilder { + + private WalkEvent walkEvent; + + WalkEventBuilder() { + walkEvent = new WalkEvent(); + } + + public WalkEventBuilder schemaPath(String schemaPath) { + walkEvent.schemaPath = schemaPath; + return this; + } + + public WalkEventBuilder schemaNode(JsonNode schemaNode) { + walkEvent.schemaNode = schemaNode; + return this; + } + + public WalkEventBuilder parentSchema(JsonSchema parentSchema) { + walkEvent.parentSchema = parentSchema; + return this; + } + + public WalkEventBuilder keyWordName(String keyWordName) { + walkEvent.keyWordName = keyWordName; + return this; + } + + public WalkEventBuilder node(JsonNode node) { + walkEvent.node = node; + return this; + } + + public WalkEventBuilder rootNode(JsonNode rootNode) { + walkEvent.rootNode = rootNode; + return this; + } + + public WalkEventBuilder at(String at) { + walkEvent.at = at; + return this; + } + + public WalkEventBuilder currentJsonSchemaFactory(JsonSchemaFactory currentJsonSchemaFactory) { + walkEvent.currentJsonSchemaFactory = currentJsonSchemaFactory; + return this; + } + + public WalkEventBuilder validationContext(ValidationContext validationContext) { + walkEvent.validationContext = validationContext; + return this; + } + + public WalkEvent build() { + return walkEvent; + } + + } + + public static WalkEventBuilder builder() { + return new WalkEventBuilder(); + } } diff --git a/src/main/java/com/networknt/schema/walk/WalkListenerRunner.java b/src/main/java/com/networknt/schema/walk/WalkListenerRunner.java index 089cf2bf8..5eb331a61 100644 --- a/src/main/java/com/networknt/schema/walk/WalkListenerRunner.java +++ b/src/main/java/com/networknt/schema/walk/WalkListenerRunner.java @@ -3,17 +3,18 @@ import com.fasterxml.jackson.databind.JsonNode; import com.networknt.schema.JsonSchema; import com.networknt.schema.JsonSchemaFactory; +import com.networknt.schema.ValidationContext; import com.networknt.schema.ValidationMessage; import java.util.Set; public interface WalkListenerRunner { - public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, - String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, JsonSchemaFactory jsonSchemaFactory); + public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, + String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, JsonSchemaFactory jsonSchemaFactory); - public void runPostWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath, - JsonNode schemaNode, JsonSchema parentSchema, JsonSchemaFactory jsonSchemaFactory, - Set validationMessages); + public void runPostWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath, + JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, JsonSchemaFactory jsonSchemaFactory, + Set validationMessages); }