Skip to content

Commit

Permalink
Fix enum
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tay committed Jan 19, 2024
1 parent 832b9fc commit 8c467d8
Show file tree
Hide file tree
Showing 7 changed files with 597 additions and 2 deletions.
59 changes: 57 additions & 2 deletions src/main/java/com/networknt/schema/EnumValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.DecimalNode;
import com.fasterxml.jackson.databind.node.NullNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -45,7 +47,10 @@ public EnumValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath,
for (JsonNode n : schemaNode) {
if (n.isNumber()) {
// convert to DecimalNode for number comparison
nodes.add(DecimalNode.valueOf(n.decimalValue()));
nodes.add(processNumberNode(n));
} else if (n.isArray()) {
ArrayNode a = processArrayNode((ArrayNode) n);
nodes.add(a);
} else {
nodes.add(n);
}
Expand Down Expand Up @@ -78,7 +83,11 @@ public EnumValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath,
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {
debug(logger, node, rootNode, instanceLocation);

if (node.isNumber()) node = DecimalNode.valueOf(node.decimalValue());
if (node.isNumber()) {
node = processNumberNode(node);
} else if (node.isArray()) {
node = processArrayNode((ArrayNode) node);
}
if (!nodes.contains(node) && !( this.validationContext.getConfig().isTypeLoose() && isTypeLooseContainsInEnum(node))) {
return Collections.singleton(message().instanceLocation(instanceLocation)
.locale(executionContext.getExecutionConfig().getLocale()).arguments(error).build());
Expand All @@ -105,4 +114,50 @@ private boolean isTypeLooseContainsInEnum(JsonNode node) {
return false;
}

/**
* Processes the number and ensures trailing zeros are stripped.
*
* @param n the node
* @return the node
*/
protected JsonNode processNumberNode(JsonNode n) {
return DecimalNode.valueOf(new BigDecimal(n.decimalValue().toPlainString()));
}

/**
* Processes the array and ensures that numbers within have trailing zeroes stripped.
*
* @param node the node
* @return the node
*/
protected ArrayNode processArrayNode(ArrayNode node) {
if (!hasNumber(node)) {
return node;
}
ArrayNode a = (ArrayNode) node.deepCopy();
for (int x = 0; x < a.size(); x++) {
JsonNode v = a.get(x);
if (v.isNumber()) {
v = processNumberNode(v);
a.set(x, v);
}
}
return a;
}

/**
* Determines if the array node contains a number.
*
* @param node the node
* @return the node
*/
protected boolean hasNumber(ArrayNode node) {
for (int x = 0; x < node.size(); x++) {
JsonNode v = node.get(x);
if (v.isNumber()) {
return true;
}
}
return false;
}
}
96 changes: 96 additions & 0 deletions src/test/suite/tests/draft-next/enum.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@
}
]
},
{
"description": "enum with [false] does not match [0]",
"schema": {
"$schema": "https://json-schema.org/draft/next/schema",
"enum": [[false]]
},
"tests": [
{
"description": "[false] is valid",
"data": [false],
"valid": true
},
{
"description": "[0] is invalid",
"data": [0],
"valid": false
},
{
"description": "[0.0] is invalid",
"data": [0.0],
"valid": false
}
]
},
{
"description": "enum with true does not match 1",
"schema": {
Expand All @@ -192,6 +216,30 @@
}
]
},
{
"description": "enum with [true] does not match [1]",
"schema": {
"$schema": "https://json-schema.org/draft/next/schema",
"enum": [[true]]
},
"tests": [
{
"description": "[true] is valid",
"data": [true],
"valid": true
},
{
"description": "[1] is invalid",
"data": [1],
"valid": false
},
{
"description": "[1.0] is invalid",
"data": [1.0],
"valid": false
}
]
},
{
"description": "enum with 0 does not match false",
"schema": {
Expand All @@ -216,6 +264,30 @@
}
]
},
{
"description": "enum with [0] does not match [false]",
"schema": {
"$schema": "https://json-schema.org/draft/next/schema",
"enum": [[0]]
},
"tests": [
{
"description": "[false] is invalid",
"data": [false],
"valid": false
},
{
"description": "[0] is valid",
"data": [0],
"valid": true
},
{
"description": "[0.0] is valid",
"data": [0.0],
"valid": true
}
]
},
{
"description": "enum with 1 does not match true",
"schema": {
Expand All @@ -240,6 +312,30 @@
}
]
},
{
"description": "enum with [1] does not match [true]",
"schema": {
"$schema": "https://json-schema.org/draft/next/schema",
"enum": [[1]]
},
"tests": [
{
"description": "[true] is invalid",
"data": [true],
"valid": false
},
{
"description": "[1] is valid",
"data": [1],
"valid": true
},
{
"description": "[1.0] is valid",
"data": [1.0],
"valid": true
}
]
},
{
"description": "nul characters in strings",
"schema": {
Expand Down
96 changes: 96 additions & 0 deletions src/test/suite/tests/draft2019-09/enum.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@
}
]
},
{
"description": "enum with [false] does not match [0]",
"schema": {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [[false]]
},
"tests": [
{
"description": "[false] is valid",
"data": [false],
"valid": true
},
{
"description": "[0] is invalid",
"data": [0],
"valid": false
},
{
"description": "[0.0] is invalid",
"data": [0.0],
"valid": false
}
]
},
{
"description": "enum with true does not match 1",
"schema": {
Expand All @@ -192,6 +216,30 @@
}
]
},
{
"description": "enum with [true] does not match [1]",
"schema": {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [[true]]
},
"tests": [
{
"description": "[true] is valid",
"data": [true],
"valid": true
},
{
"description": "[1] is invalid",
"data": [1],
"valid": false
},
{
"description": "[1.0] is invalid",
"data": [1.0],
"valid": false
}
]
},
{
"description": "enum with 0 does not match false",
"schema": {
Expand All @@ -216,6 +264,30 @@
}
]
},
{
"description": "enum with [0] does not match [false]",
"schema": {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [[0]]
},
"tests": [
{
"description": "[false] is invalid",
"data": [false],
"valid": false
},
{
"description": "[0] is valid",
"data": [0],
"valid": true
},
{
"description": "[0.0] is valid",
"data": [0.0],
"valid": true
}
]
},
{
"description": "enum with 1 does not match true",
"schema": {
Expand All @@ -240,6 +312,30 @@
}
]
},
{
"description": "enum with [1] does not match [true]",
"schema": {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"enum": [[1]]
},
"tests": [
{
"description": "[true] is invalid",
"data": [true],
"valid": false
},
{
"description": "[1] is valid",
"data": [1],
"valid": true
},
{
"description": "[1.0] is valid",
"data": [1.0],
"valid": true
}
]
},
{
"description": "nul characters in strings",
"schema": {
Expand Down
Loading

0 comments on commit 8c467d8

Please sign in to comment.