Skip to content
Open
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
2 changes: 1 addition & 1 deletion docs/utilities/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ and [function](https://jmespath.org/tutorial.html#functions) expressions, where
## Change the schema version
By default, powertools-validation is configured to use [draft 7](https://json-schema.org/draft-07/json-schema-release-notes.html) (`SpecificationVersion.DRAFT_7`) 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.
You can use the `ValidationConfig` to change that behaviour. If `@Validation` sets a `schemaVersion` other than the default `DRAFT_7`, the annotation version takes precedence.

=== "Handler with custom schema version"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package software.amazon.lambda.powertools.validation.internal;

import static com.networknt.schema.SpecificationVersion.DRAFT_2019_09;
import static com.networknt.schema.SpecificationVersion.DRAFT_7;
import static java.nio.charset.StandardCharsets.UTF_8;
import static software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor.placedOnRequestHandler;
import static software.amazon.lambda.powertools.utilities.jmespath.Base64Function.decode;
Expand Down Expand Up @@ -79,7 +79,7 @@ public Object around(ProceedingJoinPoint pjp,
Object[] proceedArgs = pjp.getArgs();
boolean validationNeeded = false;

if (validation.schemaVersion() != DRAFT_2019_09) {
if (validation.schemaVersion() != DRAFT_7) {
ValidationConfig.get().setSchemaVersion(validation.schemaVersion());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.util.stream.Stream;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -110,6 +111,11 @@ void setUp() {
MockitoAnnotations.openMocks(this);
}

@AfterEach
void tearDown() {
ValidationConfig.get().setSchemaVersion(SpecificationVersion.DRAFT_7);
}

@ParameterizedTest
@ArgumentsSource(ResponseEventsArgumentsProvider.class)
void testValidateOutboundJsonSchemaWithExceptions(Object object) throws Throwable {
Expand Down Expand Up @@ -189,6 +195,29 @@ void testValidateOutboundJsonSchema_APIGWV2() throws Throwable {
assertThatNoException().isThrownBy(() -> validationAspect.around(pjp, validation));
}

@Test
void testDefaultAnnotationSchemaVersion_shouldKeepSchemaVersionFromConfig() throws Throwable {
ValidationConfig.get().setSchemaVersion(SpecificationVersion.DRAFT_4);
when(validation.schemaVersion()).thenReturn(SpecificationVersion.DRAFT_7);
when(pjp.getSignature()).thenReturn(signature);
when(pjp.getSignature().getDeclaringType()).thenReturn(Object.class);

validationAspect.around(pjp, validation);

assertThat(ValidationConfig.get().getSchemaVersion()).isEqualTo(SpecificationVersion.DRAFT_4);
}

@Test
void testExplicitAnnotationSchemaVersion_shouldOverrideSchemaVersionFromConfig() throws Throwable {
when(validation.schemaVersion()).thenReturn(SpecificationVersion.DRAFT_2019_09);
when(pjp.getSignature()).thenReturn(signature);
when(pjp.getSignature().getDeclaringType()).thenReturn(Object.class);

validationAspect.around(pjp, validation);

assertThat(ValidationConfig.get().getSchemaVersion()).isEqualTo(SpecificationVersion.DRAFT_2019_09);
}

@Test
void validate_inputOK_schemaInClasspath_shouldValidate() {
GenericSchemaV7APIGatewayProxyRequestEventHandler handler = new GenericSchemaV7APIGatewayProxyRequestEventHandler();
Expand Down
Loading