|
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