Skip to content

Commit

Permalink
feat(ajv-validator): Add Ajv validator middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhvost committed Oct 24, 2024
1 parent ee5d7e0 commit 3455251
Show file tree
Hide file tree
Showing 13 changed files with 499 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-poets-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/ajv-validator': patch
---

Add Ajv validator middleware
25 changes: 25 additions & 0 deletions .github/workflows/ci-ajv-validator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: ci-ajv-validator
on:
push:
branches: [main]
paths:
- 'packages/ajv-validator/**'
pull_request:
branches: ['*']
paths:
- 'packages/ajv-validator/**'

jobs:
ci:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./packages/ajv-validator
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20.x
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"build:effect-validator": "yarn workspace @hono/effect-validator build",
"build:conform-validator": "yarn workspace @hono/conform-validator build",
"build:casbin": "yarn workspace @hono/casbin build",
"build:ajv-validator": "yarn workspace @hono/ajv-validator build",
"build": "run-p 'build:*'",
"lint": "eslint 'packages/**/*.{ts,tsx}'",
"lint:fix": "eslint --fix 'packages/**/*.{ts,tsx}'",
Expand Down
1 change: 1 addition & 0 deletions packages/ajv-validator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @hono/ajv-validator
63 changes: 63 additions & 0 deletions packages/ajv-validator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Ajv validator middleware for Hono

Validator middleware using [Ajv](https://github.com/ajv-validator/ajv) for [Hono](https://honojs.dev) applications.
Define your schema with Ajv and validate incoming requests.

## Usage

No Hook:

```ts
import { type JSONSchemaType } from 'ajv';
import { ajvValidator } from '@hono/ajv-validator';

const schema: JSONSchemaType<{ name: string; age: number }> = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' },
},
required: ['name', 'age'],
additionalProperties: false,
} as const;

const route = app.post('/user', ajvValidator('json', schema), (c) => {
const user = c.req.valid('json');
return c.json({ success: true, message: `${user.name} is ${user.age}` });
});
```

Hook:

```ts
import { type JSONSchemaType } from 'ajv';
import { ajvValidator } from '@hono/ajv-validator';

const schema: JSONSchemaType<{ name: string; age: number }> = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' },
},
required: ['name', 'age'],
additionalProperties: false,
};

app.post(
'/user',
ajvValidator('json', schema, (result, c) => {
if (!result.success) {
return c.text('Invalid!', 400);
}
})
//...
);
```

## Author

Illia Khvost <https://github.com/ikhvost>

## License

MIT
1 change: 1 addition & 0 deletions packages/ajv-validator/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../jest.config.js')
39 changes: 39 additions & 0 deletions packages/ajv-validator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@hono/ajv-validator",
"version": "0.0.0",
"description": "Validator middleware using Ajv",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"files": [
"dist"
],
"scripts": {
"test": "jest",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:esm": "tsc -p tsconfig.esm.json",
"build": "rimraf dist && yarn build:cjs && yarn build:esm",
"prerelease": "yarn build && yarn test",
"release": "yarn publish"
},
"license": "MIT",
"publishConfig": {
"registry": "https://registry.npmjs.org",
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/honojs/middleware.git"
},
"homepage": "https://github.com/honojs/middleware",
"peerDependencies": {
"ajv": ">=8.12.0",
"hono": ">=3.9.0"
},
"devDependencies": {
"ajv": ">=8.12.0",
"hono": "^3.11.7",
"jest": "^29.7.0",
"rimraf": "^5.0.5"
}
}
106 changes: 106 additions & 0 deletions packages/ajv-validator/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import type { Context, Env, MiddlewareHandler, ValidationTargets } from 'hono';
import { validator } from 'hono/validator';
import { Ajv, type JSONSchemaType, type ErrorObject } from 'ajv';

type Hook<T, E extends Env, P extends string> = (
result: { success: true; data: T } | { success: false; errors: ErrorObject[] },
c: Context<E, P>
) => Response | Promise<Response> | void;

/**
* Hono middleware that validates incoming data via an Ajv JSON schema.
*
* ---
*
* No Hook
*
* ```ts
* import { type JSONSchemaType } from 'ajv';
* import { ajvValidator } from '@hono/ajv-validator';
*
* const schema: JSONSchemaType<{ name: string; age: number }> = {
* type: 'object',
* properties: {
* name: { type: 'string' },
* age: { type: 'number' },
* },
* required: ['name', 'age'],
* additionalProperties: false,
* };
*
* const route = app.post('/user', ajvValidator('json', schema), (c) => {
* const user = c.req.valid('json');
* return c.json({ success: true, message: `${user.name} is ${user.age}` });
* });
* ```
*
* ---
* Hook
*
* ```ts
* import { type JSONSchemaType } from 'ajv';
* import { ajvValidator } from '@hono/ajv-validator';
*
* const schema: JSONSchemaType<{ name: string; age: number }> = {
* type: 'object',
* properties: {
* name: { type: 'string' },
* age: { type: 'number' },
* },
* required: ['name', 'age'],
* additionalProperties: false,
* };
*
* app.post(
* '/user',
* ajvValidator('json', schema, (result, c) => {
* if (!result.success) {
* return c.text('Invalid!', 400);
* }
* })
* //...
* );
* ```
*/
export function ajvValidator<
T,
Target extends keyof ValidationTargets,
E extends Env = Env,
P extends string = string
>(
target: Target,
schema: JSONSchemaType<T>,
hook?: Hook<T, E, P>
): MiddlewareHandler<
E,
P,
{
in: { [K in Target]: T };
out: { [K in Target]: T };
}
> {
const ajv = new Ajv();
const validate = ajv.compile(schema);

return validator(target, (data, c) => {
const valid = validate(data);
if (valid) {
if (hook) {
const hookResult = hook({ success: true, data: data as T }, c);
if (hookResult instanceof Response || hookResult instanceof Promise) {
return hookResult;
}
}
return data as T;
}

const errors = validate.errors || [];
if (hook) {
const hookResult = hook({ success: false, errors }, c);
if (hookResult instanceof Response || hookResult instanceof Promise) {
return hookResult;
}
}
return c.json({ success: false, errors }, 400);
});
}
Loading

0 comments on commit 3455251

Please sign in to comment.