{"version":3,"sources":["../../src/hono/validator.ts"],"sourcesContent":["import type { ValidationTargets } from 'hono';\nimport { validator } from 'hono/validator';\nimport type { ZodType, output as outputV4 } from 'zod';\nimport {\n  NEVER,\n  type ZodMiniType,\n  type output as outputMini,\n  pipe,\n  string,\n  transform,\n} from 'zod/mini';\nimport { type BadRequest, Details } from '../error/detail';\nimport { Status } from '../error/status';\n\nexport function zValidator<S extends ZodType | ZodMiniType>(\n  target: keyof ValidationTargets,\n  schema: S\n) {\n  return validator(target, async (value) => {\n    const result = await schema.safeParseAsync(value);\n    if (result.success) return result.data as S extends ZodType ? outputV4<S> : outputMini<S>;\n\n    const fieldViolations: BadRequest['fieldViolations'] = result.error.issues.map(\n      ({ code, path, message }) => ({\n        field: path.join('.'),\n        description: message,\n        reason: code?.toUpperCase() ?? 'INVALID_ARGUMENT',\n        localizedMessage: { locale: 'en-US', message: message },\n      })\n    );\n    const details = Details.new().badRequest({ fieldViolations });\n    throw Status.invalidArgument().error(details);\n  });\n}\n\nexport const bigintId = pipe(\n  string(),\n  transform((input, ctx) => {\n    if (!/^(0|[1-9]\\d{0,19})$/.test(input)) {\n      const message = `Invalid bigint id: ${input}`;\n      ctx.issues.push({ code: 'custom', input, message });\n      return NEVER;\n    }\n    try {\n      return BigInt(input);\n    } catch {\n      const message = `Parse bigint id: ${input} failed`;\n      ctx.issues.push({ code: 'custom', input, message });\n      return NEVER;\n    }\n  })\n);\n"],"mappings":";AACA,SAAS,iBAAiB;AAE1B;AAAA,EACE;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAA0B,eAAe;AACzC,SAAS,cAAc;AAEhB,SAAS,WACd,QACA,QACA;AACA,SAAO,UAAU,QAAQ,OAAO,UAAU;AACxC,UAAM,SAAS,MAAM,OAAO,eAAe,KAAK;AAChD,QAAI,OAAO,QAAS,QAAO,OAAO;AAElC,UAAM,kBAAiD,OAAO,MAAM,OAAO;AAAA,MACzE,CAAC,EAAE,MAAM,MAAM,QAAQ,OAAO;AAAA,QAC5B,OAAO,KAAK,KAAK,GAAG;AAAA,QACpB,aAAa;AAAA,QACb,QAAQ,MAAM,YAAY,KAAK;AAAA,QAC/B,kBAAkB,EAAE,QAAQ,SAAS,QAAiB;AAAA,MACxD;AAAA,IACF;AACA,UAAM,UAAU,QAAQ,IAAI,EAAE,WAAW,EAAE,gBAAgB,CAAC;AAC5D,UAAM,OAAO,gBAAgB,EAAE,MAAM,OAAO;AAAA,EAC9C,CAAC;AACH;AAEO,IAAM,WAAW;AAAA,EACtB,OAAO;AAAA,EACP,UAAU,CAAC,OAAO,QAAQ;AACxB,QAAI,CAAC,sBAAsB,KAAK,KAAK,GAAG;AACtC,YAAM,UAAU,sBAAsB,KAAK;AAC3C,UAAI,OAAO,KAAK,EAAE,MAAM,UAAU,OAAO,QAAQ,CAAC;AAClD,aAAO;AAAA,IACT;AACA,QAAI;AACF,aAAO,OAAO,KAAK;AAAA,IACrB,QAAQ;AACN,YAAM,UAAU,oBAAoB,KAAK;AACzC,UAAI,OAAO,KAAK,EAAE,MAAM,UAAU,OAAO,QAAQ,CAAC;AAClD,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}