Skip to content

Commit 412118d

Browse files
authored
feat(v2): Validation failures return 400s (#1489)
1 parent 6141243 commit 412118d

File tree

27 files changed

+1176
-76
lines changed

27 files changed

+1176
-76
lines changed

docs/utilities/validation.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ We support JSON schema version 4, 6, 7 and 201909 (from [jmespath-jackson librar
156156

157157
`@Validation` annotation is used to validate either inbound events or functions' response.
158158

159-
It will fail fast with `ValidationException` if an event or response doesn't conform with given JSON Schema.
159+
It will fail fast if an event or response doesn't conform with given JSON Schema. For most type of events a `ValidationException` will be thrown.
160+
For API gateway events associated with REST APIs and HTTP APIs - `APIGatewayProxyRequestEvent` and `APIGatewayV2HTTPEvent` - the `@Validation`
161+
annotation will build and return a custom 400 / "Bad Request" response, with a body containing the validation errors. This saves you from having
162+
to catch the validation exception and map it back to a meaningful user error yourself.
160163

161164
While it is easier to specify a json schema file in the classpath (using the notation `"classpath:/path/to/schema.json"`), you can also provide a JSON String containing the schema.
162165

powertools-e2e-tests/handlers/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<module>metrics</module>
3333
<module>idempotency</module>
3434
<module>parameters</module>
35+
<module>validation</module>
3536
</modules>
3637

3738
<dependencyManagement>
@@ -83,6 +84,11 @@
8384
<artifactId>powertools-batch</artifactId>
8485
<version>${lambda.powertools.version}</version>
8586
</dependency>
87+
<dependency>
88+
<groupId>software.amazon.lambda</groupId>
89+
<artifactId>powertools-validation</artifactId>
90+
<version>${lambda.powertools.version}</version>
91+
</dependency>
8692
<dependency>
8793
<groupId>com.amazonaws</groupId>
8894
<artifactId>aws-lambda-java-core</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>software.amazon.lambda</groupId>
7+
<artifactId>e2e-test-handlers-parent</artifactId>
8+
<version>1.0.0</version>
9+
</parent>
10+
11+
<artifactId>e2e-test-handler-validation-alb-event</artifactId>
12+
<packaging>jar</packaging>
13+
<name>A Lambda function using Powertools for AWS Lambda (Java) validation</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>software.amazon.lambda</groupId>
18+
<artifactId>powertools-validation</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.amazonaws</groupId>
22+
<artifactId>aws-lambda-java-events</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.aspectj</groupId>
26+
<artifactId>aspectjrt</artifactId>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>dev.aspectj</groupId>
34+
<artifactId>aspectj-maven-plugin</artifactId>
35+
<configuration>
36+
<source>${maven.compiler.source}</source>
37+
<target>${maven.compiler.target}</target>
38+
<complianceLevel>${maven.compiler.target}</complianceLevel>
39+
<aspectLibraries>
40+
<aspectLibrary>
41+
<groupId>software.amazon.lambda</groupId>
42+
<artifactId>powertools-validation</artifactId>
43+
</aspectLibrary>
44+
</aspectLibraries>
45+
</configuration>
46+
<executions>
47+
<execution>
48+
<goals>
49+
<goal>compile</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
<plugin>
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-shade-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
15+
package software.amazon.lambda.powertools.e2e;
16+
17+
import com.amazonaws.services.lambda.runtime.Context;
18+
import com.amazonaws.services.lambda.runtime.RequestHandler;
19+
import com.amazonaws.services.lambda.runtime.events.ApplicationLoadBalancerRequestEvent;
20+
import com.amazonaws.services.lambda.runtime.events.ApplicationLoadBalancerResponseEvent;
21+
import com.amazonaws.services.lambda.runtime.events.SQSBatchResponse;
22+
23+
import software.amazon.lambda.powertools.validation.Validation;
24+
25+
public class Function
26+
implements RequestHandler<ApplicationLoadBalancerRequestEvent, ApplicationLoadBalancerResponseEvent> {
27+
@Validation(inboundSchema = "classpath:/validation/inbound_schema.json", outboundSchema = "classpath:/validation/outbound_schema.json")
28+
public ApplicationLoadBalancerResponseEvent handleRequest(ApplicationLoadBalancerRequestEvent input,
29+
Context context) {
30+
ApplicationLoadBalancerResponseEvent response = new ApplicationLoadBalancerResponseEvent();
31+
response.setBody(input.getBody());
32+
response.setStatusCode(200);
33+
response.setIsBase64Encoded(false);
34+
return response;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration>
3+
<Appenders>
4+
<Console name="JsonAppender" target="SYSTEM_OUT">
5+
<JsonTemplateLayout eventTemplateUri="classpath:LambdaJsonLayout.json" />
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="INFO">
10+
<AppenderRef ref="JsonAppender"/>
11+
</Root>
12+
<Logger name="JsonLogger" level="INFO" additivity="false">
13+
<AppenderRef ref="JsonAppender"/>
14+
</Logger>
15+
</Loggers>
16+
</Configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"$id": "http://example.com/product.json",
4+
"type": "object",
5+
"title": "Product schema",
6+
"description": "JSON schema to validate Products",
7+
"default": {},
8+
"examples": [
9+
{
10+
"id": 43242,
11+
"name": "FooBar XY",
12+
"price": 258
13+
}
14+
],
15+
"required": [
16+
"price"
17+
],
18+
"properties": {
19+
"price": {
20+
"$id": "#/properties/price",
21+
"type": "number",
22+
"title": "Price of the product",
23+
"description": "Positive price of the product",
24+
"default": 0,
25+
"exclusiveMinimum": 0,
26+
"examples": [
27+
258.99
28+
]
29+
}
30+
},
31+
"additionalProperties": true
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"$id": "http://example.com/product.json",
4+
"type": "object",
5+
"title": "Product schema",
6+
"description": "JSON schema to validate Products",
7+
"default": {},
8+
"examples": [
9+
{
10+
"id": 43242,
11+
"name": "FooBar XY",
12+
"price": 258
13+
}
14+
],
15+
"required": [
16+
"price"
17+
],
18+
"properties": {
19+
"price": {
20+
"$id": "#/properties/price",
21+
"type": "number",
22+
"title": "Price of the product",
23+
"description": "Positive price of the product",
24+
"default": 0,
25+
"exclusiveMaximum": 1000,
26+
"examples": [
27+
258.99
28+
]
29+
}
30+
},
31+
"additionalProperties": true
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>software.amazon.lambda</groupId>
7+
<artifactId>e2e-test-handlers-parent</artifactId>
8+
<version>1.0.0</version>
9+
</parent>
10+
11+
<artifactId>e2e-test-handler-validation-apigw-event</artifactId>
12+
<packaging>jar</packaging>
13+
<name>A Lambda function using Powertools for AWS Lambda (Java) validation</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>software.amazon.lambda</groupId>
18+
<artifactId>powertools-validation</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.amazonaws</groupId>
22+
<artifactId>aws-lambda-java-events</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.aspectj</groupId>
26+
<artifactId>aspectjrt</artifactId>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>dev.aspectj</groupId>
34+
<artifactId>aspectj-maven-plugin</artifactId>
35+
<configuration>
36+
<source>${maven.compiler.source}</source>
37+
<target>${maven.compiler.target}</target>
38+
<complianceLevel>${maven.compiler.target}</complianceLevel>
39+
<aspectLibraries>
40+
<aspectLibrary>
41+
<groupId>software.amazon.lambda</groupId>
42+
<artifactId>powertools-validation</artifactId>
43+
</aspectLibrary>
44+
</aspectLibraries>
45+
</configuration>
46+
<executions>
47+
<execution>
48+
<goals>
49+
<goal>compile</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
<plugin>
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-shade-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
15+
package software.amazon.lambda.powertools.e2e;
16+
17+
import com.amazonaws.services.lambda.runtime.Context;
18+
import com.amazonaws.services.lambda.runtime.RequestHandler;
19+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
20+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
21+
22+
import software.amazon.lambda.powertools.validation.Validation;
23+
24+
public class Function implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
25+
@Validation(inboundSchema = "classpath:/validation/inbound_schema.json", outboundSchema = "classpath:/validation/outbound_schema.json")
26+
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
27+
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
28+
response.setBody(input.getBody());
29+
response.setStatusCode(200);
30+
response.setIsBase64Encoded(false);
31+
return response;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration>
3+
<Appenders>
4+
<Console name="JsonAppender" target="SYSTEM_OUT">
5+
<JsonTemplateLayout eventTemplateUri="classpath:LambdaJsonLayout.json" />
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="INFO">
10+
<AppenderRef ref="JsonAppender"/>
11+
</Root>
12+
<Logger name="JsonLogger" level="INFO" additivity="false">
13+
<AppenderRef ref="JsonAppender"/>
14+
</Logger>
15+
</Loggers>
16+
</Configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"$id": "http://example.com/product.json",
4+
"type": "object",
5+
"title": "Product schema",
6+
"description": "JSON schema to validate Products",
7+
"default": {},
8+
"examples": [
9+
{
10+
"id": 43242,
11+
"name": "FooBar XY",
12+
"price": 258
13+
}
14+
],
15+
"required": [
16+
"price"
17+
],
18+
"properties": {
19+
"price": {
20+
"$id": "#/properties/price",
21+
"type": "number",
22+
"title": "Price of the product",
23+
"description": "Positive price of the product",
24+
"default": 0,
25+
"exclusiveMinimum": 0,
26+
"examples": [
27+
258.99
28+
]
29+
}
30+
},
31+
"additionalProperties": true
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"$id": "http://example.com/product.json",
4+
"type": "object",
5+
"title": "Product schema",
6+
"description": "JSON schema to validate Products",
7+
"default": {},
8+
"examples": [
9+
{
10+
"id": 43242,
11+
"name": "FooBar XY",
12+
"price": 258
13+
}
14+
],
15+
"required": [
16+
"price"
17+
],
18+
"properties": {
19+
"price": {
20+
"$id": "#/properties/price",
21+
"type": "number",
22+
"title": "Price of the product",
23+
"description": "Positive price of the product",
24+
"default": 0,
25+
"exclusiveMaximum": 1000,
26+
"examples": [
27+
258.99
28+
]
29+
}
30+
},
31+
"additionalProperties": true
32+
}

0 commit comments

Comments
 (0)