UNPKG

@busy-hour/blaze

Version:

<h1 align='center'>🔥 Blaze</h1> <div align='center'> An event driven framework for 🔥 Hono.js </div>

29 lines (28 loc) • 841 B
// src/validator/index.ts import { z } from "zod"; import { BlazeValidationError } from "../internal/errors/validation.js"; import { isEmpty } from "../utils/common.js"; import { validateInput } from "./helper.js"; async function validateAll(options) { const { ctx, validator, setter } = options; if (!validator || isEmpty(validator)) return; const keys = Object.keys(validator); const schema = z.object(validator); const input = {}; for (const key of keys) { const value = ctx.request[key]; input[key] = value instanceof Function ? await value() : value; } const result = await validateInput(input, schema); if (!result.success) { throw new BlazeValidationError(ctx, result.error); } for (const key of keys) { const value = result.data[key]; setter[key](value); } } export { validateAll };