Skip to content

Commit 3aafcd9

Browse files
jiachen1120stevehu
authored andcommitted
Fixed validation for path parameters and query parameters (#113)
* Fixed validators for path parameter and query parameter The following schema can work properly now - minimum - maximum - enum - pattern - maxItems - minItems * - Change method name in EnumValidator from isTypeLooseEqual to isTypeLooseContainsInEnum and add comments to it, in addition, typeLoose check is been taken out from the method. - Reconstruct the isNumber check, now all the type check will be done by typeValidator, and minimumValidator and maximumValidator will reuse the isNumber() method provided by typeValidator. - Add typeLoose config into JsonSchemaTest and adding test cases in enum, minimum and maximum. * - Added more test cases in enum test
1 parent 1693ff1 commit 3aafcd9

File tree

8 files changed

+579
-505
lines changed

8 files changed

+579
-505
lines changed
Lines changed: 92 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,92 @@
1-
/*
2-
* Copyright (c) 2016 Network New Technologies Inc.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* You may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
17-
package com.networknt.schema;
18-
19-
import com.fasterxml.jackson.databind.JsonNode;
20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
23-
import java.util.Collections;
24-
import java.util.LinkedHashSet;
25-
import java.util.HashSet;
26-
import java.util.Set;
27-
28-
public class EnumValidator extends BaseJsonValidator implements JsonValidator {
29-
private static final Logger logger = LoggerFactory.getLogger(EnumValidator.class);
30-
31-
private final Set<JsonNode> nodes;
32-
private final String error;
33-
34-
public EnumValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
35-
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ENUM, validationContext);
36-
37-
if (schemaNode != null && schemaNode.isArray()) {
38-
nodes = new HashSet<JsonNode>();
39-
StringBuilder sb = new StringBuilder();
40-
41-
sb.append('[');
42-
String separator = "";
43-
44-
for (JsonNode n : schemaNode) {
45-
nodes.add(n);
46-
47-
sb.append(separator);
48-
sb.append(n.asText());
49-
separator = ", ";
50-
}
51-
52-
sb.append(']');
53-
54-
error = sb.toString();
55-
} else {
56-
nodes = Collections.emptySet();
57-
error = "[none]";
58-
}
59-
60-
parseErrorCode(getValidatorType().getErrorCodeKey());
61-
}
62-
63-
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
64-
debug(logger, node, rootNode, at);
65-
66-
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
67-
68-
if (!nodes.contains(node)) {
69-
errors.add(buildValidationMessage(at, error));
70-
}
71-
72-
return Collections.unmodifiableSet(errors);
73-
}
74-
75-
}
1+
/*
2+
* Copyright (c) 2016 Network New Technologies Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.networknt.schema;
18+
19+
import com.fasterxml.jackson.databind.JsonNode;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import java.util.Collections;
24+
import java.util.LinkedHashSet;
25+
import java.util.HashSet;
26+
import java.util.Set;
27+
28+
public class EnumValidator extends BaseJsonValidator implements JsonValidator {
29+
private static final Logger logger = LoggerFactory.getLogger(EnumValidator.class);
30+
31+
private final Set<JsonNode> nodes;
32+
private final String error;
33+
34+
public EnumValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
35+
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ENUM, validationContext);
36+
37+
if (schemaNode != null && schemaNode.isArray()) {
38+
nodes = new HashSet<JsonNode>();
39+
StringBuilder sb = new StringBuilder();
40+
41+
sb.append('[');
42+
String separator = "";
43+
44+
for (JsonNode n : schemaNode) {
45+
nodes.add(n);
46+
47+
sb.append(separator);
48+
sb.append(n.asText());
49+
separator = ", ";
50+
}
51+
52+
sb.append(']');
53+
54+
error = sb.toString();
55+
} else {
56+
nodes = Collections.emptySet();
57+
error = "[none]";
58+
}
59+
60+
parseErrorCode(getValidatorType().getErrorCodeKey());
61+
}
62+
63+
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
64+
debug(logger, node, rootNode, at);
65+
66+
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
67+
68+
if (!nodes.contains(node) && !(config.isTypeLoose() && isTypeLooseContainsInEnum(node))) {
69+
errors.add(buildValidationMessage(at, error));
70+
}
71+
72+
return Collections.unmodifiableSet(errors);
73+
}
74+
75+
/**
76+
* Check whether enum contains the value of the JsonNode if the typeLoose is enabled.
77+
* @param node JsonNode to check
78+
*/
79+
private boolean isTypeLooseContainsInEnum(JsonNode node) {
80+
if (TypeFactory.getValueNodeType(node) == JsonType.STRING) {
81+
String nodeText = node.textValue();
82+
for (JsonNode n : nodes) {
83+
String value = n.asText();
84+
if (value != null && value.equals(nodeText)) {
85+
return true;
86+
}
87+
}
88+
}
89+
return false;
90+
}
91+
92+
}
Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
1-
/*
2-
* Copyright (c) 2016 Network New Technologies Inc.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* You may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
17-
package com.networknt.schema;
18-
19-
import com.fasterxml.jackson.databind.JsonNode;
20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
23-
import java.util.Collections;
24-
import java.util.Set;
25-
26-
public class MaximumValidator extends BaseJsonValidator implements JsonValidator {
27-
private static final Logger logger = LoggerFactory.getLogger(MaximumValidator.class);
28-
private static final String PROPERTY_EXCLUSIVE_MAXIMUM = "exclusiveMaximum";
29-
30-
private double maximum;
31-
private boolean excludeEqual = false;
32-
33-
public MaximumValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
34-
35-
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MAXIMUM, validationContext);
36-
if (schemaNode.isNumber()) {
37-
maximum = schemaNode.doubleValue();
38-
} else {
39-
throw new JsonSchemaException("maximum value is not a number");
40-
}
41-
42-
JsonNode exclusiveMaximumNode = getParentSchema().getSchemaNode().get(PROPERTY_EXCLUSIVE_MAXIMUM);
43-
if (exclusiveMaximumNode != null && exclusiveMaximumNode.isBoolean()) {
44-
excludeEqual = exclusiveMaximumNode.booleanValue();
45-
}
46-
47-
parseErrorCode(getValidatorType().getErrorCodeKey());
48-
}
49-
50-
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
51-
debug(logger, node, rootNode, at);
52-
53-
if (!node.isNumber()) {
54-
// maximum only applies to numbers
55-
return Collections.emptySet();
56-
}
57-
58-
String fieldType = this.getNodeFieldType();
59-
60-
double value = node.doubleValue();
61-
if (greaterThan(value, maximum) || (excludeEqual && equals(value, maximum))) {
62-
if (JsonType.INTEGER.toString().equals(fieldType)) {
63-
return Collections.singleton(buildValidationMessage(at, "" + (int)maximum));
64-
}
65-
return Collections.singleton(buildValidationMessage(at, "" + maximum));
66-
}
67-
return Collections.emptySet();
68-
}
69-
70-
}
1+
/*
2+
* Copyright (c) 2016 Network New Technologies Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.networknt.schema;
18+
19+
import com.fasterxml.jackson.databind.JsonNode;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import java.util.Collections;
24+
import java.util.Set;
25+
26+
public class MaximumValidator extends BaseJsonValidator implements JsonValidator {
27+
private static final Logger logger = LoggerFactory.getLogger(MaximumValidator.class);
28+
private static final String PROPERTY_EXCLUSIVE_MAXIMUM = "exclusiveMaximum";
29+
30+
private double maximum;
31+
private boolean excludeEqual = false;
32+
33+
public MaximumValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
34+
35+
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MAXIMUM, validationContext);
36+
if (schemaNode.isNumber()) {
37+
maximum = schemaNode.doubleValue();
38+
} else {
39+
throw new JsonSchemaException("maximum value is not a number");
40+
}
41+
42+
JsonNode exclusiveMaximumNode = getParentSchema().getSchemaNode().get(PROPERTY_EXCLUSIVE_MAXIMUM);
43+
if (exclusiveMaximumNode != null && exclusiveMaximumNode.isBoolean()) {
44+
excludeEqual = exclusiveMaximumNode.booleanValue();
45+
}
46+
47+
parseErrorCode(getValidatorType().getErrorCodeKey());
48+
}
49+
50+
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
51+
debug(logger, node, rootNode, at);
52+
53+
if (!TypeValidator.isNumber(node, config.isTypeLoose())) {
54+
// maximum only applies to numbers
55+
return Collections.emptySet();
56+
}
57+
58+
String fieldType = this.getNodeFieldType();
59+
60+
double value = node.asDouble();
61+
if (greaterThan(value, maximum) || (excludeEqual && equals(value, maximum))) {
62+
if (JsonType.INTEGER.toString().equals(fieldType)) {
63+
return Collections.singleton(buildValidationMessage(at, "" + (int)maximum));
64+
}
65+
return Collections.singleton(buildValidationMessage(at, "" + maximum));
66+
}
67+
return Collections.emptySet();
68+
}
69+
70+
}

0 commit comments

Comments
 (0)