{"version":3,"file":"z-validator.mjs","names":[],"sources":["../../../../src/libs/http/middleware/z-validator.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type {\n\tContext,\n\tEnv,\n\tInput,\n\tMiddlewareHandler,\n\tTypedResponse,\n\tValidationTargets,\n} from \"hono\";\nimport { validator } from \"hono/validator\";\nimport type {\n\tinput,\n\toutput,\n\tZodError,\n\tZodSafeParseResult,\n\tZodType,\n\tinfer as zInfer,\n} from \"zod\";\nimport type { LucidHonoVariables } from \"../../../types/hono.js\";\n\nexport type Hook<\n\tT,\n\tE extends Env & {\n\t\tVariables: LucidHonoVariables;\n\t},\n\tP extends string,\n\tTarget extends keyof ValidationTargets = keyof ValidationTargets,\n\t// biome-ignore lint/complexity/noBannedTypes: explanation\n\tO = {},\n\t// biome-ignore lint/suspicious/noExplicitAny: explanation\n\t_Schema extends ZodType = any,\n> = (\n\tresult: (\n\t\t| { success: true; data: T }\n\t\t| { success: false; error: ZodError; data: T }\n\t) & {\n\t\ttarget: Target;\n\t},\n\tc: Context<E, P>,\n) =>\n\t| Response\n\t| void\n\t| TypedResponse<O>\n\t// biome-ignore lint/suspicious/noConfusingVoidType: explanation\n\t| Promise<Response | void | TypedResponse<O>>;\n\ntype HasUndefined<T> = undefined extends T ? true : false;\n\nexport const zValidator = <\n\tT extends ZodType,\n\tTarget extends keyof ValidationTargets,\n\tE extends Env & {\n\t\tVariables: LucidHonoVariables;\n\t},\n\tP extends string,\n\tIn = input<T>,\n\tOut = output<T>,\n\tI extends Input = {\n\t\tin: HasUndefined<In> extends true\n\t\t\t? {\n\t\t\t\t\t[K in Target]?: In extends ValidationTargets[K]\n\t\t\t\t\t\t? In\n\t\t\t\t\t\t: { [K2 in keyof In]?: ValidationTargets[K][K2] };\n\t\t\t\t}\n\t\t\t: {\n\t\t\t\t\t[K in Target]: In extends ValidationTargets[K]\n\t\t\t\t\t\t? In\n\t\t\t\t\t\t: { [K2 in keyof In]: ValidationTargets[K][K2] };\n\t\t\t\t};\n\t\tout: { [K in Target]: Out };\n\t},\n\tV extends I = I,\n\tInferredValue = zInfer<T>,\n>(\n\ttarget: Target,\n\tschema: T,\n\t// biome-ignore lint/complexity/noBannedTypes: explanation\n\thook?: Hook<InferredValue, E, P, Target, {}, T>,\n\toptions?: {\n\t\tvalidationFunction: (\n\t\t\tschema: T,\n\t\t\tvalue: ValidationTargets[Target],\n\t\t\t// biome-ignore lint/suspicious/noExplicitAny: explanation\n\t\t) => ZodSafeParseResult<any> | Promise<ZodSafeParseResult<any>>;\n\t},\n): MiddlewareHandler<E, P, V> =>\n\t// @ts-expect-error not typed well\n\tvalidator(target, async (value, c) => {\n\t\tlet validatorValue = value;\n\t\t// in case where our `target` === `header`, Hono parses all of the headers into lowercase.\n\t\t// this might not match the Zod schema, so we want to make sure that we account for that when parsing the schema.\n\t\tif (target === \"header\" && \"_def\" in schema) {\n\t\t\t// create an object that maps lowercase schema keys to lowercase\n\t\t\t// @ts-expect-error the schema is a Zod Schema\n\t\t\tconst schemaKeys = Object.keys(schema.shape);\n\t\t\tconst caseInsensitiveKeymap = Object.fromEntries(\n\t\t\t\tschemaKeys.map((key) => [key.toLowerCase(), key]),\n\t\t\t);\n\t\t\tvalidatorValue = Object.fromEntries(\n\t\t\t\tObject.entries(value).map(([key, value]) => [\n\t\t\t\t\tcaseInsensitiveKeymap[key] || key,\n\t\t\t\t\tvalue,\n\t\t\t\t]),\n\t\t\t);\n\t\t}\n\t\tconst result = options?.validationFunction\n\t\t\t? await options.validationFunction(schema, validatorValue)\n\t\t\t: await schema.safeParseAsync(validatorValue);\n\t\tif (hook) {\n\t\t\tconst hookResult = await hook(\n\t\t\t\t{ data: validatorValue, ...result, target },\n\t\t\t\tc,\n\t\t\t);\n\t\t\tif (hookResult) {\n\t\t\t\tif (hookResult instanceof Response) {\n\t\t\t\t\treturn hookResult;\n\t\t\t\t}\n\t\t\t\tif (\"response\" in hookResult) {\n\t\t\t\t\treturn hookResult.response;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (!result.success) {\n\t\t\treturn c.json(result, 400);\n\t\t}\n\t\treturn result.data as zInfer<T>;\n\t});\n\n// Convenience functions\nexport const validateJson = <T extends ZodType>(schema: T) =>\n\tzValidator(\"json\", schema);\n\nexport const validateQuery = <T extends ZodType>(schema: T) =>\n\tzValidator(\"query\", schema);\n\nexport const validateParam = <T extends ZodType>(schema: T) =>\n\tzValidator(\"param\", schema);\n"],"mappings":"2CAgDA,MAAa,GA0BZ,EACA,EAEA,EACA,IASA,EAAU,EAAQ,MAAO,EAAO,IAAM,CACrC,IAAI,EAAiB,EAGrB,GAAI,IAAW,UAAY,SAAU,EAAQ,CAG5C,IAAM,EAAa,OAAO,KAAK,EAAO,KAAK,EACrC,EAAwB,OAAO,YACpC,EAAW,IAAK,GAAQ,CAAC,EAAI,YAAY,EAAG,CAAG,CAAC,CACjD,EACA,EAAiB,OAAO,YACvB,OAAO,QAAQ,CAAK,CAAC,CAAC,KAAK,CAAC,EAAK,KAAW,CAC3C,EAAsB,IAAQ,EAC9B,CACD,CAAC,CACF,CACD,CACA,IAAM,EAAS,GAAS,mBACrB,MAAM,EAAQ,mBAAmB,EAAQ,CAAc,EACvD,MAAM,EAAO,eAAe,CAAc,EAC7C,GAAI,EAAM,CACT,IAAM,EAAa,MAAM,EACxB,CAAE,KAAM,EAAgB,GAAG,EAAQ,QAAO,EAC1C,CACD,EACA,GAAI,EAAY,CACf,GAAI,aAAsB,SACzB,OAAO,EAER,GAAI,aAAc,EACjB,OAAO,EAAW,QAEpB,CACD,CAIA,OAHK,EAAO,QAGL,EAAO,KAFN,EAAE,KAAK,EAAQ,GAAG,CAG3B,CAAC"}