Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
nhttp/nhttpzod-validator for nhttp.
This package works with Cloudflare Workers, Node.js, Deno, BunIt is unknown whether this package works with Browsers
JSR Score
100%
Published
4 months ago (2.0.2)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970// 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;