Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3222389
Merge pull request #5 from networknt/master
prashanthjos Oct 21, 2020
1f92471
Merge pull request #6 from networknt/master
prashanthjos Dec 22, 2020
00a643d
Fixing walk issues for properties
prashanth-chaitanya Dec 22, 2020
161f59c
Correcting walk changes for few validators
prashanth-chaitanya Dec 24, 2020
32e8ab8
adding item walk listener
prashanth-chaitanya Dec 28, 2020
3e2e7cd
walk listener changes
prashanth-chaitanya Jan 13, 2021
51e45ba
walk listener changes
prashanth-chaitanya Jan 13, 2021
49f3300
Merging latest changes from networknt
prashanth-chaitanya Jan 15, 2021
67d36bf
Merge branch 'networknt-master'
prashanth-chaitanya Jan 15, 2021
9fdb3d0
merging changes from networknt
prashanth-chaitanya Feb 26, 2021
c27e1c9
merging changes from networknt
prashanth-chaitanya Feb 26, 2021
040d752
merging changes from networknt
prashanth-chaitanya Feb 26, 2021
4486cc4
Fixing issues with ValidatorState
prashanth-chaitanya Mar 12, 2021
cfbdf1c
changing the method name to getCollectorContext
prashanth-chaitanya Mar 15, 2021
d125abd
correcting the variable names and ValidatorState logic
prashanth-chaitanya Mar 15, 2021
abd45a8
correcting the documentation logic
prashanth-chaitanya Mar 15, 2021
2457a57
Merge branch 'master' of https://github.com/networknt/json-schema-val…
prashanth-chaitanya Mar 15, 2021
f0ca7d4
latest changes from networknt
prashanth-chaitanya Mar 15, 2021
34b8cf7
Fixing compilation issues in TypeFactoryTest
prashanth-chaitanya Mar 15, 2021
633c49b
Merge branch 'master' into master
prashanthjos Mar 17, 2021
beb6569
Fixing the minor brace issue
prashanth-chaitanya Mar 17, 2021
33d0deb
Merge pull request #10 from networknt/master
prashanthjos Mar 18, 2021
3a25c0b
Merge branch 'master' of https://github.com/networknt/json-schema-val…
prashanth-chaitanya Mar 18, 2021
895152c
Merge branch 'master' into networknt-master
prashanth-chaitanya Mar 18, 2021
bc374e5
Merge branch 'networknt-master'
prashanth-chaitanya Mar 18, 2021
a2f39a7
Merge branch 'networknt:master' into master
prashanthjos Aug 27, 2021
6a80389
Merge branch 'master' of https://github.com/prashanthjos/json-schema-…
prashanth-chaitanya Aug 27, 2021
5b59c56
Fixing the issue of propogating walk-listeners to child 's from paren…
prashanth-chaitanya Sep 2, 2021
d87b8ac
Adding tests for changing listeners
prashanth-chaitanya Sep 2, 2021
8b174df
Merge branch 'networknt:master' into master
prashanthjos Sep 27, 2021
833ffcd
Correcting the ref listeners config in walk event
prashanth-chaitanya Oct 4, 2021
1f37f0d
Correcting the ref listeners config in walk event
prashanth-chaitanya Oct 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/com/networknt/schema/ItemsValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,16 @@ private void doWalk(HashSet<ValidationMessage> validationMessages, int i, JsonNo
}

private void walkSchema(JsonSchema walkSchema, JsonNode node, JsonNode rootNode, String at,
boolean shouldValidateSchema, Set<ValidationMessage> validationMessages) {
boolean shouldValidateSchema, Set<ValidationMessage> 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);

}

Expand Down
71 changes: 42 additions & 29 deletions src/main/java/com/networknt/schema/JsonSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()));
}
}
}
Expand Down Expand Up @@ -242,7 +242,7 @@ private Map<String, JsonValidator> 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;
Expand All @@ -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");
Expand Down Expand Up @@ -291,9 +291,9 @@ public Set<ValidationMessage> 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);
}
}
}
Expand All @@ -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<ValidationMessage> 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;
Expand All @@ -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<ValidationMessage> 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;
Expand All @@ -366,26 +377,28 @@ public Set<ValidationMessage> 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;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/networknt/schema/PropertiesValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ private void walkSchema(Map.Entry<String, JsonSchema> 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(
propertySchema.walk(propertyNode, rootNode, at + "." + entry.getKey(), shouldValidateSchema));
}
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);

}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/networknt/schema/SchemaValidatorsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public class SchemaValidatorsConfig {

private CollectorContext collectorContext;

private boolean loadCollectors = true;

public boolean isTypeLoose() {
return typeLoose;
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,48 @@
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;
import java.util.Set;

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<JsonSchemaWalkListener> 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<JsonSchemaWalkListener> 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<JsonSchemaWalkListener> walkListeners, WalkEvent walkEvent,
Set<ValidationMessage> validationMessages) {
if (walkListeners != null) {
for (JsonSchemaWalkListener walkListener : walkListeners) {
walkListener.onWalkEnd(walkEvent, validationMessages);
}
}
}
protected void runPostWalkListeners(List<JsonSchemaWalkListener> walkListeners, WalkEvent walkEvent,
Set<ValidationMessage> validationMessages) {
if (walkListeners != null) {
for (JsonSchemaWalkListener walkListener : walkListeners) {
walkListener.onWalkEnd(walkEvent, validationMessages);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,36 @@
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;
import java.util.Set;

public class DefaultItemWalkListenerRunner extends AbstractWalkListenerRunner {

private List<JsonSchemaWalkListener> itemWalkListeners;

public DefaultItemWalkListenerRunner(List<JsonSchemaWalkListener> 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<ValidationMessage> validationMessages) {
WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema,
currentJsonSchemaFactory);
runPostWalkListeners(itemWalkListeners, walkEvent, validationMessages);
}
private List<JsonSchemaWalkListener> itemWalkListeners;

public DefaultItemWalkListenerRunner(List<JsonSchemaWalkListener> 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<ValidationMessage> validationMessages) {
WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema,
validationContext, currentJsonSchemaFactory);
runPostWalkListeners(itemWalkListeners, walkEvent, validationMessages);
}

}
Loading