Skip to content

Commit bb9c62b

Browse files
committed
Initial state of openapi-request-validation library
0 parents  commit bb9c62b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+5506
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist
2+
node_modules
3+
.idea

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright 2026 Max Schlüssel
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
```

jest.config.cjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
preset: "ts-jest",
3+
testEnvironment: "node",
4+
testMatch: ["<rootDir>/test/**/*.test.ts"],
5+
clearMocks: true,
6+
moduleNameMapper: {
7+
"^(\\.{1,2}/.*)\\.js$": "$1",
8+
},
9+
transform: {
10+
"^.+\\.ts$": [
11+
"ts-jest",
12+
{
13+
tsconfig: "<rootDir>/tsconfig.test.json",
14+
},
15+
],
16+
},
17+
}

package.json

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"name": "openapi-request-validation",
3+
"version": "0.1.0",
4+
"description": "Build-time OpenAPI request validation for serverless and lightweight Node runtimes",
5+
"type": "module",
6+
"sideEffects": false,
7+
"main": "./dist/cjs/index.js",
8+
"module": "./dist/esm/index.js",
9+
"types": "./dist/types/index.d.ts",
10+
"bin": {
11+
"openapi-request-validation": "./dist/cjs/cli.js"
12+
},
13+
"exports": {
14+
".": {
15+
"types": "./dist/types/index.d.ts",
16+
"import": "./dist/esm/index.js",
17+
"require": "./dist/cjs/index.js"
18+
},
19+
"./generator": {
20+
"types": "./dist/types/generator.d.ts",
21+
"import": "./dist/esm/generator.js",
22+
"require": "./dist/cjs/generator.js"
23+
},
24+
"./runtime": {
25+
"types": "./dist/types/runtime.d.ts",
26+
"import": "./dist/esm/runtime.js",
27+
"require": "./dist/cjs/runtime.js"
28+
},
29+
"./package.json": "./package.json"
30+
},
31+
"typesVersions": {
32+
"*": {
33+
"generator": [
34+
"dist/types/generator.d.ts"
35+
],
36+
"runtime": [
37+
"dist/types/runtime.d.ts"
38+
]
39+
}
40+
},
41+
"files": [
42+
"dist",
43+
"README.md"
44+
],
45+
"scripts": {
46+
"build": "node ./scripts/build.mjs",
47+
"test": "npm run build && node ./node_modules/jest/bin/jest.js --runInBand"
48+
},
49+
"keywords": [
50+
"openapi",
51+
"ajv",
52+
"lambda",
53+
"apigateway",
54+
"validation"
55+
],
56+
"engines": {
57+
"node": ">=20"
58+
},
59+
"dependencies": {
60+
"@apidevtools/swagger-parser": "^12.0.0",
61+
"ajv": "^8.17.1",
62+
"ajv-formats": "^3.0.1",
63+
"openapi-types": "^12.1.3"
64+
},
65+
"devDependencies": {
66+
"@types/express": "^5.0.3",
67+
"@types/jest": "^29.5.14",
68+
"@types/node": "^24.0.0",
69+
"express": "^5.1.0",
70+
"jest": "^29.7.0",
71+
"ts-jest": "^29.4.1",
72+
"typescript": "^5.8.2"
73+
}
74+
}

playground/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>OpenAPI Request Validation Playground</title>
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
<script type="module" src="/src/main.tsx"></script>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)