Skip to content

Commit

Permalink
fixes #54 implement Const validator
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehu committed Nov 16, 2019
1 parent 9f8febd commit e83fc2e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/main/java/com/networknt/schema/ConstValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

public class ConstValidator extends BaseJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(ConstValidator.class);
JsonNode schemaNode;

public ConstValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.CONST, validationContext);
this.schemaNode = schemaNode;
}

public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
debug(logger, node, rootNode, at);

Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
if(schemaNode.isNumber() && node.isNumber()) {
if(schemaNode.decimalValue().compareTo(node.decimalValue()) != 0) {
errors.add(buildValidationMessage(at, schemaNode.asText()));
}
} else if (!schemaNode.equals(node)) {
errors.add(buildValidationMessage(at, schemaNode.asText()));
}
return Collections.unmodifiableSet(errors);
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/networknt/schema/ValidatorTypeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
EXCLUSIVE_MAXIMUM("exclusiveMaximum", "1038", new MessageFormat("{0}: must have a exclusive maximum value of {1}"), ExclusiveMaximumValidator.class, 14), // V6|V7|V201909
EXCLUSIVE_MINIMUM("exclusiveMinimum", "1039", new MessageFormat("{0}: must have a exclusive minimum value of {1}"), ExclusiveMinimumValidator.class, 14),
TRUE("true", "1040", null, TrueValidator.class, 14),
FALSE("false", "1041", new MessageFormat("Boolean schema false is not valid"), FalseValidator.class, 14);
FALSE("false", "1041", new MessageFormat("Boolean schema false is not valid"), FalseValidator.class, 14),
CONST("const", "1042", new MessageFormat("{0}: must be a constant value {1}"), ConstValidator.class, 14);

private static Map<String, ValidatorTypeCode> constants = new HashMap<String, ValidatorTypeCode>();
private static SpecVersion specVersion = new SpecVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ public void testBooleanSchemaValidator() throws Exception {
}

@Test
@Ignore
public void testConstValidator() throws Exception {
runTestFile("draft2019-09/const.json");
}
Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/networknt/schema/V7JsonSchemaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ public void testBooleanSchemaValidator() throws Exception {
}

@Test
@Ignore
public void testConstValidator() throws Exception {
runTestFile("draft7/const.json");
}
Expand Down

0 comments on commit e83fc2e

Please sign in to comment.