Skip to content

build(deps): bump com.networknt:json-schema-validator from 1.0.87 to 1.4.3 #1674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions docs/utilities/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ You can validate inbound and outbound events using `@Validation` annotation.

You can also use the `Validator#validate()` methods, if you want more control over the validation process such as handling a validation error.

We support JSON schema version 4, 6, 7 and 201909 (from [jmespath-jackson library](https://github.com/burtcorp/jmespath-java)).
We support JSON schema version 4, 6, 7, 2019-09 and 2020-12 using the [NetworkNT JSON Schema Validator](https://github.com/networknt/json-schema-validator). ([Compatibility with JSON Schema versions](https://github.com/networknt/json-schema-validator/blob/master/doc/compatibility.md)).

The validator is configured to enable format assertions by default even for 2019-09 and 2020-12.

### Validation annotation

Expand Down Expand Up @@ -228,7 +230,8 @@ and [function](https://jmespath.org/tutorial.html#functions) expressions, where


## Change the schema version
By default, powertools-validation is configured with [V7](https://json-schema.org/draft-07/json-schema-release-notes.html).
By default, powertools-validation is configured to use [V7](https://json-schema.org/draft-07/json-schema-release-notes.html) as the default dialect if [`$schema`](https://json-schema.org/understanding-json-schema/reference/schema#schema) is not explicitly specified within the schema. If [`$schema`](https://json-schema.org/understanding-json-schema/reference/schema#schema) is explicitly specified within the schema, the validator will use the specified dialect.

You can use the `ValidationConfig` to change that behaviour.

=== "Handler with custom schema version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void test_invalidInboundSQSEvent() throws IOException {
// THEN
// invocation should fail inbound validation and return an error message
JsonNode validJsonNode = objectMapper.readTree(invocationResult.getResult());
assertThat(validJsonNode.get("errorMessage").asText()).contains("$.price: is missing but it is required");
assertThat(validJsonNode.get("errorMessage").asText()).contains(": required property 'price' not found");
}

@Test
Expand All @@ -99,6 +99,6 @@ void test_invalidOutboundSQSEvent() throws IOException {
// THEN
// invocation should fail outbound validation and return 400
JsonNode validJsonNode = objectMapper.readTree(invocationResult.getResult());
assertThat(validJsonNode.get("errorMessage").asText()).contains("$.price: must have an exclusive maximum value of 1000");
assertThat(validJsonNode.get("errorMessage").asText()).contains("/price: must have an exclusive maximum value of 1000");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void test_invalidInboundApiGWEvent() throws IOException {
// invocation should fail inbound validation and return 400
JsonNode validJsonNode = objectMapper.readTree(invocationResult.getResult());
assertThat(validJsonNode.get("statusCode").asInt()).isEqualTo(400);
assertThat(validJsonNode.get("body").asText()).contains("$.price: is missing but it is required");
assertThat(validJsonNode.get("body").asText()).contains(": required property 'price' not found");
}

@Test
Expand All @@ -102,6 +102,6 @@ void test_invalidOutboundApiGWEvent() throws IOException {
JsonNode validJsonNode = objectMapper.readTree(invocationResult.getResult());
assertThat(validJsonNode.get("statusCode").asInt()).isEqualTo(400);
assertThat(validJsonNode.get("body").asText())
.contains("$.price: must have an exclusive maximum value of 1000");
.contains("/price: must have an exclusive maximum value of 1000");
}
}
2 changes: 1 addition & 1 deletion powertools-validation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.0.87</version>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ public SpecVersion.VersionFlag getSchemaVersion() {
}

/**
* Set the version of the json schema specifications (default is V7)
* Set the version of the json schema specifications to use if $schema is not
* explicitly specified within the schema (default is V7). If $schema is
* explicitly specified within the schema is explicitly specified within the
* schema, the validator will use the specified dialect.
*
* @param version May be V4, V6, V7, V201909 or V202012
* @see <a href=
* "https://json-schema.org/understanding-json-schema/reference/schema#declaring-a-dialect">Declaring
* a Dialect</a>
*/
public void setSchemaVersion(SpecVersion.VersionFlag version) {
if (version != jsonSchemaVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.NullNode;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.SchemaLocation;
import com.networknt.schema.SchemaValidatorsConfig;
import com.networknt.schema.ValidationMessage;
import com.networknt.schema.uri.URITranslator;
import io.burt.jmespath.Expression;
import java.io.ByteArrayOutputStream;
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 software.amazon.lambda.powertools.validation.internal.ValidationAspect;

/**
* Validation utility, used to manually validate Json against Json Schema
Expand Down Expand Up @@ -255,27 +253,26 @@ public static JsonSchema getJsonSchema(String schema, boolean validateSchema) {

private static JsonSchema createJsonSchema(String schema) {
JsonSchema jsonSchema;
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true)
.preloadJsonSchemaRefMaxNestingDepth(10).build();
if (schema.startsWith(CLASSPATH)) {
String filePath = schema.substring(CLASSPATH.length());
try (InputStream schemaStream = ValidationAspect.class.getResourceAsStream(filePath)) {

SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.addUriTranslator(URITranslator.prefix("https://json-schema.org", "resource:"));

jsonSchema = ValidationConfig.get().getFactory().getSchema(schemaStream, config);
try {
jsonSchema = ValidationConfig.get().getFactory().getSchema(SchemaLocation.of(schema), config);
} catch (Exception e) {
String filePath = schema.substring(CLASSPATH.length());
throw new IllegalArgumentException(
"'" + schema + "' is invalid, verify '" + filePath + "' is in your classpath");
"'" + schema + "' is invalid, verify '" + filePath + "' is in your classpath", e);
}
} else {
jsonSchema = ValidationConfig.get().getFactory().getSchema(schema);
jsonSchema = ValidationConfig.get().getFactory().getSchema(schema, config);
}

return jsonSchema;
}

private static void validateSchema(String schema, JsonSchema jsonSchema) {
String schemaId = ValidationConfig.get().getSchemaVersion().getId().replace("https://json-schema.org", "");
String schemaId = jsonSchema.getValidationContext().getMetaSchema().getIri()
.replace("https://json-schema.org", "").replace("http://json-schema.org", "");
try {
validate(jsonSchema.getSchemaNode(),
getJsonSchema(CLASSPATH + schemaId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void testLoadSchemaV7OK() {
ValidationConfig.get().setSchemaVersion(SpecVersion.VersionFlag.V7);
JsonSchema jsonSchema = getJsonSchema("classpath:/schema_v7.json", true);
assertThat(jsonSchema).isNotNull();
assertThat(jsonSchema.getCurrentUri()).asString().isEqualTo("http://example.com/product.json");
assertThat(jsonSchema.getId()).isEqualTo("http://example.com/product.json");
}

@Test
Expand All @@ -57,7 +57,7 @@ public void testLoadSchemaV7KO() {
assertThatThrownBy(() -> getJsonSchema("classpath:/schema_v7_ko.json", true))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(
"The schema classpath:/schema_v7_ko.json is not valid, it does not respect the specification /draft-07/schema");
"The schema classpath:/schema_v7_ko.json is not valid, it does not respect the specification /draft-07/schema#");
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion powertools-validation/src/test/resources/schema_v7.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/product.json",
"type": "object",
"title": "Product schema",
Expand Down
2 changes: 1 addition & 1 deletion powertools-validation/src/test/resources/schema_v7_ko.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Product schema",
"description": "JSON schema to validate Products",
Expand Down
Loading