|
| 1 | +import type { Context, Env, MiddlewareHandler, ValidationTargets } from 'hono'; |
| 2 | +import { validator } from 'hono/validator'; |
| 3 | +import { Ajv, type JSONSchemaType, type ErrorObject } from 'ajv'; |
| 4 | + |
| 5 | +type Hook<T, E extends Env, P extends string> = ( |
| 6 | + result: { success: true; data: T } | { success: false; errors: ErrorObject[] }, |
| 7 | + c: Context<E, P> |
| 8 | +) => Response | Promise<Response> | void; |
| 9 | + |
| 10 | +/** |
| 11 | + * Hono middleware that validates incoming data via an Ajv JSON schema. |
| 12 | + * |
| 13 | + * --- |
| 14 | + * |
| 15 | + * No Hook |
| 16 | + * |
| 17 | + * ```ts |
| 18 | + * import { type JSONSchemaType } from 'ajv'; |
| 19 | + * import { ajvValidator } from '@hono/ajv-validator'; |
| 20 | + * |
| 21 | + * const schema: JSONSchemaType<{ name: string; age: number }> = { |
| 22 | + * type: 'object', |
| 23 | + * properties: { |
| 24 | + * name: { type: 'string' }, |
| 25 | + * age: { type: 'number' }, |
| 26 | + * }, |
| 27 | + * required: ['name', 'age'], |
| 28 | + * additionalProperties: false, |
| 29 | + * }; |
| 30 | + * |
| 31 | + * const route = app.post('/user', ajvValidator('json', schema), (c) => { |
| 32 | + * const user = c.req.valid('json'); |
| 33 | + * return c.json({ success: true, message: `${user.name} is ${user.age}` }); |
| 34 | + * }); |
| 35 | + * ``` |
| 36 | + * |
| 37 | + * --- |
| 38 | + * Hook |
| 39 | + * |
| 40 | + * ```ts |
| 41 | + * import { type JSONSchemaType } from 'ajv'; |
| 42 | + * import { ajvValidator } from '@hono/ajv-validator'; |
| 43 | + * |
| 44 | + * const schema: JSONSchemaType<{ name: string; age: number }> = { |
| 45 | + * type: 'object', |
| 46 | + * properties: { |
| 47 | + * name: { type: 'string' }, |
| 48 | + * age: { type: 'number' }, |
| 49 | + * }, |
| 50 | + * required: ['name', 'age'], |
| 51 | + * additionalProperties: false, |
| 52 | + * }; |
| 53 | + * |
| 54 | + * app.post( |
| 55 | + * '/user', |
| 56 | + * ajvValidator('json', schema, (result, c) => { |
| 57 | + * if (!result.success) { |
| 58 | + * return c.text('Invalid!', 400); |
| 59 | + * } |
| 60 | + * }) |
| 61 | + * //... |
| 62 | + * ); |
| 63 | + * ``` |
| 64 | + */ |
| 65 | +export function ajvValidator< |
| 66 | + T, |
| 67 | + Target extends keyof ValidationTargets, |
| 68 | + E extends Env = Env, |
| 69 | + P extends string = string |
| 70 | +>( |
| 71 | + target: Target, |
| 72 | + schema: JSONSchemaType<T>, |
| 73 | + hook?: Hook<T, E, P> |
| 74 | +): MiddlewareHandler< |
| 75 | + E, |
| 76 | + P, |
| 77 | + { |
| 78 | + in: { [K in Target]: T }; |
| 79 | + out: { [K in Target]: T }; |
| 80 | + } |
| 81 | +> { |
| 82 | + const ajv = new Ajv(); |
| 83 | + const validate = ajv.compile(schema); |
| 84 | + |
| 85 | + return validator(target, (data, c) => { |
| 86 | + const valid = validate(data); |
| 87 | + if (valid) { |
| 88 | + if (hook) { |
| 89 | + const hookResult = hook({ success: true, data: data as T }, c); |
| 90 | + if (hookResult instanceof Response || hookResult instanceof Promise) { |
| 91 | + return hookResult; |
| 92 | + } |
| 93 | + } |
| 94 | + return data as T; |
| 95 | + } |
| 96 | + |
| 97 | + const errors = validate.errors || []; |
| 98 | + if (hook) { |
| 99 | + const hookResult = hook({ success: false, errors }, c); |
| 100 | + if (hookResult instanceof Response || hookResult instanceof Promise) { |
| 101 | + return hookResult; |
| 102 | + } |
| 103 | + } |
| 104 | + return c.json({ success: false, errors }, 400); |
| 105 | + }); |
| 106 | +} |
0 commit comments