Skip to content

Commit e3a5cf7

Browse files
committed
Rename SchemaValidatorsConfig to SchemaRegistryConfig and move from ValidationContext to SchemaRegistry and refactor
1 parent 4a2a289 commit e3a5cf7

File tree

127 files changed

+783
-1112
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+783
-1112
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ default boolean matches(ExecutionContext executionContext, ValidationContext val
9999
* @return true if matches
100100
*/
101101
default boolean matches(ExecutionContext executionContext, ValidationContext validationContext, JsonNode value) {
102-
JsonType nodeType = TypeFactory.getValueNodeType(value, validationContext.getConfig());
102+
JsonType nodeType = TypeFactory.getValueNodeType(value, validationContext.getSchemaRegistryConfig());
103103
if (nodeType != JsonType.STRING) {
104104
return true;
105105
}

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

Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,8 @@ private static Schema obtainSubSchemaNode(final JsonNode schemaNode, final Valid
117117
if (text == null) {
118118
return null;
119119
}
120-
121120
final SchemaLocation schemaLocation = SchemaLocation.of(node.textValue());
122-
123-
return validationContext.getJsonSchemaFactory().getSchema(schemaLocation, validationContext.getConfig());
121+
return validationContext.getSchemaRegistry().getSchema(schemaLocation);
124122
}
125123
public static class JsonNodePathLegacy {
126124
private static final JsonNodePath INSTANCE = new JsonNodePath(PathType.LEGACY);
@@ -153,14 +151,14 @@ public void validate(ExecutionContext executionContext, JsonNode node) {
153151
* @return The path.
154152
*/
155153
protected JsonNodePath atRoot() {
156-
if (this.validationContext.getConfig().getPathType().equals(PathType.JSON_POINTER)) {
154+
if (this.validationContext.getSchemaRegistryConfig().getPathType().equals(PathType.JSON_POINTER)) {
157155
return JsonNodePathJsonPointer.getInstance();
158-
} else if (this.validationContext.getConfig().getPathType().equals(PathType.LEGACY)) {
156+
} else if (this.validationContext.getSchemaRegistryConfig().getPathType().equals(PathType.LEGACY)) {
159157
return JsonNodePathLegacy.getInstance();
160-
} else if (this.validationContext.getConfig().getPathType().equals(PathType.JSON_PATH)) {
158+
} else if (this.validationContext.getSchemaRegistryConfig().getPathType().equals(PathType.JSON_PATH)) {
161159
return JsonNodePathJsonPath.getInstance();
162160
}
163-
return new JsonNodePath(this.validationContext.getConfig().getPathType());
161+
return new JsonNodePath(this.validationContext.getSchemaRegistryConfig().getPathType());
164162
}
165163

166164
static Schema from(ValidationContext validationContext, SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, Schema parent, boolean suppressSubSchemaRetrieval) {
@@ -184,18 +182,18 @@ private static SchemaLocation resolve(SchemaLocation schemaLocation, JsonNode sc
184182
resolve = id.substring(0, fragment);
185183
}
186184
SchemaLocation result = !"".equals(resolve) ? schemaLocation.resolve(resolve) : schemaLocation;
187-
JsonSchemaIdValidator validator = validationContext.getConfig().getSchemaIdValidator();
185+
JsonSchemaIdValidator validator = validationContext.getSchemaRegistryConfig().getSchemaIdValidator();
188186
if (validator != null) {
189187
if (!validator.validate(id, rootSchema, schemaLocation, result, validationContext)) {
190-
SchemaLocation idSchemaLocation = schemaLocation.append(validationContext.getMetaSchema().getIdKeyword());
188+
SchemaLocation idSchemaLocation = schemaLocation.append(validationContext.getDialect().getIdKeyword());
191189
Error error = Error.builder()
192190
.messageKey(ValidatorTypeCode.ID.getValue()).keyword(ValidatorTypeCode.ID.getValue())
193191
.instanceLocation(idSchemaLocation.getFragment())
194-
.arguments(id, validationContext.getMetaSchema().getIdKeyword(), idSchemaLocation)
192+
.arguments(id, validationContext.getDialect().getIdKeyword(), idSchemaLocation)
195193
.schemaLocation(idSchemaLocation)
196194
.schemaNode(schemaNode)
197-
.messageFormatter(args -> validationContext.getConfig().getMessageSource().getMessage(
198-
ValidatorTypeCode.ID.getValue(), validationContext.getConfig().getLocale(), args))
195+
.messageFormatter(args -> validationContext.getSchemaRegistryConfig().getMessageSource().getMessage(
196+
ValidatorTypeCode.ID.getValue(), validationContext.getSchemaRegistryConfig().getLocale(), args))
199197
.build();
200198
throw new InvalidSchemaException(error);
201199
}
@@ -243,15 +241,15 @@ private Schema(ValidationContext validationContext, SchemaLocation schemaLocatio
243241
this.id = null;
244242
}
245243
}
246-
String anchor = this.validationContext.getMetaSchema().readAnchor(this.schemaNode);
244+
String anchor = this.validationContext.getDialect().readAnchor(this.schemaNode);
247245
if (anchor != null) {
248246
String absoluteIri = this.schemaLocation.getAbsoluteIri() != null
249247
? this.schemaLocation.getAbsoluteIri().toString()
250248
: "";
251249
this.validationContext.getSchemaResources()
252250
.putIfAbsent(absoluteIri + "#" + anchor, this);
253251
}
254-
String dynamicAnchor = this.validationContext.getMetaSchema().readDynamicAnchor(schemaNode);
252+
String dynamicAnchor = this.validationContext.getDialect().readDynamicAnchor(schemaNode);
255253
if (dynamicAnchor != null) {
256254
String absoluteIri = this.schemaLocation.getAbsoluteIri() != null
257255
? this.schemaLocation.getAbsoluteIri().toString()
@@ -324,9 +322,8 @@ protected Schema(
324322
* @return the schema
325323
*/
326324
public Schema fromRef(Schema refEvaluationParentSchema, JsonNodePath refEvaluationPath) {
327-
ValidationContext validationContext = new ValidationContext(this.getValidationContext().getMetaSchema(),
328-
this.getValidationContext().getJsonSchemaFactory(),
329-
refEvaluationParentSchema.validationContext.getConfig(),
325+
ValidationContext validationContext = new ValidationContext(this.getValidationContext().getDialect(),
326+
this.getValidationContext().getSchemaRegistry(),
330327
refEvaluationParentSchema.getValidationContext().getSchemaReferences(),
331328
refEvaluationParentSchema.getValidationContext().getSchemaResources(),
332329
refEvaluationParentSchema.getValidationContext().getDynamicAnchors());
@@ -352,43 +349,43 @@ public Schema fromRef(Schema refEvaluationParentSchema, JsonNodePath refEvaluati
352349
evaluationParentSchema, /* errorMessage */ null);
353350
}
354351

355-
public Schema withConfig(SchemaValidatorsConfig config) {
356-
if (this.getValidationContext().getMetaSchema().getKeywords().containsKey("discriminator")
357-
&& !config.isDiscriminatorKeywordEnabled()) {
358-
config = SchemaValidatorsConfig.builder(config)
359-
.discriminatorKeywordEnabled(true)
360-
.nullableKeywordEnabled(true)
361-
.build();
362-
}
363-
if (!this.getValidationContext().getConfig().equals(config)) {
364-
ValidationContext validationContext = new ValidationContext(this.getValidationContext().getMetaSchema(),
365-
this.getValidationContext().getJsonSchemaFactory(), config,
366-
this.getValidationContext().getSchemaReferences(),
367-
this.getValidationContext().getSchemaResources(),
368-
this.getValidationContext().getDynamicAnchors());
369-
boolean validatorsLoaded = false;
370-
TypeValidator typeValidator = null;
371-
List<KeywordValidator> validators = null;
372-
return new Schema(
373-
/* Below from JsonSchema */
374-
validators,
375-
validatorsLoaded,
376-
recursiveAnchor,
377-
typeValidator,
378-
id,
379-
/* Below from BaseJsonValidator */
380-
suppressSubSchemaRetrieval,
381-
schemaNode,
382-
validationContext,
383-
parentSchema,
384-
schemaLocation,
385-
evaluationPath,
386-
evaluationParentSchema,
387-
/* errorMessage */ null);
388-
389-
}
390-
return this;
391-
}
352+
// public Schema withConfig(SchemaValidatorsConfig config) {
353+
// if (this.getValidationContext().getMetaSchema().getKeywords().containsKey("discriminator")
354+
// && !config.isDiscriminatorKeywordEnabled()) {
355+
// config = SchemaValidatorsConfig.builder(config)
356+
// .discriminatorKeywordEnabled(true)
357+
// .nullableKeywordEnabled(true)
358+
// .build();
359+
// }
360+
// if (!this.getValidationContext().getSchemaRegistryConfig().equals(config)) {
361+
// ValidationContext validationContext = new ValidationContext(this.getValidationContext().getMetaSchema(),
362+
// this.getValidationContext().getSchemaRegistry(), config,
363+
// this.getValidationContext().getSchemaReferences(),
364+
// this.getValidationContext().getSchemaResources(),
365+
// this.getValidationContext().getDynamicAnchors());
366+
// boolean validatorsLoaded = false;
367+
// TypeValidator typeValidator = null;
368+
// List<KeywordValidator> validators = null;
369+
// return new Schema(
370+
// /* Below from JsonSchema */
371+
// validators,
372+
// validatorsLoaded,
373+
// recursiveAnchor,
374+
// typeValidator,
375+
// id,
376+
// /* Below from BaseJsonValidator */
377+
// suppressSubSchemaRetrieval,
378+
// schemaNode,
379+
// validationContext,
380+
// parentSchema,
381+
// schemaLocation,
382+
// evaluationPath,
383+
// evaluationParentSchema,
384+
// /* errorMessage */ null);
385+
//
386+
// }
387+
// return this;
388+
// }
392389

393390
public ValidationContext getValidationContext() {
394391
return this.validationContext;
@@ -703,7 +700,7 @@ private long activeDialect() {
703700

704701
@Override
705702
public void validate(ExecutionContext executionContext, JsonNode jsonNode, JsonNode rootNode, JsonNodePath instanceLocation) {
706-
if (this.validationContext.getConfig().isDiscriminatorKeywordEnabled()) {
703+
if (this.validationContext.isDiscriminatorKeywordEnabled()) {
707704
ObjectNode discriminator = (ObjectNode) schemaNode.get("discriminator");
708705
if (null != discriminator && null != executionContext.getCurrentDiscriminatorContext()) {
709706
executionContext.getCurrentDiscriminatorContext().registerDiscriminator(schemaLocation,
@@ -715,7 +712,7 @@ public void validate(ExecutionContext executionContext, JsonNode jsonNode, JsonN
715712
v.validate(executionContext, jsonNode, rootNode, instanceLocation);
716713
}
717714

718-
if (this.validationContext.getConfig().isDiscriminatorKeywordEnabled()) {
715+
if (this.validationContext.isDiscriminatorKeywordEnabled()) {
719716
ObjectNode discriminator = (ObjectNode) this.schemaNode.get("discriminator");
720717
if (null != discriminator) {
721718
final DiscriminatorContext discriminatorContext = executionContext
@@ -1026,7 +1023,7 @@ public <T> T validate(ExecutionContext executionContext, JsonNode node, OutputFo
10261023
*/
10271024
private JsonNode deserialize(String input, InputFormat inputFormat) {
10281025
try {
1029-
return this.getValidationContext().getJsonSchemaFactory().readTree(input, inputFormat);
1026+
return this.getValidationContext().getSchemaRegistry().readTree(input, inputFormat);
10301027
} catch (IOException e) {
10311028
throw new IllegalArgumentException("Invalid input", e);
10321029
}
@@ -1063,7 +1060,7 @@ private ValidationResult validateAndCollect(ExecutionContext executionContext, J
10631060
validate(executionContext, jsonNode, rootNode, instanceLocation);
10641061

10651062
// Get the config.
1066-
SchemaValidatorsConfig config = this.validationContext.getConfig();
1063+
SchemaValidatorsConfig config = this.validationContext.getSchemaRegistryConfig();
10671064

10681065
// 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.
10691066
if (config.doLoadCollectors()) {
@@ -1336,7 +1333,7 @@ private <T> T walkAtNodeInternal(ExecutionContext executionContext, JsonNode nod
13361333
walk(executionContext, node, rootNode, instanceLocation, validate);
13371334

13381335
// Get the config.
1339-
SchemaValidatorsConfig config = this.validationContext.getConfig();
1336+
SchemaValidatorsConfig config = this.validationContext.getSchemaRegistryConfig();
13401337
// 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.
13411338
/* When doLoadCollectors is removed after the deprecation period the following block should be removed */
13421339
if (config.doLoadCollectors()) {
@@ -1359,14 +1356,14 @@ public void walk(ExecutionContext executionContext, JsonNode node, JsonNode root
13591356
try {
13601357
// Call all the pre-walk listeners. If at least one of the pre walk listeners
13611358
// returns SKIP, then skip the walk.
1362-
if (this.validationContext.getConfig().getKeywordWalkListenerRunner().runPreWalkListeners(executionContext,
1359+
if (this.validationContext.getSchemaRegistryConfig().getKeywordWalkListenerRunner().runPreWalkListeners(executionContext,
13631360
evaluationPathWithKeyword.getName(-1), node, rootNode, instanceLocation,
13641361
this, validator)) {
13651362
validator.walk(executionContext, node, rootNode, instanceLocation, shouldValidateSchema);
13661363
}
13671364
} finally {
13681365
// Call all the post-walk listeners.
1369-
this.validationContext.getConfig().getKeywordWalkListenerRunner().runPostWalkListeners(executionContext,
1366+
this.validationContext.getSchemaRegistryConfig().getKeywordWalkListenerRunner().runPostWalkListeners(executionContext,
13701367
evaluationPathWithKeyword.getName(-1), node, rootNode, instanceLocation,
13711368
this, validator,
13721369
executionContext.getErrors().subList(currentErrors, executionContext.getErrors().size()));
@@ -1433,7 +1430,7 @@ public boolean isRecursiveAnchor() {
14331430
* @return the execution context
14341431
*/
14351432
public ExecutionContext createExecutionContext() {
1436-
SchemaValidatorsConfig config = validationContext.getConfig();
1433+
SchemaValidatorsConfig config = validationContext.getSchemaRegistryConfig();
14371434
// Copy execution config defaults from validation config
14381435
ExecutionConfig executionConfig = new ExecutionConfig();
14391436
executionConfig.setLocale(config.getLocale());

0 commit comments

Comments
 (0)