UNPKG

exstack

Version:

A utility library designed to simplify and enhance Express.js applications.

99 lines (97 loc) 3.29 kB
import * as z from "zod"; import { RequestHandler } from "express"; //#region src/zod.d.ts /** * Possible request targets to validate. */ type Target = 'body' | 'query' | 'params' | 'all'; /** * Shape of schemas passed to `.all()`. */ type Schema<T extends Target> = T extends 'all' ? { body?: z.ZodType; query?: z.ZodType; params?: z.ZodType; } : z.ZodType; /** * Express.js-compatible validator class. */ declare class Validator { #private; /** * Validates the request body against a Zod schema. * @param {ZodType} schema - Zod schema to validate `req.body` * @returns {RequestHandler} Middleware that validates `req.body` and populates `req._validated.body` */ body: (schema: z.ZodType) => RequestHandler; /** * Validates the request query string against a Zod schema. * @param {ZodType} schema - Zod schema to validate `req.query` * @returns {RequestHandler} Middleware that validates `req.query` and populates `req._validated.query` */ query: (schema: z.ZodType) => RequestHandler; /** * Validates the request URL parameters against a Zod schema. * @param {ZodType} schema - Zod schema to validate `req.params` * @returns {RequestHandler} Middleware that validates `req.params` and populates `req._validated.params` */ params: (schema: z.ZodType) => RequestHandler; /** * Validates multiple parts of the request (body, query, params) simultaneously. * @param {Schema<'all'>} schema - Object containing Zod schemas for each request part * @returns {RequestHandler} Middleware that validates all provided request parts and populates `req._validated` */ all: (schema: Schema<"all">) => RequestHandler; } /** * Provides middleware for validating request body, query, params, or all of them using Zod schemas. * Attaches a `req.valid()` helper to access validated data. */ declare const validator: Validator; /** * Extends Express Request. */ declare module 'express-serve-static-core' { interface Request { /** * Retrieve already validated data. * * ### Examples * * ```ts * import { z } from 'zod'; * import { validator } from './validator'; * * // Define a schema for the body * const userSchema = z.object({ * name: z.string(), * age: z.number().int(), * }); * * // Use validator middleware * app.post('/user', validator.body(userSchema), (req, res) => { * // ✅ Inferred automatically from the Zod schema * const user = req.valid('body'); * // user: { name: string; age: number } * * // ✅ Explicitly specify Zod type if you prefer clarity * const typedUser = req.valid<typeof userSchema>('body'); * * // ✅ Or manually define your own structure * const manualUser = req.valid<{ name: string; age: number }>('body'); * * res.json(user); * }); * ``` */ valid<T extends Target>(type: T): T extends 'body' ? Record<string, any> : T extends 'query' ? Record<string, any> : T extends 'all' ? { body: Body; query: Query; params: Params; } : Record<string, any>; valid<T extends z.ZodType>(type: Target): z.Infer<T>; valid<T extends object>(type: Target): T; } } //#endregion export { validator };