Skip to content

Commit e83fc2e

Browse files
committed
fixes #54 implement Const validator
1 parent 9f8febd commit e83fc2e

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.networknt.schema;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.math.BigDecimal;
8+
import java.util.Collections;
9+
import java.util.LinkedHashSet;
10+
import java.util.Set;
11+
12+
public class ConstValidator extends BaseJsonValidator implements JsonValidator {
13+
private static final Logger logger = LoggerFactory.getLogger(ConstValidator.class);
14+
JsonNode schemaNode;
15+
16+
public ConstValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
17+
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.CONST, validationContext);
18+
this.schemaNode = schemaNode;
19+
}
20+
21+
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
22+
debug(logger, node, rootNode, at);
23+
24+
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
25+
if(schemaNode.isNumber() && node.isNumber()) {
26+
if(schemaNode.decimalValue().compareTo(node.decimalValue()) != 0) {
27+
errors.add(buildValidationMessage(at, schemaNode.asText()));
28+
}
29+
} else if (!schemaNode.equals(node)) {
30+
errors.add(buildValidationMessage(at, schemaNode.asText()));
31+
}
32+
return Collections.unmodifiableSet(errors);
33+
}
34+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
7070
EXCLUSIVE_MAXIMUM("exclusiveMaximum", "1038", new MessageFormat("{0}: must have a exclusive maximum value of {1}"), ExclusiveMaximumValidator.class, 14), // V6|V7|V201909
7171
EXCLUSIVE_MINIMUM("exclusiveMinimum", "1039", new MessageFormat("{0}: must have a exclusive minimum value of {1}"), ExclusiveMinimumValidator.class, 14),
7272
TRUE("true", "1040", null, TrueValidator.class, 14),
73-
FALSE("false", "1041", new MessageFormat("Boolean schema false is not valid"), FalseValidator.class, 14);
73+
FALSE("false", "1041", new MessageFormat("Boolean schema false is not valid"), FalseValidator.class, 14),
74+
CONST("const", "1042", new MessageFormat("{0}: must be a constant value {1}"), ConstValidator.class, 14);
7475

7576
private static Map<String, ValidatorTypeCode> constants = new HashMap<String, ValidatorTypeCode>();
7677
private static SpecVersion specVersion = new SpecVersion();

src/test/java/com/networknt/schema/V201909JsonSchemaTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ public void testBooleanSchemaValidator() throws Exception {
261261
}
262262

263263
@Test
264-
@Ignore
265264
public void testConstValidator() throws Exception {
266265
runTestFile("draft2019-09/const.json");
267266
}

src/test/java/com/networknt/schema/V7JsonSchemaTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ public void testBooleanSchemaValidator() throws Exception {
257257
}
258258

259259
@Test
260-
@Ignore
261260
public void testConstValidator() throws Exception {
262261
runTestFile("draft7/const.json");
263262
}

0 commit comments

Comments
 (0)