|
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 java.net.URI; |
20 |
| -import java.util.Set; |
21 |
| - |
22 |
| -import org.apache.commons.lang3.StringUtils; |
23 |
| -import org.slf4j.Logger; |
24 |
| - |
25 |
| -import com.fasterxml.jackson.databind.JsonNode; |
26 |
| - |
27 |
| -public abstract class BaseJsonValidator implements JsonValidator { |
28 |
| - private String schemaPath; |
29 |
| - private JsonNode schemaNode; |
30 |
| - private JsonSchema parentSchema; |
31 |
| - private boolean suppressSubSchemaRetrieval; |
32 |
| - private ValidatorTypeCode validatorType; |
33 |
| - private ErrorMessageType errorMessageType; |
34 |
| - /** |
35 |
| - * SchemaValidatorsConfig can only get and set in validationContext |
36 |
| - */ |
37 |
| - protected SchemaValidatorsConfig config; |
38 |
| - |
39 |
| - /** |
40 |
| - * ThreadLocal to allow to pass state in recursive validator calls |
41 |
| - */ |
42 |
| - protected final static ThreadLocal<ValidatorState> validatorState = new ThreadLocal<ValidatorState>(); |
43 |
| - |
44 |
| - public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, |
45 |
| - ValidatorTypeCode validatorType, ValidationContext validationContext) { |
46 |
| - this(schemaPath, schemaNode, parentSchema, validatorType, false ); |
47 |
| - this.config = validationContext.getConfig() == null ? new SchemaValidatorsConfig() : validationContext.getConfig(); |
48 |
| - } |
49 |
| - |
50 |
| - public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, |
51 |
| - ValidatorTypeCode validatorType, boolean suppressSubSchemaRetrieval) { |
52 |
| - this.errorMessageType = validatorType; |
53 |
| - this.schemaPath = schemaPath; |
54 |
| - this.schemaNode = schemaNode; |
55 |
| - this.parentSchema = parentSchema; |
56 |
| - this.validatorType = validatorType; |
57 |
| - this.suppressSubSchemaRetrieval = suppressSubSchemaRetrieval; |
58 |
| - } |
59 |
| - |
60 |
| - protected String getSchemaPath() { |
61 |
| - return schemaPath; |
62 |
| - } |
63 |
| - |
64 |
| - public JsonNode getSchemaNode() { |
65 |
| - return schemaNode; |
66 |
| - } |
67 |
| - |
68 |
| - protected JsonSchema getParentSchema() { |
69 |
| - return parentSchema; |
70 |
| - } |
71 |
| - |
72 |
| - protected JsonSchema fetchSubSchemaNode(ValidationContext validationContext) { |
73 |
| - return suppressSubSchemaRetrieval ? null : obtainSubSchemaNode(schemaNode, validationContext); |
74 |
| - } |
75 |
| - |
76 |
| - |
77 |
| - private static JsonSchema obtainSubSchemaNode(final JsonNode schemaNode, final ValidationContext validationContext){ |
78 |
| - final JsonNode node = schemaNode.get("id"); |
79 |
| - if(node == null) return null; |
80 |
| - if(node.equals(schemaNode.get("$schema"))) return null; |
81 |
| - |
82 |
| - final String text = node.textValue(); |
83 |
| - if (text == null) { |
84 |
| - return null; |
85 |
| - } |
86 |
| - else { |
87 |
| - final URI uri; |
88 |
| - try { |
89 |
| - uri = validationContext.getURIFactory().create(node.textValue()); |
90 |
| - } catch (IllegalArgumentException e) { |
91 |
| - return null; |
92 |
| - } |
93 |
| - return validationContext.getJsonSchemaFactory().getSchema(uri, validationContext.getConfig()); |
94 |
| - } |
95 |
| - } |
96 |
| - |
97 |
| - public Set<ValidationMessage> validate(JsonNode node) { |
98 |
| - return validate(node, node, AT_ROOT); |
99 |
| - } |
100 |
| - |
101 |
| - protected boolean equals(double n1, double n2) { |
102 |
| - return Math.abs(n1 - n2) < 1e-12; |
103 |
| - } |
104 |
| - |
105 |
| - protected boolean greaterThan(double n1, double n2) { |
106 |
| - return n1 - n2 > 1e-12; |
107 |
| - } |
108 |
| - |
109 |
| - protected boolean lessThan(double n1, double n2) { |
110 |
| - return n1 - n2 < -1e-12; |
111 |
| - } |
112 |
| - |
113 |
| - protected void parseErrorCode(String errorCodeKey) { |
114 |
| - JsonNode errorCodeNode = getParentSchema().getSchemaNode().get(errorCodeKey); |
115 |
| - if (errorCodeNode != null && errorCodeNode.isTextual()) { |
116 |
| - String errorCodeText = errorCodeNode.asText(); |
117 |
| - if (StringUtils.isNotBlank(errorCodeText)) { |
118 |
| - errorMessageType = CustomErrorMessageType.of(errorCodeText); |
119 |
| - } |
120 |
| - } |
121 |
| - } |
122 |
| - |
123 |
| - |
124 |
| - protected ValidationMessage buildValidationMessage(String at, String... arguments) { |
125 |
| - return ValidationMessage.of(getValidatorType().getValue(), errorMessageType, at, arguments); |
126 |
| - } |
127 |
| - |
128 |
| - protected void debug(Logger logger, JsonNode node, JsonNode rootNode, String at) { |
129 |
| - if (logger.isDebugEnabled()) { |
130 |
| - logger.debug("validate( " + node + ", " + rootNode + ", " + at + ")"); |
131 |
| - } |
132 |
| - } |
133 |
| - |
134 |
| - protected ValidatorTypeCode getValidatorType() { |
135 |
| - return validatorType; |
136 |
| - } |
137 |
| - |
138 |
| - protected String getNodeFieldType() { |
139 |
| - JsonNode typeField = this.getParentSchema().getSchemaNode().get("type"); |
140 |
| - if (typeField != null) { |
141 |
| - return typeField.asText(); |
142 |
| - } |
143 |
| - return null; |
144 |
| - } |
145 |
| -} |
| 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 java.net.URI; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import org.apache.commons.lang3.StringUtils; |
| 23 | +import org.slf4j.Logger; |
| 24 | + |
| 25 | +import com.fasterxml.jackson.databind.JsonNode; |
| 26 | + |
| 27 | +public abstract class BaseJsonValidator implements JsonValidator { |
| 28 | + private String schemaPath; |
| 29 | + private JsonNode schemaNode; |
| 30 | + private JsonSchema parentSchema; |
| 31 | + private boolean suppressSubSchemaRetrieval; |
| 32 | + private ValidatorTypeCode validatorType; |
| 33 | + private ErrorMessageType errorMessageType; |
| 34 | + /** |
| 35 | + * SchemaValidatorsConfig can only get and set in validationContext |
| 36 | + */ |
| 37 | + protected SchemaValidatorsConfig config; |
| 38 | + protected final boolean failFast; |
| 39 | + |
| 40 | + /** |
| 41 | + * ThreadLocal to allow to pass state in recursive validator calls |
| 42 | + */ |
| 43 | + protected final static ThreadLocal<ValidatorState> validatorState = new ThreadLocal<ValidatorState>(); |
| 44 | + |
| 45 | + public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, |
| 46 | + ValidatorTypeCode validatorType, ValidationContext validationContext) { |
| 47 | + this(schemaPath, schemaNode, parentSchema, validatorType, false , |
| 48 | + validationContext.getConfig() != null && validationContext.getConfig().isFailFast()); |
| 49 | + this.config = validationContext.getConfig() == null ? new SchemaValidatorsConfig() : validationContext.getConfig(); |
| 50 | + } |
| 51 | + |
| 52 | + public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, |
| 53 | + ValidatorTypeCode validatorType, boolean suppressSubSchemaRetrieval, boolean failFast) { |
| 54 | + |
| 55 | + this.errorMessageType = validatorType; |
| 56 | + this.schemaPath = schemaPath; |
| 57 | + this.schemaNode = schemaNode; |
| 58 | + this.parentSchema = parentSchema; |
| 59 | + this.validatorType = validatorType; |
| 60 | + this.suppressSubSchemaRetrieval = suppressSubSchemaRetrieval; |
| 61 | + this.failFast = failFast; |
| 62 | + } |
| 63 | + |
| 64 | + protected String getSchemaPath() { |
| 65 | + return schemaPath; |
| 66 | + } |
| 67 | + |
| 68 | + public JsonNode getSchemaNode() { |
| 69 | + return schemaNode; |
| 70 | + } |
| 71 | + |
| 72 | + protected JsonSchema getParentSchema() { |
| 73 | + return parentSchema; |
| 74 | + } |
| 75 | + |
| 76 | + protected JsonSchema fetchSubSchemaNode(ValidationContext validationContext) { |
| 77 | + return suppressSubSchemaRetrieval ? null : obtainSubSchemaNode(schemaNode, validationContext); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + private static JsonSchema obtainSubSchemaNode(final JsonNode schemaNode, final ValidationContext validationContext){ |
| 82 | + final JsonNode node = schemaNode.get("id"); |
| 83 | + if(node == null) return null; |
| 84 | + if(node.equals(schemaNode.get("$schema"))) return null; |
| 85 | + |
| 86 | + final String text = node.textValue(); |
| 87 | + if (text == null) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + else { |
| 91 | + final URI uri; |
| 92 | + try { |
| 93 | + uri = validationContext.getURIFactory().create(node.textValue()); |
| 94 | + } catch (IllegalArgumentException e) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + return validationContext.getJsonSchemaFactory().getSchema(uri, validationContext.getConfig()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public Set<ValidationMessage> validate(JsonNode node) { |
| 102 | + return validate(node, node, AT_ROOT); |
| 103 | + } |
| 104 | + |
| 105 | + protected boolean equals(double n1, double n2) { |
| 106 | + return Math.abs(n1 - n2) < 1e-12; |
| 107 | + } |
| 108 | + |
| 109 | + protected boolean greaterThan(double n1, double n2) { |
| 110 | + return n1 - n2 > 1e-12; |
| 111 | + } |
| 112 | + |
| 113 | + protected boolean lessThan(double n1, double n2) { |
| 114 | + return n1 - n2 < -1e-12; |
| 115 | + } |
| 116 | + |
| 117 | + protected void parseErrorCode(String errorCodeKey) { |
| 118 | + JsonNode errorCodeNode = getParentSchema().getSchemaNode().get(errorCodeKey); |
| 119 | + if (errorCodeNode != null && errorCodeNode.isTextual()) { |
| 120 | + String errorCodeText = errorCodeNode.asText(); |
| 121 | + if (StringUtils.isNotBlank(errorCodeText)) { |
| 122 | + errorMessageType = CustomErrorMessageType.of(errorCodeText); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + protected ValidationMessage buildValidationMessage(String at, String... arguments) { |
| 129 | + final ValidationMessage message = ValidationMessage.of(getValidatorType().getValue(), errorMessageType, at, arguments); |
| 130 | + if (failFast) { |
| 131 | + throw new JsonSchemaException(message); |
| 132 | + } |
| 133 | + return message; |
| 134 | + } |
| 135 | + |
| 136 | + protected void debug(Logger logger, JsonNode node, JsonNode rootNode, String at) { |
| 137 | + if (logger.isDebugEnabled()) { |
| 138 | + logger.debug("validate( " + node + ", " + rootNode + ", " + at + ")"); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + protected ValidatorTypeCode getValidatorType() { |
| 143 | + return validatorType; |
| 144 | + } |
| 145 | + |
| 146 | + protected String getNodeFieldType() { |
| 147 | + JsonNode typeField = this.getParentSchema().getSchemaNode().get("type"); |
| 148 | + if (typeField != null) { |
| 149 | + return typeField.asText(); |
| 150 | + } |
| 151 | + return null; |
| 152 | + } |
| 153 | +} |
0 commit comments