|
| 1 | +# openapi-request-validation |
| 2 | + |
| 3 | +Build-time OpenAPI request validation for serverless and lightweight Node runtimes. Performs request validation based on |
| 4 | +OpenAPI specifications. Supports AWS Lambda API Gateway events, lambda-api requests and Express. |
| 5 | + |
| 6 | +Uses AJV to generate standalone request validators and ships runtime helpers for easy validation. |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +Install the package as a normal dependency, since it is required both at build time and runtime: |
| 11 | +```bash |
| 12 | +npm install openapi-request-validation |
| 13 | +``` |
| 14 | + |
| 15 | +## CLI |
| 16 | + |
| 17 | +Generate validators from a spec into a target directory: |
| 18 | + |
| 19 | +```bash |
| 20 | +openapi-request-validation ./openapi.yaml ./src/generated |
| 21 | +``` |
| 22 | + |
| 23 | +This writes a file such as `./src/generated/openapi-validators.js`. Each operation with a request shape gets an export |
| 24 | +like `validateCreateUserRequest`. Your tsconfig must specify `allowJs: true` to use these validators. |
| 25 | + |
| 26 | +## Runtime use |
| 27 | + |
| 28 | +### API Gateway |
| 29 | + |
| 30 | +Use `validateApiGatewayEvent(...)` to validate a raw API Gateway event before your handler logic runs. |
| 31 | + |
| 32 | +```ts |
| 33 | +import {validateCreateCustomerOrderRequest} from "./generated/openapi-validators.js" |
| 34 | +import {validateApiGatewayEvent} from "openapi-request-validation" |
| 35 | + |
| 36 | +export async function handler(event: unknown) { |
| 37 | + validateApiGatewayEvent(validateCreateCustomerOrderRequest, event) |
| 38 | + |
| 39 | + return { |
| 40 | + statusCode: 204, |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +### lambda-api |
| 46 | + |
| 47 | +Wrap route handlers with `withValidation(...)` or call `validateLambdaApiRequest(...)` directly. |
| 48 | + |
| 49 | +```ts |
| 50 | +import {validateCreateCustomerOrderRequest} from "./generated/openapi-validators.js" |
| 51 | +import {withValidation} from "openapi-request-validation" |
| 52 | + |
| 53 | +api.post( |
| 54 | + "/customers/:customerId/orders", |
| 55 | + withValidation(validateCreateCustomerOrderRequest, (req, res) => { |
| 56 | + res.send({ok: true}) |
| 57 | + }), |
| 58 | +) |
| 59 | +``` |
| 60 | + |
| 61 | +### Express |
| 62 | + |
| 63 | +Wrap Express handlers with `withExpressValidation(...)` or call `validateExpressRequest(...)` directly. |
| 64 | + |
| 65 | +```ts |
| 66 | +import express from "express" |
| 67 | +import {validateCreateCustomerOrderRequest} from "./generated/openapi-validators.js" |
| 68 | +import {withExpressValidation} from "openapi-request-validation" |
| 69 | + |
| 70 | +const app = express() |
| 71 | +app.use(express.json()) |
| 72 | + |
| 73 | +app.post( |
| 74 | + "/customers/:customerId/orders", |
| 75 | + withExpressValidation(validateCreateCustomerOrderRequest, (req, res) => { |
| 76 | + res.status(201).json({ok: true}) |
| 77 | + }), |
| 78 | +) |
| 79 | +``` |
| 80 | + |
| 81 | +## Error handling |
| 82 | + |
| 83 | +The runtime helpers throw `RequestValidationError` with HTTP status `400`. |
| 84 | + |
| 85 | +If your application already uses its own error type, pass `createError` in the optional runtime options: |
| 86 | + |
| 87 | +```ts |
| 88 | +import {validateExpressRequest} from "openapi-request-validation" |
| 89 | + |
| 90 | +validateExpressRequest(validateCreateCustomerOrderRequest, req, { |
| 91 | + createError: (message) => new BadRequestProblem(message), |
| 92 | +}) |
| 93 | +``` |
0 commit comments