import type { FastifyReply, FastifyRequest, FastifySchema } from 'fastify'; import type { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { z } from 'zod'; /** * Type helper to check if a value is a Zod schema * Exported for use in routeRegistration.ts */ export declare function isZodSchema(value: unknown): value is z.ZodTypeAny; /** * Schema definition that accepts both Zod schemas and JSON Schema * This allows mixing validation approaches within a single route definition. */ export interface ZodRouteSchema { params?: z.ZodTypeAny | JSONSchema; querystring?: z.ZodTypeAny | JSONSchema; body?: z.ZodTypeAny | JSONSchema; headers?: z.ZodTypeAny | JSONSchema; response?: Record; [key: string]: unknown; } /** * Schema type indicators to track which fields use Zod vs JSON Schema */ export interface SchemaTypeIndicators { params?: 'zod' | 'json'; querystring?: 'zod' | 'json'; body?: 'zod' | 'json'; headers?: 'zod' | 'json'; response?: Record; } /** * Helper type to extract TypeScript types from mixed Zod and JSON Schema schemas * Returns unknown for properties that don't exist (matching Fastify's default behavior) */ type ExtractZodSchemaTypes = { Params: T['params'] extends z.ZodTypeAny ? z.infer : T['params'] extends JSONSchema ? FromSchema : unknown; Body: T['body'] extends z.ZodTypeAny ? z.infer : T['body'] extends JSONSchema ? FromSchema : unknown; Querystring: T['querystring'] extends z.ZodTypeAny ? z.infer : T['querystring'] extends JSONSchema ? FromSchema : unknown; Headers: T['headers'] extends z.ZodTypeAny ? z.infer : T['headers'] extends JSONSchema ? FromSchema : unknown; Response: T['response'] extends Record ? { [K in keyof T['response']]: T['response'][K] extends z.ZodTypeAny ? z.infer : T['response'][K] extends JSONSchema ? FromSchema : never; } : unknown; }; /** * Route handler function type with inferred types from Zod schemas */ type TypedZodRouteHandler = (request: FastifyRequest>, reply: FastifyReply) => Promise | unknown; /** * Route module returned by defineRouteZod */ export interface DefinedZodRoute { schema: FastifySchema & { __zodSchemas?: Partial<{ params?: z.ZodTypeAny; querystring?: z.ZodTypeAny; body?: z.ZodTypeAny; headers?: z.ZodTypeAny; response?: Record; }>; __schemaTypes?: SchemaTypeIndicators; }; handler: TypedZodRouteHandler; } /** * Formats Zod validation errors into a single error message string. * Format: "Bad Request: [component] - [comma-separated list of issues]" * This function is exported so it can be used in routeRegistration.ts */ export declare function formatZodError(error: z.ZodError, component: string): string; /** * Formats JSON Schema validation errors into a single error message string. * Format: "Bad Request: [component] - [comma-separated list of issues]" * This function is exported so it can be used in routeRegistration.ts */ export declare function formatJsonSchemaError(errors: Array<{ instancePath: string; message?: string; }>, component: string): string; /** * Helper to define a route using Zod schemas and/or JSON Schema. * This function extracts TypeScript types from Zod schemas using `z.infer` * and from JSON Schema using `FromSchema`, allowing you to mix validation approaches. * * @example * ```typescript * import { defineRouteZod } from 'fastify-file-router'; * import { z } from 'zod'; * * // Pure Zod example * export const route = defineRouteZod({ * schema: { * params: z.object({ * id: z.string().min(1) * }), * body: z.object({ * name: z.string().optional(), * email: z.email().optional() * }) * }, * handler: async (request, reply) => { * // request.params.id is correctly typed as string * // request.body.name and request.body.email are correctly typed * const { id } = request.params; * const { name, email } = request.body; * reply.status(200).send({ id, name, email }); * } * }); * ``` * * @example * ```typescript * // Mixed Zod and JSON Schema example * export const route = defineRouteZod({ * schema: { * params: z.object({ id: z.string() }), // Zod schema * body: { * type: 'object', * properties: { name: { type: 'string' } }, * required: ['name'] * } as const // JSON Schema * }, * handler: async (request, reply) => { * // Types are correctly inferred from both schema types * const { id } = request.params; // string (from Zod) * const { name } = request.body; // string (from JSON Schema) * reply.status(200).send({ id, name }); * } * }); * ``` */ export declare function defineRouteZod(route: { schema: T; handler: TypedZodRouteHandler; }): DefinedZodRoute; export {}; //# sourceMappingURL=defineRouteZod.d.ts.map