Skip to content

Commit d3bba94

Browse files
committed
Refactor to remove ErrorMessageType
1 parent 36e941d commit d3bba94

33 files changed

+186
-312
lines changed

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,8 @@ public abstract class BaseJsonValidator implements JsonValidator {
4141
protected final JsonNodePath evaluationPath;
4242
protected final JsonSchema evaluationParentSchema;
4343

44-
// Pending removal
45-
protected final ErrorMessageType errorMessageType;
46-
4744
public BaseJsonValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
48-
JsonSchema parentSchema, ValidatorTypeCode validatorType, ValidationContext validationContext) {
49-
this(schemaLocation, evaluationPath, schemaNode, parentSchema, validatorType, validatorType, validationContext);
50-
}
51-
52-
public BaseJsonValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
53-
JsonSchema parentSchema, ErrorMessageType errorMessageType, Keyword keyword,
45+
JsonSchema parentSchema, Keyword keyword,
5446
ValidationContext validationContext) {
5547
this.validationContext = validationContext;
5648
this.schemaNode = schemaNode;
@@ -64,8 +56,6 @@ public BaseJsonValidator(SchemaLocation schemaLocation, JsonNodePath evaluationP
6456
} else {
6557
this.errorMessage = null;
6658
}
67-
68-
this.errorMessageType = errorMessageType;
6959
this.evaluationPath = evaluationPath;
7060
this.evaluationParentSchema = null;
7161
}
@@ -75,7 +65,6 @@ public BaseJsonValidator(SchemaLocation schemaLocation, JsonNodePath evaluationP
7565
*
7666
* @param schemaNode the schema node
7767
* @param validationContext the validation context
78-
* @param errorMessageType the error message type
7968
* @param keyword the keyword
8069
* @param parentSchema the parent schema
8170
* @param schemaLocation the schema location
@@ -88,7 +77,6 @@ protected BaseJsonValidator(
8877
JsonNode schemaNode,
8978
ValidationContext validationContext,
9079
/* Below from ValidationMessageHandler */
91-
ErrorMessageType errorMessageType,
9280
Keyword keyword,
9381
JsonSchema parentSchema,
9482
SchemaLocation schemaLocation,
@@ -103,7 +91,6 @@ protected BaseJsonValidator(
10391
this.schemaLocation = schemaLocation;
10492
this.errorMessage = errorMessage;
10593

106-
this.errorMessageType = errorMessageType;
10794
this.evaluationPath = evaluationPath;
10895
this.evaluationParentSchema = evaluationParentSchema;
10996
}
@@ -233,9 +220,9 @@ protected MessageSourceValidationMessage.Builder message() {
233220
if (failFast) {
234221
throw new FailFastAssertionException(message);
235222
}
236-
}).code(this.errorMessageType.getErrorCode()).schemaNode(this.schemaNode).schemaLocation(this.schemaLocation)
237-
.evaluationPath(this.evaluationPath).type(this.keyword != null ? this.keyword.getValue() : null)
238-
.messageKey(this.errorMessageType.getErrorCodeValue());
223+
}).schemaNode(this.schemaNode).schemaLocation(this.schemaLocation)
224+
.evaluationPath(this.evaluationPath).keyword(this.keyword != null ? this.keyword.getValue() : null)
225+
.messageKey(this.getKeyword());
239226
}
240227

241228
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private void boundsViolated(ExecutionContext executionContext, ValidatorTypeCode
194194
}
195195
executionContext.addError(message().instanceNode(instanceNode).instanceLocation(instanceLocation).messageKey(messageKey)
196196
.locale(locale).failFast(failFast).arguments(String.valueOf(bounds), this.schema.getSchemaNode().toString())
197-
.code(validatorTypeCode.getErrorCode()).type(validatorTypeCode.getValue()).build());
197+
.keyword(validatorTypeCode.getValue()).build());
198198
}
199199

200200
/**

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

Lines changed: 0 additions & 35 deletions
This file was deleted.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ public void validate(ExecutionContext executionContext, JsonNode node, JsonNode
9494
debug(logger, executionContext, node, rootNode, instanceLocation);
9595
JsonSchema refSchema = this.schema.getSchema();
9696
if (refSchema == null) {
97-
ValidationMessage validationMessage = message().type(ValidatorTypeCode.DYNAMIC_REF.getValue())
98-
.code("internal.unresolvedRef").message("{0}: Reference {1} cannot be resolved")
97+
ValidationMessage validationMessage = message().keyword(ValidatorTypeCode.DYNAMIC_REF.getValue())
98+
.messageKey("internal.unresolvedRef").message("{0}: Reference {1} cannot be resolved")
9999
.instanceLocation(instanceLocation).evaluationPath(getEvaluationPath())
100100
.arguments(schemaNode.asText()).build();
101101
throw new InvalidSchemaRefException(validationMessage);
@@ -111,8 +111,8 @@ public void walk(ExecutionContext executionContext, JsonNode node, JsonNode root
111111
// with the latest config. Reset the config.
112112
JsonSchema refSchema = this.schema.getSchema();
113113
if (refSchema == null) {
114-
ValidationMessage validationMessage = message().type(ValidatorTypeCode.DYNAMIC_REF.getValue())
115-
.code("internal.unresolvedRef").message("{0}: Reference {1} cannot be resolved")
114+
ValidationMessage validationMessage = message().keyword(ValidatorTypeCode.DYNAMIC_REF.getValue())
115+
.messageKey("internal.unresolvedRef").message("{0}: Reference {1} cannot be resolved")
116116
.instanceLocation(instanceLocation).evaluationPath(getEvaluationPath())
117117
.arguments(schemaNode.asText()).build();
118118
throw new InvalidSchemaRefException(validationMessage);

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

Lines changed: 0 additions & 38 deletions
This file was deleted.

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,19 @@
2727
*/
2828
public class FormatKeyword implements Keyword {
2929
private final String value;
30-
private final ErrorMessageType errorMessageType;
3130
private final Map<String, Format> formats;
3231

3332
public FormatKeyword(Map<String, Format> formats) {
3433
this(ValidatorTypeCode.FORMAT, formats);
3534
}
3635

37-
public FormatKeyword(ValidatorTypeCode type, Map<String, Format> formats) {
38-
this(type.getValue(), type, formats);
36+
public FormatKeyword(Keyword type, Map<String, Format> formats) {
37+
this(type.getValue(), formats);
3938
}
4039

41-
public FormatKeyword(String value, ErrorMessageType errorMessageType, Map<String, Format> formats) {
40+
public FormatKeyword(String value, Map<String, Format> formats) {
4241
this.value = value;
4342
this.formats = formats;
44-
this.errorMessageType = errorMessageType;
4543
}
4644

4745
Collection<Format> getFormats() {
@@ -56,7 +54,7 @@ public JsonValidator newValidator(SchemaLocation schemaLocation, JsonNodePath ev
5654
format = this.formats.get(formatName);
5755
}
5856
return new FormatValidator(schemaLocation, evaluationPath, schemaNode, parentSchema, validationContext, format,
59-
errorMessageType, this);
57+
this);
6058
}
6159

6260
@Override

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,11 @@ public class FormatValidator extends BaseFormatJsonValidator implements JsonVali
3434

3535
public FormatValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
3636
JsonSchema parentSchema, ValidationContext validationContext, Format format,
37-
ErrorMessageType errorMessageType, Keyword keyword) {
38-
super(schemaLocation, evaluationPath, schemaNode, parentSchema, errorMessageType, keyword, validationContext);
37+
Keyword keyword) {
38+
super(schemaLocation, evaluationPath, schemaNode, parentSchema, keyword, validationContext);
3939
this.format = format;
4040
}
4141

42-
public FormatValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
43-
JsonSchema parentSchema, ValidationContext validationContext, Format format, ValidatorTypeCode type) {
44-
this(schemaLocation, evaluationPath, schemaNode, parentSchema, validationContext, format, type, type);
45-
}
46-
4742
/**
4843
* Gets the annotation value.
4944
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private boolean doValidate(ExecutionContext executionContext, int i, JsonNode no
176176
} else {
177177
// no additional item allowed, return error
178178
executionContext.addError(message().instanceNode(rootNode).instanceLocation(instanceLocation)
179-
.type("additionalItems")
179+
.keyword("additionalItems")
180180
.messageKey("additionalItems")
181181
.evaluationPath(this.additionalItemsEvaluationPath)
182182
.schemaLocation(this.additionalItemsSchemaLocation)

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private static SchemaLocation resolve(SchemaLocation schemaLocation, JsonNode sc
185185
if (!validator.validate(id, rootSchema, schemaLocation, result, validationContext)) {
186186
SchemaLocation idSchemaLocation = schemaLocation.append(validationContext.getMetaSchema().getIdKeyword());
187187
ValidationMessage validationMessage = ValidationMessage.builder()
188-
.code(ValidatorTypeCode.ID.getValue()).type(ValidatorTypeCode.ID.getValue())
188+
.messageKey(ValidatorTypeCode.ID.getValue()).keyword(ValidatorTypeCode.ID.getValue())
189189
.instanceLocation(idSchemaLocation.getFragment())
190190
.arguments(id, validationContext.getMetaSchema().getIdKeyword(), idSchemaLocation)
191191
.schemaLocation(idSchemaLocation)
@@ -269,8 +269,6 @@ private JsonSchema(ValidationContext validationContext, SchemaLocation schemaLoc
269269
* @param suppressSubSchemaRetrieval to suppress sub schema retrieval
270270
* @param schemaNode the schema node
271271
* @param validationContext the validation context
272-
* @param errorMessageType the error message type
273-
* @param keyword the keyword
274272
* @param parentSchema the parent schema
275273
* @param schemaLocation the schema location
276274
* @param evaluationPath the evaluation path
@@ -289,8 +287,6 @@ protected JsonSchema(
289287
JsonNode schemaNode,
290288
ValidationContext validationContext,
291289
/* Below from ValidationMessageHandler */
292-
ErrorMessageType errorMessageType,
293-
Keyword keyword,
294290
JsonSchema parentSchema,
295291
SchemaLocation schemaLocation,
296292
JsonNodePath evaluationPath,
@@ -350,8 +346,7 @@ public JsonSchema fromRef(JsonSchema refEvaluationParentSchema, JsonNodePath ref
350346
schemaNode,
351347
validationContext,
352348
/* Below from ValidationMessageHandler */
353-
/*errorMessageType*/ null,
354-
/*keyword*/ null, parentSchema, schemaLocation, evaluationPath,
349+
parentSchema, schemaLocation, evaluationPath,
355350
evaluationParentSchema, /* errorMessage */ null);
356351
}
357352

@@ -384,8 +379,6 @@ public JsonSchema withConfig(SchemaValidatorsConfig config) {
384379
schemaNode,
385380
validationContext,
386381
/* Below from ValidationMessageHandler */
387-
/* errorMessageType */ null,
388-
/* keyword */ null,
389382
parentSchema,
390383
schemaLocation,
391384
evaluationPath,
@@ -516,7 +509,7 @@ public JsonSchema getSubSchema(JsonNodePath fragment) {
516509
}
517510
if (found == null) {
518511
ValidationMessage validationMessage = ValidationMessage.builder()
519-
.type(ValidatorTypeCode.REF.getValue()).code("internal.unresolvedRef")
512+
.keyword(ValidatorTypeCode.REF.getValue()).messageKey("internal.unresolvedRef")
520513
.message("{0}: Reference {1} cannot be resolved")
521514
.instanceLocation(schemaLocation.getFragment())
522515
.schemaLocation(schemaLocation)
@@ -634,8 +627,8 @@ private List<JsonValidator> read(JsonNode schemaNode) {
634627

635628
if ("$recursiveAnchor".equals(pname)) {
636629
if (!nodeToUse.isBoolean()) {
637-
ValidationMessage validationMessage = ValidationMessage.builder().type("$recursiveAnchor")
638-
.code("internal.invalidRecursiveAnchor")
630+
ValidationMessage validationMessage = ValidationMessage.builder().keyword("$recursiveAnchor")
631+
.messageKey("internal.invalidRecursiveAnchor")
639632
.message(
640633
"{0}: The value of a $recursiveAnchor must be a Boolean literal but is {1}")
641634
.instanceLocation(path)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public ValidationMessage build() {
6363
String messagePattern = null;
6464
if (this.errorMessage != null) {
6565
messagePattern = this.errorMessage.get("");
66-
if (this.property != null) {
67-
String specificMessagePattern = this.errorMessage.get(this.property);
66+
if (this.details != null && this.details.get("property") != null) {
67+
String specificMessagePattern = this.errorMessage.get(this.details.get("property"));
6868
if (specificMessagePattern != null) {
6969
messagePattern = specificMessagePattern;
7070
}

0 commit comments

Comments
 (0)