/opt/canhelp/node_modules/@hono/zod-validator/dist
Edit: /opt/canhelp/node_modules/@hono/zod-validator/dist/index.js (1142B)
// src/index.ts
import { validator } from "hono/validator";
import { ZodObject } from "zod";
var zValidator = (target, schema, hook) => (
// @ts-expect-error not typed well
validator(target, async (value, c) => {
let validatorValue = value;
if (target === "header" && schema instanceof ZodObject) {
const schemaKeys = Object.keys(schema.shape);
const caseInsensitiveKeymap = Object.fromEntries(
schemaKeys.map((key) => [key.toLowerCase(), key])
);
validatorValue = Object.fromEntries(
Object.entries(value).map(([key, value2]) => [caseInsensitiveKeymap[key] || key, value2])
);
}
const result = await schema.safeParseAsync(validatorValue);
if (hook) {
const hookResult = await hook({ data: validatorValue, ...result, target }, c);
if (hookResult) {
if (hookResult instanceof Response) {
return hookResult;
}
if ("response" in hookResult) {
return hookResult.response;
}
}
}
if (!result.success) {
return c.json(result, 400);
}
return result.data;
})
);
export {
zValidator
};