{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { ZodIssueCode, ZodParsedType, defaultErrorMap, ZodErrorMap } from \"zod\";\nimport i18next, { i18n } from \"i18next\";\n\nconst jsonStringifyReplacer = (_: string, value: any): any => {\n  if (typeof value === \"bigint\") {\n    return value.toString();\n  }\n  return value;\n};\n\nfunction joinValues<T extends any[]>(array: T, separator = \" | \"): string {\n  return array\n    .map((val) => (typeof val === \"string\" ? `'${val}'` : val))\n    .join(separator);\n}\n\nconst isRecord = (value: unknown): value is Record<string, unknown> => {\n  if (typeof value !== \"object\" || value === null) return false;\n\n  for (const key in value) {\n    if (!Object.prototype.hasOwnProperty.call(value, key)) return false;\n  }\n\n  return true;\n};\n\nconst getKeyAndValues = (\n  param: unknown,\n  defaultKey: string\n): {\n  values: Record<string, unknown>;\n  key: string;\n} => {\n  if (typeof param === \"string\") return { key: param, values: {} };\n\n  if (isRecord(param)) {\n    const key =\n      \"key\" in param && typeof param.key === \"string\" ? param.key : defaultKey;\n    const values =\n      \"values\" in param && isRecord(param.values) ? param.values : {};\n    return { key, values };\n  }\n\n  return { key: defaultKey, values: {} };\n};\n\nexport type MakeZodI18nMap = (option?: ZodI18nMapOption) => ZodErrorMap;\n\nexport type ZodI18nMapOption = {\n  t?: i18n[\"t\"];\n  ns?: string | readonly string[];\n  handlePath?: HandlePathOption | false;\n};\n\nexport type HandlePathOption = {\n  context?: string;\n  ns?: string | readonly string[];\n  keyPrefix?: string;\n};\n\nconst defaultNs = \"zod\";\n\nexport const makeZodI18nMap: MakeZodI18nMap = (option) => (issue, ctx) => {\n  const { t, ns, handlePath } = {\n    t: i18next.t,\n    ns: defaultNs,\n    ...option,\n    handlePath:\n      option?.handlePath !== false\n        ? {\n            context: \"with_path\",\n            ns: option?.ns ?? defaultNs,\n            keyPrefix: undefined,\n            ...option?.handlePath,\n          }\n        : null,\n  };\n\n  let message: string;\n  message = defaultErrorMap(issue, ctx).message;\n\n  const path =\n    issue.path.length > 0 && !!handlePath\n      ? {\n          context: handlePath.context,\n          path: t(\n            [handlePath.keyPrefix, issue.path.join(\".\")]\n              .filter(Boolean)\n              .join(\".\"),\n            {\n              ns: handlePath.ns,\n              defaultValue: issue.path.join(\".\"),\n            }\n          ),\n        }\n      : {};\n\n  switch (issue.code) {\n    case ZodIssueCode.invalid_type:\n      if (issue.received === ZodParsedType.undefined) {\n        message = t(\"errors.invalid_type_received_undefined\", {\n          ns,\n          defaultValue: message,\n          ...path,\n        });\n      } else if (issue.received === ZodParsedType.null) {\n        message = t(\"errors.invalid_type_received_null\", {\n          ns,\n          defaultValue: message,\n          ...path,\n        });\n      } else {\n        message = t(\"errors.invalid_type\", {\n          expected: t(`types.${issue.expected}`, {\n            defaultValue: issue.expected,\n            ns,\n          }),\n          received: t(`types.${issue.received}`, {\n            defaultValue: issue.received,\n            ns,\n          }),\n          ns,\n          defaultValue: message,\n          ...path,\n        });\n      }\n      break;\n    case ZodIssueCode.invalid_literal:\n      message = t(\"errors.invalid_literal\", {\n        expected: JSON.stringify(issue.expected, jsonStringifyReplacer),\n        ns,\n        defaultValue: message,\n        ...path,\n      });\n      break;\n    case ZodIssueCode.unrecognized_keys:\n      message = t(\"errors.unrecognized_keys\", {\n        keys: joinValues(issue.keys, \", \"),\n        count: issue.keys.length,\n        ns,\n        defaultValue: message,\n        ...path,\n      });\n      break;\n    case ZodIssueCode.invalid_union:\n      message = t(\"errors.invalid_union\", {\n        ns,\n        defaultValue: message,\n        ...path,\n      });\n      break;\n    case ZodIssueCode.invalid_union_discriminator:\n      message = t(\"errors.invalid_union_discriminator\", {\n        options: joinValues(issue.options),\n        ns,\n        defaultValue: message,\n        ...path,\n      });\n      break;\n    case ZodIssueCode.invalid_enum_value:\n      message = t(\"errors.invalid_enum_value\", {\n        options: joinValues(issue.options),\n        received: issue.received,\n        ns,\n        defaultValue: message,\n        ...path,\n      });\n      break;\n    case ZodIssueCode.invalid_arguments:\n      message = t(\"errors.invalid_arguments\", {\n        ns,\n        defaultValue: message,\n        ...path,\n      });\n      break;\n    case ZodIssueCode.invalid_return_type:\n      message = t(\"errors.invalid_return_type\", {\n        ns,\n        defaultValue: message,\n        ...path,\n      });\n      break;\n    case ZodIssueCode.invalid_date:\n      message = t(\"errors.invalid_date\", {\n        ns,\n        defaultValue: message,\n        ...path,\n      });\n      break;\n    case ZodIssueCode.invalid_string:\n      if (typeof issue.validation === \"object\") {\n        if (\"startsWith\" in issue.validation) {\n          message = t(`errors.invalid_string.startsWith`, {\n            startsWith: issue.validation.startsWith,\n            ns,\n            defaultValue: message,\n            ...path,\n          });\n        } else if (\"endsWith\" in issue.validation) {\n          message = t(`errors.invalid_string.endsWith`, {\n            endsWith: issue.validation.endsWith,\n            ns,\n            defaultValue: message,\n            ...path,\n          });\n        }\n      } else {\n        message = t(`errors.invalid_string.${issue.validation}`, {\n          validation: t(`validations.${issue.validation}`, {\n            defaultValue: issue.validation,\n            ns,\n          }),\n          ns,\n          defaultValue: message,\n          ...path,\n        });\n      }\n      break;\n    case ZodIssueCode.too_small:\n      const minimum =\n        issue.type === \"date\"\n          ? new Date(issue.minimum as number)\n          : issue.minimum;\n      message = t(\n        `errors.too_small.${issue.type}.${\n          issue.exact\n            ? \"exact\"\n            : issue.inclusive\n            ? \"inclusive\"\n            : \"not_inclusive\"\n        }`,\n        {\n          minimum,\n          count: typeof minimum === \"number\" ? minimum : undefined,\n          ns,\n          defaultValue: message,\n          ...path,\n        }\n      );\n      break;\n    case ZodIssueCode.too_big:\n      const maximum =\n        issue.type === \"date\"\n          ? new Date(issue.maximum as number)\n          : issue.maximum;\n      message = t(\n        `errors.too_big.${issue.type}.${\n          issue.exact\n            ? \"exact\"\n            : issue.inclusive\n            ? \"inclusive\"\n            : \"not_inclusive\"\n        }`,\n        {\n          maximum,\n          count: typeof maximum === \"number\" ? maximum : undefined,\n          ns,\n          defaultValue: message,\n          ...path,\n        }\n      );\n      break;\n    case ZodIssueCode.custom:\n      const { key, values } = getKeyAndValues(\n        issue.params?.i18n,\n        \"errors.custom\"\n      );\n\n      message = t(key, {\n        ...values,\n        ns,\n        defaultValue: message,\n        ...path,\n      });\n      break;\n    case ZodIssueCode.invalid_intersection_types:\n      message = t(\"errors.invalid_intersection_types\", {\n        ns,\n        defaultValue: message,\n        ...path,\n      });\n      break;\n    case ZodIssueCode.not_multiple_of:\n      message = t(\"errors.not_multiple_of\", {\n        multipleOf: issue.multipleOf,\n        ns,\n        defaultValue: message,\n        ...path,\n      });\n      break;\n    case ZodIssueCode.not_finite:\n      message = t(\"errors.not_finite\", {\n        ns,\n        defaultValue: message,\n        ...path,\n      });\n      break;\n    default:\n  }\n\n  return { message };\n};\n\nexport const zodI18nMap = makeZodI18nMap();\n"],"mappings":";AAAA,SAAS,cAAc,eAAe,uBAAoC;AAC1E,OAAO,aAAuB;AAE9B,IAAM,wBAAwB,CAAC,GAAW,UAAoB;AAC5D,MAAI,OAAO,UAAU,UAAU;AAC7B,WAAO,MAAM,SAAS;AAAA,EACxB;AACA,SAAO;AACT;AAEA,SAAS,WAA4B,OAAU,YAAY,OAAe;AACxE,SAAO,MACJ,IAAI,CAAC,QAAS,OAAO,QAAQ,WAAW,IAAI,SAAS,GAAI,EACzD,KAAK,SAAS;AACnB;AAEA,IAAM,WAAW,CAAC,UAAqD;AACrE,MAAI,OAAO,UAAU,YAAY,UAAU;AAAM,WAAO;AAExD,aAAW,OAAO,OAAO;AACvB,QAAI,CAAC,OAAO,UAAU,eAAe,KAAK,OAAO,GAAG;AAAG,aAAO;AAAA,EAChE;AAEA,SAAO;AACT;AAEA,IAAM,kBAAkB,CACtB,OACA,eAIG;AACH,MAAI,OAAO,UAAU;AAAU,WAAO,EAAE,KAAK,OAAO,QAAQ,CAAC,EAAE;AAE/D,MAAI,SAAS,KAAK,GAAG;AACnB,UAAM,MACJ,SAAS,SAAS,OAAO,MAAM,QAAQ,WAAW,MAAM,MAAM;AAChE,UAAM,SACJ,YAAY,SAAS,SAAS,MAAM,MAAM,IAAI,MAAM,SAAS,CAAC;AAChE,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