|
13 | 13 | */
|
14 | 14 | package software.amazon.lambda.powertools.validation;
|
15 | 15 |
|
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Set; |
| 20 | +import java.util.concurrent.ConcurrentHashMap; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
16 | 23 | import com.fasterxml.jackson.core.JsonProcessingException;
|
17 | 24 | import com.fasterxml.jackson.databind.JsonNode;
|
18 | 25 | import com.fasterxml.jackson.databind.node.JsonNodeType;
|
|
21 | 28 | import io.burt.jmespath.Expression;
|
22 | 29 | import software.amazon.lambda.powertools.validation.internal.ValidationAspect;
|
23 | 30 |
|
24 |
| -import java.io.InputStream; |
25 |
| -import java.util.Map; |
26 |
| -import java.util.Set; |
27 |
| -import java.util.concurrent.ConcurrentHashMap; |
28 |
| -import java.util.stream.Collectors; |
29 |
| - |
30 | 31 | /**
|
31 | 32 | * Validation utility, used to manually validate Json against Json Schema
|
32 | 33 | */
|
@@ -229,31 +230,37 @@ public static JsonSchema getJsonSchema(String schema) {
|
229 | 230 | public static JsonSchema getJsonSchema(String schema, boolean validateSchema) {
|
230 | 231 | JsonSchema jsonSchema = schemas.get(schema);
|
231 | 232 |
|
232 |
| - if (jsonSchema == null) { |
233 |
| - if (schema.startsWith(CLASSPATH)) { |
234 |
| - String filePath = schema.substring(CLASSPATH.length()); |
235 |
| - InputStream schemaStream = ValidationAspect.class.getResourceAsStream(filePath); |
| 233 | + if (jsonSchema != null) { |
| 234 | + return jsonSchema; |
| 235 | + } |
| 236 | + |
| 237 | + if (schema.startsWith(CLASSPATH)) { |
| 238 | + String filePath = schema.substring(CLASSPATH.length()); |
| 239 | + try (InputStream schemaStream = ValidationAspect.class.getResourceAsStream(filePath)) { |
236 | 240 | if (schemaStream == null) {
|
237 | 241 | throw new IllegalArgumentException("'" + schema + "' is invalid, verify '" + filePath + "' is in your classpath");
|
238 | 242 | }
|
| 243 | + |
239 | 244 | jsonSchema = ValidationConfig.get().getFactory().getSchema(schemaStream);
|
240 |
| - } else { |
241 |
| - jsonSchema = ValidationConfig.get().getFactory().getSchema(schema); |
| 245 | + } catch (IOException e) { |
| 246 | + throw new IllegalArgumentException("'" + schema + "' is invalid, verify '" + filePath + "' is in your classpath"); |
242 | 247 | }
|
| 248 | + } else { |
| 249 | + jsonSchema = ValidationConfig.get().getFactory().getSchema(schema); |
| 250 | + } |
243 | 251 |
|
244 |
| - if (validateSchema) { |
245 |
| - String version = ValidationConfig.get().getSchemaVersion().toString(); |
246 |
| - try { |
247 |
| - validate(jsonSchema.getSchemaNode(), |
248 |
| - getJsonSchema("classpath:/schemas/meta_schema_" + version)); |
249 |
| - } catch (ValidationException ve) { |
250 |
| - throw new IllegalArgumentException("The schema " + schema + " is not valid, it does not respect the specification " + version, ve); |
251 |
| - } |
| 252 | + if (validateSchema) { |
| 253 | + String version = ValidationConfig.get().getSchemaVersion().toString(); |
| 254 | + try { |
| 255 | + validate(jsonSchema.getSchemaNode(), |
| 256 | + getJsonSchema("classpath:/schemas/meta_schema_" + version)); |
| 257 | + } catch (ValidationException ve) { |
| 258 | + throw new IllegalArgumentException("The schema " + schema + " is not valid, it does not respect the specification " + version, ve); |
252 | 259 | }
|
253 |
| - |
254 |
| - schemas.put(schema, jsonSchema); |
255 | 260 | }
|
256 | 261 |
|
| 262 | + schemas.put(schema, jsonSchema); |
| 263 | + |
257 | 264 | return jsonSchema;
|
258 | 265 | }
|
259 | 266 |
|
|
0 commit comments