Skip to main content

@nhttp/zod@2.0.2
Built and signed on GitHub Actions

zod-validator for nhttp.

This package works with Cloudflare Workers, Node.js, Deno, BunIt is unknown whether this package works with Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
It is unknown whether this package works with Browsers
JSR Score
100%
Published
4 months ago (2.0.2)
// mod.ts /** * @module * * This module contains validate body with `zod` for NHttp. */ import { z, type ZodSchema } from "npm:zod@3.23.8"; import { joinHandlers, type TDecorator } from "jsr:/@nhttp/nhttp@^2.0.2/controller"; import { type Handler, HttpError, type RequestEvent, type TRet, } from "jsr:@nhttp/nhttp@^2.0.2"; /** * validate using `zod`. * @example * app.post("/save", validate(ZodSchema), ...handlers); */ export function validate< S extends string = "body", T extends unknown = unknown, >( schema: ZodSchema<T>, target = <S> "body", onError?: (err: TRet, rev: RequestEvent) => TRet, ): Handler<{ [k in S]: T }> { return (rev, next) => { try { const tgt = rev[target]; const res = schema.parse(tgt); if (typeof res === "object") { (<TRet> rev)[target] = res; } return next(); } catch (e) { if (onError) { return onError(e, rev as RequestEvent); } throw new HttpError(422, e.errors); } }; } /** * validate using `zod` for decorators. * @example * ```ts * class UserController { * * ⁤@Validate(UserSchema) * ⁤@Post() * save() {...} * } * ``` */ export function Validate( schema: ZodSchema, target = "body", onError?: (err: TRet, rev: RequestEvent) => TRet, ): TDecorator { return (tgt: TRet, prop: string, des: PropertyDescriptor) => { joinHandlers(tgt.constructor.name, prop, [ validate(schema, target, onError), ]); return des; }; } export { z }; export default validate;