{"version":3,"file":"zod-to-json.cjs","sources":["../../src/zod-to-json.ts"],"sourcesContent":["import type {\n  $ZodDate,\n  $ZodUndefined,\n  $ZodUnion,\n  JSONSchema,\n  RegistryToJSONSchemaParams,\n} from 'zod/v4/core'\nimport { $ZodRegistry, $ZodType, toJSONSchema } from 'zod/v4/core'\nimport type { SchemaRegistryMeta } from './registry.ts'\nimport { getReferenceUri } from './utils.ts'\n\nconst SCHEMA_REGISTRY_ID_PLACEHOLDER = '__SCHEMA__ID__PLACEHOLDER__'\nconst SCHEMA_URI_PLACEHOLDER = '__SCHEMA__PLACEHOLDER__'\n\nfunction isZodDate(entity: unknown): entity is $ZodDate {\n  return entity instanceof $ZodType && entity._zod.def.type === 'date'\n}\n\nfunction isZodUnion(entity: unknown): entity is $ZodUnion {\n  return entity instanceof $ZodType && entity._zod.def.type === 'union'\n}\n\nfunction isZodUndefined(entity: unknown): entity is $ZodUndefined {\n  return entity instanceof $ZodType && entity._zod.def.type === 'undefined'\n}\n\nconst getOverride = (\n  ctx: {\n    zodSchema: $ZodType\n    jsonSchema: JSONSchema.BaseSchema\n  },\n  io: 'input' | 'output',\n) => {\n  if (isZodUnion(ctx.zodSchema)) {\n    // Filter unrepresentable types in unions\n    // TODO: Should be fixed upstream and not merged in this plugin.\n    // Remove when passed: https://github.com/colinhacks/zod/pull/5013\n    ctx.jsonSchema.anyOf = ctx.jsonSchema.anyOf?.filter((schema) => Object.keys(schema).length > 0)\n  }\n\n  if (isZodDate(ctx.zodSchema)) {\n    // Allow dates to be represented as strings in output schemas\n    if (io === 'output') {\n      ctx.jsonSchema.type = 'string'\n      ctx.jsonSchema.format = 'date-time'\n    }\n  }\n\n  if (isZodUndefined(ctx.zodSchema)) {\n    // Allow undefined to be represented as null in output schemas\n    if (io === 'output') {\n      ctx.jsonSchema.type = 'null'\n    }\n  }\n}\n\nexport type ZodToJsonConfig = {} & Omit<\n  RegistryToJSONSchemaParams,\n  'io' | 'metadata' | 'cycles' | 'reused' | 'uri'\n>\n\nconst composeOverride = (\n  io: 'input' | 'output',\n  override?: RegistryToJSONSchemaParams['override'],\n): RegistryToJSONSchemaParams['override'] => {\n  return (ctx) => {\n    getOverride(ctx, io)\n    override?.(ctx)\n  }\n}\n\nconst deleteInvalidProperties: (\n  schema: JSONSchema.BaseSchema,\n) => Omit<JSONSchema.BaseSchema, 'id' | '$schema'> = (schema) => {\n  const object = { ...schema }\n\n  delete object.id\n  delete object.$schema\n\n  // ToDo added in newer zod\n  delete object.$id\n\n  return object\n}\n\nexport const zodSchemaToJson: (\n  zodSchema: $ZodType,\n  registry: $ZodRegistry<SchemaRegistryMeta>,\n  io: 'input' | 'output',\n  config: ZodToJsonConfig,\n) => ReturnType<typeof deleteInvalidProperties> = (zodSchema, registry, io, config) => {\n  /**\n   * Checks whether the provided schema is registered in the given registry.\n   * If it is present and has an `id`, it can be referenced as component.\n   *\n   * @see https://github.com/turkerdev/fastify-type-provider-zod/issues/173\n   */\n  const schemaRegistryEntry = registry.get(zodSchema)\n  if (schemaRegistryEntry?.id) {\n    return { $ref: getReferenceUri(schemaRegistryEntry.id) }\n  }\n\n  /**\n   * Unfortunately, at the time of writing, there is no way to generate a schema with `$ref`\n   * using `toJSONSchema` and a zod schema.\n   *\n   * As a workaround, we create a zod registry containing only the specific schema we want to convert.\n   *\n   * @see https://github.com/colinhacks/zod/issues/4281\n   */\n  const tempRegistry = new $ZodRegistry<SchemaRegistryMeta>()\n  tempRegistry.add(zodSchema, { id: SCHEMA_REGISTRY_ID_PLACEHOLDER })\n\n  const {\n    schemas: { [SCHEMA_REGISTRY_ID_PLACEHOLDER]: result },\n  } = toJSONSchema(tempRegistry, {\n    ...config,\n    io,\n    target: config.target,\n    metadata: registry,\n    unrepresentable: config.unrepresentable ?? 'any',\n    cycles: 'ref',\n    reused: 'inline',\n    /**\n     * The uri option only allows customizing the base path of the `$ref`, and it automatically appends a path to it.\n     * As a workaround, we set a placeholder that looks something like this.\n     * @see jsonSchemaReplaceRef\n     * @see https://github.com/colinhacks/zod/issues/4750\n     */\n    uri: () => SCHEMA_URI_PLACEHOLDER,\n    override: composeOverride(io, config.override),\n  })\n\n  const jsonSchema = deleteInvalidProperties(result)\n\n  /**\n   * Replace the previous generated placeholders with the final `$ref` value\n   */\n  return JSON.parse(JSON.stringify(jsonSchema), (__key, value) => {\n    if (typeof value === 'string' && value.startsWith(SCHEMA_URI_PLACEHOLDER)) {\n      return getReferenceUri(value.slice(SCHEMA_URI_PLACEHOLDER.length))\n    }\n    return value\n  }) as typeof result\n}\n\nexport const zodRegistryToJson: (\n  registry: $ZodRegistry<SchemaRegistryMeta>,\n  io: 'input' | 'output',\n  config: ZodToJsonConfig,\n) => Record<string, JSONSchema.BaseSchema> = (registry, io, config) => {\n  const result = toJSONSchema(registry, {\n    ...config,\n    io,\n    target: config.target,\n    metadata: registry,\n    unrepresentable: config.unrepresentable ?? 'any',\n    cycles: 'ref',\n    reused: 'inline',\n    uri: (id) => getReferenceUri(id),\n    override: composeOverride(io, config.override),\n  }).schemas\n\n  const jsonSchemas: Record<string, JSONSchema.BaseSchema> = {}\n  for (const id in result) {\n    jsonSchemas[id] = deleteInvalidProperties(result[id])\n  }\n\n  return jsonSchemas\n}\n"],"names":["$ZodType","getReferenceUri","$ZodRegistry","toJSONSchema"],"mappings":";;;;AAWA,MAAM,iCAAiC;AACvC,MAAM,yBAAyB;AAE/B,SAAS,UAAU,QAAqC;AACtD,SAAO,kBAAkBA,KAAAA,YAAY,OAAO,KAAK,IAAI,SAAS;AAChE;AAEA,SAAS,WAAW,QAAsC;AACxD,SAAO,kBAAkBA,KAAAA,YAAY,OAAO,KAAK,IAAI,SAAS;AAChE;AAEA,SAAS,eAAe,QAA0C;AAChE,SAAO,kBAAkBA,KAAAA,YAAY,OAAO,KAAK,IAAI,SAAS;AAChE;AAEA,MAAM,cAAc,CAClB,KAIA,OACG;AACH,MAAI,WAAW,IAAI,SAAS,GAAG;AAI7B,QAAI,WAAW,QAAQ,IAAI,WAAW,OAAO,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