Skip to content

Commit 534920c

Browse files
committed
Rename JsonValidator to KeywordValidator and fix class hierarchy
1 parent 244ab0c commit 534920c

File tree

74 files changed

+260
-325
lines changed

Some content is hidden

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

74 files changed

+260
-325
lines changed

src/main/java/com/networknt/schema/AbstractJsonValidator.java renamed to src/main/java/com/networknt/schema/AbstractKeywordValidator.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,38 @@
2222
import com.networknt.schema.annotation.JsonNodeAnnotation;
2323

2424
/**
25-
* Base {@link JsonValidator}.
25+
* Abstract {@link KeywordValidator}.
2626
*/
27-
public abstract class AbstractJsonValidator implements JsonValidator {
28-
private final SchemaLocation schemaLocation;
29-
private final JsonNode schemaNode;
30-
private final JsonNodePath evaluationPath;
27+
public abstract class AbstractKeywordValidator implements KeywordValidator {
3128
private final String keyword;
29+
protected final JsonNode schemaNode;
30+
protected final SchemaLocation schemaLocation;
31+
32+
protected final JsonNodePath evaluationPath;
3233

3334
/**
3435
* Constructor.
35-
*
36-
* @param schemaLocation the schema location
37-
* @param evaluationPath the evaluation path
3836
* @param keyword the keyword
3937
* @param schemaNode the schema node
38+
* @param schemaLocation the schema location
39+
* @param evaluationPath the evaluation path
4040
*/
41-
public AbstractJsonValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, Keyword keyword, JsonNode schemaNode) {
41+
public AbstractKeywordValidator(String keyword, JsonNode schemaNode, SchemaLocation schemaLocation, JsonNodePath evaluationPath) {
42+
this.keyword = keyword;
43+
this.schemaNode = schemaNode;
4244
this.schemaLocation = schemaLocation;
4345
this.evaluationPath = evaluationPath;
44-
this.keyword = keyword.getValue();
45-
this.schemaNode = schemaNode;
46+
}
47+
48+
/**
49+
* Constructor.
50+
* @param keyword the keyword
51+
* @param schemaNode the schema node
52+
* @param schemaLocation the schema location
53+
* @param evaluationPath the evaluation path
54+
*/
55+
public AbstractKeywordValidator(Keyword keyword, JsonNode schemaNode, SchemaLocation schemaLocation, JsonNodePath evaluationPath) {
56+
this(keyword.getValue(), schemaNode, schemaLocation, evaluationPath);
4657
}
4758

4859
@Override

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
import java.util.Set;
3434

3535
/**
36-
* {@link JsonValidator} for additionalProperties.
36+
* {@link KeywordValidator} for additionalProperties.
3737
*/
38-
public class AdditionalPropertiesValidator extends BaseJsonValidator {
38+
public class AdditionalPropertiesValidator extends BaseKeywordValidator {
3939
private static final Logger logger = LoggerFactory.getLogger(AdditionalPropertiesValidator.class);
4040

4141
private final boolean allowAdditionalProperties;
@@ -47,7 +47,7 @@ public class AdditionalPropertiesValidator extends BaseJsonValidator {
4747

4848
public AdditionalPropertiesValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema,
4949
ValidationContext validationContext) {
50-
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ADDITIONAL_PROPERTIES, validationContext);
50+
super(ValidatorTypeCode.ADDITIONAL_PROPERTIES, schemaNode, schemaLocation, parentSchema, validationContext, evaluationPath);
5151
if (schemaNode.isBoolean()) {
5252
allowAdditionalProperties = schemaNode.booleanValue();
5353
additionalPropertiesSchema = null;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
import org.slf4j.LoggerFactory;
2626

2727
/**
28-
* {@link JsonValidator} for allOf.
28+
* {@link KeywordValidator} for allOf.
2929
*/
30-
public class AllOfValidator extends BaseJsonValidator {
30+
public class AllOfValidator extends BaseKeywordValidator {
3131
private static final Logger logger = LoggerFactory.getLogger(AllOfValidator.class);
3232

3333
private final List<JsonSchema> schemas;
3434

3535
public AllOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
36-
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ALL_OF, validationContext);
36+
super(ValidatorTypeCode.ALL_OF, schemaNode, schemaLocation, parentSchema, validationContext, evaluationPath);
3737
if (!schemaNode.isArray()) {
3838
JsonType nodeType = TypeFactory.getValueNodeType(schemaNode, this.validationContext.getConfig());
3939
throw new JsonSchemaException(error().instanceNode(schemaNode)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
*/
2424
public class AnnotationKeyword extends AbstractKeyword {
2525

26-
private static final class Validator extends AbstractJsonValidator {
26+
private static final class Validator extends AbstractKeywordValidator {
2727
public Validator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
2828
JsonSchema parentSchema, ValidationContext validationContext, Keyword keyword) {
29-
super(schemaLocation, evaluationPath, keyword, schemaNode);
29+
super(keyword, schemaNode, schemaLocation, evaluationPath);
3030
}
3131

3232
@Override
@@ -57,7 +57,7 @@ public AnnotationKeyword(String keyword) {
5757
}
5858

5959
@Override
60-
public JsonValidator newValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
60+
public KeywordValidator newValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
6161
JsonSchema parentSchema, ValidationContext validationContext) {
6262
return new Validator(schemaLocation, evaluationPath, schemaNode, parentSchema, validationContext, this);
6363
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import java.util.*;
2525

2626
/**
27-
* {@link JsonValidator} for anyOf.
27+
* {@link KeywordValidator} for anyOf.
2828
*/
29-
public class AnyOfValidator extends BaseJsonValidator {
29+
public class AnyOfValidator extends BaseKeywordValidator {
3030
private static final Logger logger = LoggerFactory.getLogger(AnyOfValidator.class);
3131
private static final String DISCRIMINATOR_REMARK = "and the discriminator-selected candidate schema didn't pass validation";
3232

@@ -35,7 +35,7 @@ public class AnyOfValidator extends BaseJsonValidator {
3535
private Boolean canShortCircuit = null;
3636

3737
public AnyOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
38-
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ANY_OF, validationContext);
38+
super(ValidatorTypeCode.ANY_OF, schemaNode, schemaLocation, parentSchema, validationContext, evaluationPath);
3939
if (!schemaNode.isArray()) {
4040
JsonType nodeType = TypeFactory.getValueNodeType(schemaNode, this.validationContext.getConfig());
4141
throw new JsonSchemaException(error().instanceNode(schemaNode)
@@ -200,7 +200,7 @@ protected boolean canShortCircuit(ExecutionContext executionContext) {
200200
protected boolean canShortCircuit() {
201201
if (this.canShortCircuit == null) {
202202
boolean canShortCircuit = true;
203-
for (JsonValidator validator : getEvaluationParentSchema().getValidators()) {
203+
for (KeywordValidator validator : getEvaluationParentSchema().getValidators()) {
204204
if ("unevaluatedProperties".equals(validator.getKeyword())
205205
|| "unevaluatedItems".equals(validator.getKeyword())) {
206206
canShortCircuit = false;

src/main/java/com/networknt/schema/BaseJsonValidator.java renamed to src/main/java/com/networknt/schema/BaseKeywordValidator.java

Lines changed: 14 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -17,80 +17,65 @@
1717
package com.networknt.schema;
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
20-
import com.networknt.schema.annotation.JsonNodeAnnotation;
2120

2221
import org.slf4j.Logger;
2322

2423
import java.util.Collection;
2524
import java.util.Map;
26-
import java.util.function.Consumer;
2725

2826
/**
29-
* Base {@link JsonValidator}.
27+
* Base {@link KeywordValidator}.
3028
*/
31-
public abstract class BaseJsonValidator implements JsonValidator {
32-
protected final JsonNode schemaNode;
33-
29+
public abstract class BaseKeywordValidator extends AbstractKeywordValidator {
3430
protected final ValidationContext validationContext;
3531

36-
protected final Keyword keyword;
3732
protected final JsonSchema parentSchema;
38-
protected final SchemaLocation schemaLocation;
3933
protected final Map<String, String> errorMessage;
4034

41-
protected final JsonNodePath evaluationPath;
4235
protected final JsonSchema evaluationParentSchema;
4336

44-
public BaseJsonValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
45-
JsonSchema parentSchema, Keyword keyword,
46-
ValidationContext validationContext) {
37+
public BaseKeywordValidator(Keyword keyword, JsonNode schemaNode, SchemaLocation schemaLocation,
38+
JsonSchema parentSchema, ValidationContext validationContext,
39+
JsonNodePath evaluationPath) {
40+
super(keyword, schemaNode, schemaLocation, evaluationPath);
4741
this.validationContext = validationContext;
48-
this.schemaNode = schemaNode;
4942

50-
this.keyword = keyword;
5143
this.parentSchema = parentSchema;
52-
this.schemaLocation = schemaLocation;
5344
if (keyword != null && parentSchema != null && validationContext.getConfig().getErrorMessageKeyword() != null) {
5445
this.errorMessage = ErrorMessages.getErrorMessage(parentSchema,
5546
validationContext.getConfig().getErrorMessageKeyword(), keyword.getValue());
5647
} else {
5748
this.errorMessage = null;
5849
}
59-
this.evaluationPath = evaluationPath;
6050
this.evaluationParentSchema = null;
6151
}
6252

6353
/**
6454
* Constructor to create a copy using fields.
65-
*
55+
* @param keyword the keyword
6656
* @param schemaNode the schema node
57+
* @param schemaLocation the schema location
6758
* @param validationContext the validation context
68-
* @param keyword the keyword
6959
* @param parentSchema the parent schema
70-
* @param schemaLocation the schema location
7160
* @param evaluationPath the evaluation path
7261
* @param evaluationParentSchema the evaluation parent schema
7362
* @param errorMessage the error message
7463
*/
75-
protected BaseJsonValidator(
76-
/* Below from BaseJsonValidator */
64+
protected BaseKeywordValidator(
65+
Keyword keyword,
7766
JsonNode schemaNode,
67+
SchemaLocation schemaLocation,
7868
ValidationContext validationContext,
79-
Keyword keyword,
8069
JsonSchema parentSchema,
81-
SchemaLocation schemaLocation,
8270
JsonNodePath evaluationPath,
8371
JsonSchema evaluationParentSchema,
8472
Map<String, String> errorMessage) {
85-
this.schemaNode = schemaNode;
73+
super(keyword, schemaNode, schemaLocation, evaluationPath);
8674
this.validationContext = validationContext;
8775

88-
this.keyword = keyword;
8976
this.parentSchema = parentSchema;
90-
this.schemaLocation = schemaLocation;
9177
this.errorMessage = errorMessage;
9278

93-
this.evaluationPath = evaluationPath;
9479
this.evaluationParentSchema = evaluationParentSchema;
9580
}
9681

@@ -117,25 +102,6 @@ public static void debug(Logger logger, ExecutionContext executionContext, JsonN
117102
}
118103
}
119104

120-
@Override
121-
public SchemaLocation getSchemaLocation() {
122-
return this.schemaLocation;
123-
}
124-
125-
@Override
126-
public JsonNodePath getEvaluationPath() {
127-
return this.evaluationPath;
128-
}
129-
130-
@Override
131-
public String getKeyword() {
132-
return this.keyword.getValue();
133-
}
134-
135-
public JsonNode getSchemaNode() {
136-
return this.schemaNode;
137-
}
138-
139105
/**
140106
* Gets the parent schema.
141107
* <p>
@@ -176,13 +142,6 @@ protected void preloadJsonSchemas(final Collection<JsonSchema> schemas) {
176142
}
177143
}
178144

179-
180-
181-
@Override
182-
public String toString() {
183-
return getEvaluationPath().getName(-1);
184-
}
185-
186145
/**
187146
* Determines if the keyword exists adjacent in the evaluation path.
188147
* <p>
@@ -199,7 +158,7 @@ public String toString() {
199158
protected boolean hasAdjacentKeywordInEvaluationPath(String keyword) {
200159
JsonSchema schema = getEvaluationParentSchema();
201160
while (schema != null) {
202-
for (JsonValidator validator : schema.getValidators()) {
161+
for (KeywordValidator validator : schema.getValidators()) {
203162
if (keyword.equals(validator.getKeyword())) {
204163
return true;
205164
}
@@ -218,41 +177,6 @@ protected MessageSourceError.Builder error() {
218177
return MessageSourceError
219178
.builder(this.validationContext.getConfig().getMessageSource(), this.errorMessage)
220179
.schemaNode(this.schemaNode).schemaLocation(this.schemaLocation).evaluationPath(this.evaluationPath)
221-
.keyword(this.keyword != null ? this.keyword.getValue() : null).messageKey(this.getKeyword());
222-
}
223-
224-
/**
225-
* Determine if annotations should be reported.
226-
*
227-
* @param executionContext the execution context
228-
* @return true if annotations should be reported
229-
*/
230-
protected boolean collectAnnotations(ExecutionContext executionContext) {
231-
return collectAnnotations(executionContext, getKeyword());
232-
}
233-
234-
/**
235-
* Determine if annotations should be reported.
236-
*
237-
* @param executionContext the execution context
238-
* @param keyword the keyword
239-
* @return true if annotations should be reported
240-
*/
241-
protected boolean collectAnnotations(ExecutionContext executionContext, String keyword) {
242-
return executionContext.getExecutionConfig().isAnnotationCollectionEnabled()
243-
&& executionContext.getExecutionConfig().getAnnotationCollectionFilter().test(keyword);
244-
}
245-
246-
/**
247-
* Puts an annotation.
248-
*
249-
* @param executionContext the execution context
250-
* @param customizer to customize the annotation
251-
*/
252-
protected void putAnnotation(ExecutionContext executionContext, Consumer<JsonNodeAnnotation.Builder> customizer) {
253-
JsonNodeAnnotation.Builder builder = JsonNodeAnnotation.builder().evaluationPath(this.evaluationPath)
254-
.schemaLocation(this.schemaLocation).keyword(getKeyword());
255-
customizer.accept(builder);
256-
executionContext.getAnnotations().put(builder.build());
180+
.keyword(this.getKeyword()).messageKey(this.getKeyword());
257181
}
258182
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
import org.slf4j.LoggerFactory;
2121

2222
/**
23-
* {@link JsonValidator} for const.
23+
* {@link KeywordValidator} for const.
2424
*/
25-
public class ConstValidator extends BaseJsonValidator implements JsonValidator {
25+
public class ConstValidator extends BaseKeywordValidator implements KeywordValidator {
2626
private static final Logger logger = LoggerFactory.getLogger(ConstValidator.class);
2727

2828
public ConstValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
2929
JsonSchema parentSchema, ValidationContext validationContext) {
30-
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.CONST, validationContext);
30+
super(ValidatorTypeCode.CONST, schemaNode, schemaLocation, parentSchema, validationContext, evaluationPath);
3131
}
3232

3333
public void validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
import static com.networknt.schema.VersionCode.MinV201909;
3131

3232
/**
33-
* {@link JsonValidator} for contains.
33+
* {@link KeywordValidator} for contains.
3434
*/
35-
public class ContainsValidator extends BaseJsonValidator {
35+
public class ContainsValidator extends BaseKeywordValidator {
3636
private static final Logger logger = LoggerFactory.getLogger(ContainsValidator.class);
3737
private static final String CONTAINS_MAX = "contains.max";
3838
private static final String CONTAINS_MIN = "contains.min";
@@ -46,7 +46,7 @@ public class ContainsValidator extends BaseJsonValidator {
4646
private Boolean hasUnevaluatedItemsValidator = null;
4747

4848
public ContainsValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
49-
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.CONTAINS, validationContext);
49+
super(ValidatorTypeCode.CONTAINS, schemaNode, schemaLocation, parentSchema, validationContext, evaluationPath);
5050

5151
// Draft 6 added the contains keyword but maxContains and minContains first
5252
// appeared in Draft 2019-09 so the semantics of the validation changes

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
import java.util.Base64;
2525

2626
/**
27-
* {@link JsonValidator} for contentEncoding.
27+
* {@link KeywordValidator} for contentEncoding.
2828
* <p>
2929
* Note that since 2019-09 this keyword only generates annotations and not
3030
* errors.
3131
*/
32-
public class ContentEncodingValidator extends BaseJsonValidator {
32+
public class ContentEncodingValidator extends BaseKeywordValidator {
3333
private static final Logger logger = LoggerFactory.getLogger(ContentEncodingValidator.class);
3434
private final String contentEncoding;
3535

@@ -44,8 +44,8 @@ public class ContentEncodingValidator extends BaseJsonValidator {
4444
*/
4545
public ContentEncodingValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
4646
JsonSchema parentSchema, ValidationContext validationContext) {
47-
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.CONTENT_ENCODING,
48-
validationContext);
47+
super(ValidatorTypeCode.CONTENT_ENCODING, schemaNode, schemaLocation, parentSchema, validationContext,
48+
evaluationPath);
4949
this.contentEncoding = schemaNode.textValue();
5050
}
5151

0 commit comments

Comments
 (0)