forked from aws-powertools/powertools-lambda-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidationUtils.java
293 lines (263 loc) · 12.2 KB
/
ValidationUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package software.amazon.lambda.powertools.validation;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import com.amazonaws.services.lambda.runtime.serialization.PojoSerializer;
import com.amazonaws.services.lambda.runtime.serialization.events.LambdaEventSerializers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.NullNode;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.ValidationMessage;
import io.burt.jmespath.Expression;
import software.amazon.lambda.powertools.validation.internal.ValidationAspect;
/**
* Validation utility, used to manually validate Json against Json Schema
*/
public class ValidationUtils {
private static final String CLASSPATH = "classpath:";
private static final ConcurrentHashMap<String, JsonSchema> schemas = new ConcurrentHashMap<>();
private ValidationUtils() {
}
/**
* Validate part of a json object against a json schema
*
* @param obj The object to validate
* @param jsonSchema The schema used to validate: either the schema itself or a path to file in the classpath: "classpath:/path/to/schema.json"
* @param envelope a path to a sub object within obj
*/
public static void validate(Object obj, String jsonSchema, String envelope) throws ValidationException {
validate(obj, getJsonSchema(jsonSchema), envelope);
}
/**
* Validate part of a json object against a json schema
*
* @param obj The object to validate
* @param jsonSchema The schema used to validate
* @param envelope a path to a sub object within obj
*/
public static void validate(Object obj, JsonSchema jsonSchema, String envelope) throws ValidationException {
if (envelope == null || envelope.isEmpty()) {
validate(obj, jsonSchema);
return;
}
JsonNode subNode;
try {
PojoSerializer pojoSerializer = LambdaEventSerializers.serializerFor(obj.getClass(), ClassLoader.getSystemClassLoader());
ByteArrayOutputStream out = new ByteArrayOutputStream();
pojoSerializer.toJson(obj, out);
JsonNode jsonNode = ValidationConfig.get().getObjectMapper().readTree(out.toString("UTF-8"));
Expression<JsonNode> expression = ValidationConfig.get().getJmesPath().compile(envelope);
subNode = expression.search(jsonNode);
if (subNode == null || subNode instanceof NullNode) {
throw new ValidationException("Not found");
}
} catch (Exception e) {
throw new ValidationException("Cannot find envelope <"+envelope+"> in the object <"+obj+">", e);
}
if (subNode.getNodeType() == JsonNodeType.ARRAY) {
subNode.forEach(jsonNode -> validate(jsonNode, jsonSchema));
} else if (subNode.getNodeType() == JsonNodeType.OBJECT) {
validate(subNode, jsonSchema);
} else if (subNode.getNodeType() == JsonNodeType.STRING) {
// try to validate as json string
try {
validate(subNode.asText(), jsonSchema);
} catch (ValidationException e) {
throw new ValidationException("Invalid format for '" + envelope + "': 'STRING' and no JSON found in it.");
}
} else {
throw new ValidationException("Invalid format for '" + envelope + "': '" + subNode.getNodeType() + "'");
}
}
/**
* Validate a json object against a json schema
*
* @param obj Object to validate
* @param jsonSchema The schema used to validate: either the schema itself or a path to file in the classpath: "classpath:/path/to/schema.json"
* @throws ValidationException if validation fails
*/
public static void validate(Object obj, String jsonSchema) throws ValidationException {
validate(obj, getJsonSchema(jsonSchema));
}
/**
* Validate a json object against a json schema
*
* @param obj Object to validate
* @param jsonSchema The schema used to validate
* @throws ValidationException if validation fails
*/
public static void validate(Object obj, JsonSchema jsonSchema) throws ValidationException {
JsonNode jsonNode;
try {
jsonNode = ValidationConfig.get().getObjectMapper().valueToTree(obj);
} catch (Exception e) {
throw new ValidationException("Object <"+obj+"> is not valid against the schema provided", e);
}
validate(jsonNode, jsonSchema);
}
/**
* Validate a json object (in string format) against a json schema
*
* @param json Json in string format
* @param jsonSchema The schema used to validate: either the schema itself or a path to file in the classpath: "classpath:/path/to/schema.json"
* @throws ValidationException if validation fails
*/
public static void validate(String json, String jsonSchema) throws ValidationException {
validate(json, getJsonSchema(jsonSchema));
}
/**
* Validate a json object (in string format) against a json schema
*
* @param json json in string format
* @param jsonSchema the schema used to validate json string
* @throws ValidationException if validation fails
*/
public static void validate(String json, JsonSchema jsonSchema) throws ValidationException {
JsonNode jsonNode;
try {
jsonNode = ValidationConfig.get().getObjectMapper().readTree(json);
} catch (Exception e) {
throw new ValidationException("Json <"+json+"> is not valid against the schema provided", e);
}
validate(jsonNode, jsonSchema);
}
/**
* Validate a json object (in map format) against a json schema
*
* @param map Map to be transformed in json and validated against the schema
* @param jsonSchema The schema used to validate: either the schema itself or a path to file in the classpath: "classpath:/path/to/schema.json"
* @throws ValidationException if validation fails
*/
public static void validate(Map<String, Object> map, String jsonSchema) throws ValidationException {
validate(map, getJsonSchema(jsonSchema));
}
/**
* Validate a json object (in map format) against a json schema
*
* @param map Map to be transformed in json and validated against the schema
* @param jsonSchema the schema used to validate json map
* @throws ValidationException if validation fails
*/
public static void validate(Map<String, Object> map, JsonSchema jsonSchema) throws ValidationException {
JsonNode jsonNode;
try {
jsonNode = ValidationConfig.get().getObjectMapper().valueToTree(map);
} catch (Exception e) {
throw new ValidationException("Map <"+map+"> cannot be converted to json for validation", e);
}
validate(jsonNode, jsonSchema);
}
/**
* Validate a json object (in JsonNode format) against a json schema.<br>
* Perform the actual validation.
*
* @param jsonNode Json to be validated against the schema
* @param jsonSchema The schema used to validate: either the schema itself or a path to file in the classpath: "classpath:/path/to/schema.json"
* @throws ValidationException if validation fails
*/
public static void validate(JsonNode jsonNode, String jsonSchema) throws ValidationException {
validate(jsonNode, getJsonSchema(jsonSchema));
}
/**
* Validate a json object (in JsonNode format) against a json schema.<br>
* Perform the actual validation.
*
* @param jsonNode json to be validated against the schema
* @param jsonSchema the schema to validate json node
* @throws ValidationException if validation fails
*/
public static void validate(JsonNode jsonNode, JsonSchema jsonSchema) throws ValidationException {
Set<ValidationMessage> validationMessages = jsonSchema.validate(jsonNode);
if (!validationMessages.isEmpty()) {
String message;
try {
message = ValidationConfig.get().getObjectMapper().writeValueAsString(new ValidationErrors(validationMessages));
} catch (JsonProcessingException e) {
message = validationMessages.stream().map(ValidationMessage::getMessage).collect(Collectors.joining(", "));
}
throw new ValidationException(message);
}
}
/**
* Retrieve {@link JsonSchema} from string (either the schema itself, either from the classpath).<br/>
* No validation of the schema will be performed (equivalent to <pre>getJsonSchema(schema, false)</pre><br/>
* Store it in memory to avoid reloading it.<br/>
*
* @param schema either the schema itself of a "classpath:/path/to/schema.json"
* @return the loaded json schema
*/
public static JsonSchema getJsonSchema(String schema) {
return getJsonSchema(schema, false);
}
/**
* Retrieve {@link JsonSchema} from string (either the schema itself, either from the classpath).<br/>
* Optional: validate the schema against the version specifications.<br/>
* Store it in memory to avoid reloading it.<br/>
*
* @param schema either the schema itself of a "classpath:/path/to/schema.json"
* @param validateSchema specify if the schema itself must be validated against specifications
* @return the loaded json schema
*/
public static JsonSchema getJsonSchema(String schema, boolean validateSchema) {
JsonSchema jsonSchema = schemas.get(schema);
if (jsonSchema != null) {
return jsonSchema;
}
if (schema.startsWith(CLASSPATH)) {
String filePath = schema.substring(CLASSPATH.length());
try (InputStream schemaStream = ValidationAspect.class.getResourceAsStream(filePath)) {
if (schemaStream == null) {
throw new IllegalArgumentException("'" + schema + "' is invalid, verify '" + filePath + "' is in your classpath");
}
jsonSchema = ValidationConfig.get().getFactory().getSchema(schemaStream);
} catch (IOException e) {
throw new IllegalArgumentException("'" + schema + "' is invalid, verify '" + filePath + "' is in your classpath");
}
} else {
jsonSchema = ValidationConfig.get().getFactory().getSchema(schema);
}
if (validateSchema) {
String version = ValidationConfig.get().getSchemaVersion().toString();
try {
validate(jsonSchema.getSchemaNode(),
getJsonSchema("classpath:/schemas/meta_schema_" + version));
} catch (ValidationException ve) {
throw new IllegalArgumentException("The schema " + schema + " is not valid, it does not respect the specification " + version, ve);
}
}
schemas.put(schema, jsonSchema);
return jsonSchema;
}
/**
*
*/
public static class ValidationErrors {
private final Set<ValidationMessage> validationErrors;
private ValidationErrors(Set<ValidationMessage> validationErrors) {
this.validationErrors = validationErrors;
}
public Set<ValidationMessage> getValidationErrors() {
return Collections.unmodifiableSet(validationErrors);
}
}
}