Skip to content

Commit 2fe95aa

Browse files
committed
First iteration of code generation
1 parent cb8e6d6 commit 2fe95aa

File tree

9 files changed

+1038
-33
lines changed

9 files changed

+1038
-33
lines changed

packages/event-handler/README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,109 @@ export const handler = async (event: unknown, context: Context) =>
297297

298298
See the [documentation](https://docs.aws.amazon.com/powertools/typescript/latest/features/event-handler/appsync-events) for more details on how to use the AppSync event handler.
299299

300+
## REST API - Validation Middleware
301+
302+
The validation middleware provides first-class support for request and response validation using schema libraries like Zod, Valibot, or ArkType through the Standard Schema specification.
303+
304+
### Installation
305+
306+
Install the validation middleware peer dependency:
307+
308+
```sh
309+
npm i @standard-schema/spec
310+
```
311+
312+
Then install your preferred schema library:
313+
314+
```sh
315+
# Zod
316+
npm i zod
317+
318+
# Or Valibot
319+
npm i valibot
320+
321+
# Or ArkType
322+
npm i arktype
323+
```
324+
325+
### Basic Usage
326+
327+
```typescript
328+
import { Router } from '@aws-lambda-powertools/event-handler/experimental-rest';
329+
import { validation } from '@aws-lambda-powertools/event-handler/experimental-rest/middleware';
330+
import { z } from 'zod';
331+
332+
const app = new Router();
333+
334+
const userSchema = z.object({
335+
name: z.string().min(1),
336+
email: z.string().email(),
337+
});
338+
339+
app.post('/users', {
340+
middleware: [validation({ req: { body: userSchema } })],
341+
}, async (reqCtx) => {
342+
const body = reqCtx.req.body; // Fully typed from schema
343+
return { statusCode: 201, body: { id: '123', ...body } };
344+
});
345+
346+
export const handler = app.resolve.bind(app);
347+
```
348+
349+
### Validation Options
350+
351+
The validation middleware supports validating different parts of the request and response:
352+
353+
```typescript
354+
validation({
355+
req: {
356+
body: bodySchema, // Validate request body
357+
headers: headerSchema, // Validate request headers
358+
path: pathSchema, // Validate path parameters
359+
query: querySchema, // Validate query parameters
360+
},
361+
res: {
362+
body: responseSchema, // Validate response body
363+
headers: headerSchema, // Validate response headers
364+
},
365+
})
366+
```
367+
368+
### Error Handling
369+
370+
Validation failures throw `RequestValidationError` (422) or `ResponseValidationError` (500):
371+
372+
```typescript
373+
import { Router, RequestValidationError } from '@aws-lambda-powertools/event-handler/experimental-rest';
374+
375+
const app = new Router();
376+
377+
app.onError(RequestValidationError, (error) => {
378+
return {
379+
statusCode: 422,
380+
body: {
381+
error: 'Validation Failed',
382+
message: error.message,
383+
component: error.component, // 'body', 'headers', 'path', or 'query'
384+
},
385+
};
386+
});
387+
```
388+
389+
### Development Mode
390+
391+
Set `POWERTOOLS_DEV=true` to include detailed validation errors in responses for debugging.
392+
393+
### Supported Schema Libraries
394+
395+
Any library implementing the [Standard Schema](https://github.com/standard-schema/standard-schema) specification:
396+
397+
* **Zod** (v3.x)
398+
* **Valibot** (v1.x)
399+
* **ArkType** (v2.x)
400+
401+
See the [examples directory](../../examples/snippets/event-handler/rest/) for complete usage examples.
402+
300403
## Contribute
301404

302405
If you are interested in contributing to this project, please refer to our [Contributing Guidelines](https://github.com/aws-powertools/powertools-lambda-typescript/blob/main/CONTRIBUTING.md).
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Router } from './lib/esm/rest/index.js';
2+
3+
const app = new Router();
4+
5+
console.log('Test 1: Simple route without validation');
6+
app.post('/simple', async () => ({ statusCode: 200 }));
7+
8+
console.log('\nTest 2: Route with validation');
9+
app.post('/validated', async () => ({ statusCode: 201 }), {
10+
validation: { req: { body: { '~standard': { version: 1, vendor: 'test', validate: () => ({ value: {} }) } } } }
11+
});
12+
13+
console.log('\nDone - if no errors, routes registered successfully');

packages/event-handler/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@
127127
"dependencies": {
128128
"@aws-lambda-powertools/commons": "2.28.1"
129129
},
130+
"peerDependencies": {
131+
"@standard-schema/spec": "^1.0.0"
132+
},
133+
"peerDependenciesMeta": {
134+
"@standard-schema/spec": {
135+
"optional": true
136+
}
137+
},
130138
"keywords": [
131139
"aws",
132140
"lambda",

0 commit comments

Comments
 (0)