import { InferInput } from "hono/validator"; import { StandardSchemaV1 } from "@standard-schema/spec"; import { Context, Env, Input, TypedResponse, ValidationTargets } from "hono"; import { Handler } from "hono/types"; //#region src/index.d.ts type HasUndefined = undefined extends T ? true : false; type FailedResponse = Response & TypedResponse<{ readonly success: false; readonly error: readonly StandardSchemaV1.Issue[]; readonly data: T; }, 400, "json">; type MustBeResponse = T extends Promise ? Promise> : T extends Response | TypedResponse ? T : never; type Hook | Promise | void> = void | Response | TypedResponse | Promise | void>> = (result: ({ success: true; data: T; } | { success: false; error: readonly StandardSchemaV1.Issue[]; data: T; }) & { target: Target; }, c: Context) => R; /** * Validation middleware for libraries that support [Standard Schema](https://standardschema.dev/) specification. * * This middleware validates incoming request data against a provided schema * that conforms to the Standard Schema specification. It supports validation * of JSON bodies, headers, queries, forms, and other request targets. * * @param target - The request target to validate ('json', 'header', 'query', 'form', etc.) * @param schema - A schema object conforming to Standard Schema specification * @param hook - Optional hook function called with validation results for custom error handling * @returns A Hono middleware handler that validates requests and makes validated data available via `c.req.valid()` * * @example Basic JSON validation * ```ts * import { z } from 'zod' * import { sValidator } from '@hono/standard-validator' * * const schema = z.object({ * name: z.string(), * age: z.number(), * }) * * app.post('/author', sValidator('json', schema), (c) => { * const data = c.req.valid('json') * return c.json({ * success: true, * message: `${data.name} is ${data.age}`, * }) * }) * ``` * * @example With custom error handling hook * ```ts * app.post( * '/post', * sValidator('json', schema, (result, c) => { * if (!result.success) { * return c.text('Invalid!', 400) * } * }), * (c) => { * // Handler code * } * ) * ``` * * @example Header validation * ```ts * import { object, string } from 'valibot' * * const schema = object({ * 'content-type': string(), * 'user-agent': string(), * }) * * app.post('/author', sValidator('header', schema), (c) => { * const headers = c.req.valid('header') * // do something with headers * }) * ``` */ declare const sValidator: , Out = StandardSchemaV1.InferOutput, I extends Input = { in: HasUndefined extends true ? { [K in Target]?: [In] extends [ValidationTargets[K]] ? In : InferInput; } : { [K in Target]: [In] extends [ValidationTargets[K]] ? In : InferInput; }; out: { [K in Target]: Out; }; }, V extends I = I, R extends void | Response | TypedResponse | Promise | void> = FailedResponse>(target: Target, schema: Schema, hook?: Hook, E, P, Target, R>) => Handler>; interface FlattenedErrorObject { formErrors: string[]; fieldErrors: Record; } /** * Sorts validation errors by their paths. * @param issues An array of {@link StandardSchemaV1.Issue validation issues}. * @returns An object with sorted form and field errors. */ declare const flattenErrors: (issues: readonly StandardSchemaV1.Issue[]) => FlattenedErrorObject; //#endregion export { FailedResponse, type Hook, flattenErrors, sValidator }; //# sourceMappingURL=index.d.mts.map