@@ -8,13 +8,12 @@ This utility provides JSON Schema validation for payloads held within events and
8
8
** Key features**
9
9
10
10
* Validate incoming events and responses
11
- * Built-in validation for most common events (API Gateway, SNS, SQS, ...)
11
+ * Built-in validation for most common events (API Gateway, SNS, SQS, ...) and support for partial batch failures (SQS, Kinesis)
12
12
* JMESPath support validate only a sub part of the event
13
13
14
14
## Install
15
- Depending on your version of Java (either Java 1.8 or 11+), the configuration slightly changes.
16
15
17
- === "Maven Java 11+ "
16
+ === "Maven"
18
17
```xml hl_lines="3-7 16 18 24-27"
19
18
<dependencies >
20
19
...
@@ -58,52 +57,7 @@ Depending on your version of Java (either Java 1.8 or 11+), the configuration sl
58
57
</build >
59
58
```
60
59
61
- === "Maven Java 1.8"
62
-
63
- ```xml hl_lines="3-7 16 18 24-27"
64
- <dependencies>
65
- ...
66
- <dependency>
67
- <groupId>software.amazon.lambda</groupId>
68
- <artifactId>powertools-validation</artifactId>
69
- <version>{{ powertools.version }}</version>
70
- </dependency>
71
- ...
72
- </dependencies>
73
- ...
74
- <!-- configure the aspectj-maven-plugin to compile-time weave (CTW) the aws-lambda-powertools-java aspects into your project -->
75
- <build>
76
- <plugins>
77
- ...
78
- <plugin>
79
- <groupId>org.codehaus.mojo</groupId>
80
- <artifactId>aspectj-maven-plugin</artifactId>
81
- <version>1.14.0</version>
82
- <configuration>
83
- <source>1.8</source>
84
- <target>1.8</target>
85
- <complianceLevel>1.8</complianceLevel>
86
- <aspectLibraries>
87
- <aspectLibrary>
88
- <groupId>software.amazon.lambda</groupId>
89
- <artifactId>powertools-validation</artifactId>
90
- </aspectLibrary>
91
- </aspectLibraries>
92
- </configuration>
93
- <executions>
94
- <execution>
95
- <goals>
96
- <goal>compile</goal>
97
- </goals>
98
- </execution>
99
- </executions>
100
- </plugin>
101
- ...
102
- </plugins>
103
- </build>
104
- ```
105
-
106
- === "Gradle Java 11+"
60
+ === "Gradle"
107
61
108
62
```groovy hl_lines="3 11"
109
63
plugins {
@@ -123,27 +77,6 @@ Depending on your version of Java (either Java 1.8 or 11+), the configuration sl
123
77
targetCompatibility = 11 // or higher
124
78
```
125
79
126
- === "Gradle Java 1.8"
127
-
128
- ```groovy hl_lines="3 11"
129
- plugins {
130
- id 'java'
131
- id 'io.freefair.aspectj.post-compile-weaving' version '6.6.3'
132
- }
133
-
134
- repositories {
135
- mavenCentral()
136
- }
137
-
138
- dependencies {
139
- aspect 'software.amazon.lambda:powertools-validation:{{ powertools.version }}'
140
- }
141
-
142
- sourceCompatibility = 1.8
143
- targetCompatibility = 1.8
144
- ```
145
-
146
-
147
80
## Validating events
148
81
149
82
You can validate inbound and outbound events using ` @Validation ` annotation.
@@ -157,10 +90,15 @@ We support JSON schema version 4, 6, 7 and 201909 (from [jmespath-jackson librar
157
90
` @Validation ` annotation is used to validate either inbound events or functions' response.
158
91
159
92
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.
93
+
160
94
For API gateway events associated with REST APIs and HTTP APIs - ` APIGatewayProxyRequestEvent ` and ` APIGatewayV2HTTPEvent ` - the ` @Validation `
161
95
annotation will build and return a custom 400 / "Bad Request" response, with a body containing the validation errors. This saves you from having
162
96
to catch the validation exception and map it back to a meaningful user error yourself.
163
97
98
+ For SQS and Kinesis events - ` SQSEvent ` and ` KinesisEvent ` - the ` @Validation ` annotation will add the invalid messages
99
+ to the batch item failures list in the response, respectively ` SQSBatchResponse ` and ` StreamsEventResponse `
100
+ and removed from the event so that you do not process them within the handler.
101
+
164
102
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.
165
103
166
104
=== "MyFunctionHandler.java"
@@ -217,31 +155,31 @@ For the following events and responses, the Validator will automatically perform
217
155
218
156
** Events **
219
157
220
- Type of event | Class | Path to content |
221
- ------------------------------------------------- | ------------------------------------------------- | -------------------------------------------------
222
- API Gateway REST | APIGatewayProxyRequestEvent | ` body `
223
- API Gateway HTTP | APIGatewayV2HTTPEvent | ` body `
224
- Application Load Balancer | ApplicationLoadBalancerRequestEvent | ` body `
225
- Cloudformation Custom Resource | CloudFormationCustomResourceEvent | ` resourceProperties `
226
- CloudWatch Logs | CloudWatchLogsEvent | ` awslogs.powertools_base64_gzip(data) `
227
- EventBridge / Cloudwatch | ScheduledEvent | ` detail `
228
- Kafka | KafkaEvent | ` records[*][*].value `
229
- Kinesis | KinesisEvent | ` Records[*].kinesis.powertools_base64(data) `
230
- Kinesis Firehose | KinesisFirehoseEvent | ` Records[*].powertools_base64(data) `
231
- Kinesis Analytics from Firehose | KinesisAnalyticsFirehoseInputPreprocessingEvent | ` Records[*].powertools_base64(data) `
232
- Kinesis Analytics from Streams | KinesisAnalyticsStreamsInputPreprocessingEvent | ` Records[*].powertools_base64(data) `
233
- SNS | SNSEvent | ` Records[*].Sns.Message `
234
- SQS | SQSEvent | ` Records[*].body `
158
+ | Type of event | Class | Path to content |
159
+ | ---------------------------------| -------------------------------------------------| ----------------------------------------------|
160
+ | API Gateway REST | APIGatewayProxyRequestEvent | ` body ` |
161
+ | API Gateway HTTP | APIGatewayV2HTTPEvent | ` body ` |
162
+ | Application Load Balancer | ApplicationLoadBalancerRequestEvent | ` body ` |
163
+ | Cloudformation Custom Resource | CloudFormationCustomResourceEvent | ` resourceProperties ` |
164
+ | CloudWatch Logs | CloudWatchLogsEvent | ` awslogs.powertools_base64_gzip(data) ` |
165
+ | EventBridge / Cloudwatch | ScheduledEvent | ` detail ` |
166
+ | Kafka | KafkaEvent | ` records[*][*].value ` |
167
+ | Kinesis | KinesisEvent | ` Records[*].kinesis.powertools_base64(data) ` |
168
+ | Kinesis Firehose | KinesisFirehoseEvent | ` Records[*].powertools_base64(data) ` |
169
+ | Kinesis Analytics from Firehose | KinesisAnalyticsFirehoseInputPreprocessingEvent | ` Records[*].powertools_base64(data) ` |
170
+ | Kinesis Analytics from Streams | KinesisAnalyticsStreamsInputPreprocessingEvent | ` Records[*].powertools_base64(data) ` |
171
+ | SNS | SNSEvent | ` Records[*].Sns.Message ` |
172
+ | SQS | SQSEvent | ` Records[*].body ` |
235
173
236
174
** Responses **
237
175
238
- Type of response | Class | Path to content (envelope)
239
- ------------------------------------------------- | ------------------------------------------------- | -------------------------------------------------
240
- API Gateway REST | APIGatewayProxyResponseEvent} | ` body `
241
- API Gateway HTTP | APIGatewayV2HTTPResponse} | ` body `
242
- API Gateway WebSocket | APIGatewayV2WebSocketResponse} | ` body `
243
- Load Balancer | ApplicationLoadBalancerResponseEvent} | ` body `
244
- Kinesis Analytics | KinesisAnalyticsInputPreprocessingResponse} | `Records[ * ] .powertools_base64(data)``
176
+ | Type of response | Class | Path to content (envelope) |
177
+ | -----------------------| ---------------------------------------------| ---------------------------------------|
178
+ | API Gateway REST | APIGatewayProxyResponseEvent} | ` body ` |
179
+ | API Gateway HTTP | APIGatewayV2HTTPResponse} | ` body ` |
180
+ | API Gateway WebSocket | APIGatewayV2WebSocketResponse} | ` body ` |
181
+ | Load Balancer | ApplicationLoadBalancerResponseEvent} | ` body ` |
182
+ | Kinesis Analytics | KinesisAnalyticsInputPreprocessingResponse} | `Records[ * ] .powertools_base64(data)`` |
245
183
246
184
## Custom events and responses
247
185
0 commit comments