{"version":3,"file":"standard-schema.mjs","names":[],"sources":["../src/standard-schema.ts"],"sourcesContent":["import type {\n  StandardSchemaV1,\n  StandardJSONSchemaV1,\n} from \"@standard-schema/spec\";\n\nexport type { StandardSchemaV1, StandardJSONSchemaV1 };\n\n/**\n * Extract the Output type from a StandardSchemaV1 schema.\n * Replaces `z.infer<S>` for generic schema inference.\n */\nexport type InferSchemaOutput<S> =\n  S extends StandardSchemaV1<any, infer O> ? O : never;\n\nexport interface SchemaToJsonSchemaOptions {\n  /**\n   * Injected `zodToJsonSchema` function so that `shared` does not depend on\n   * `zod-to-json-schema`. Required when the schema is a Zod v3 schema that\n   * does not implement Standard JSON Schema V1.\n   */\n  zodToJsonSchema?: (\n    schema: unknown,\n    options?: { $refStrategy?: string },\n  ) => Record<string, unknown>;\n}\n\n/**\n * Check whether a schema implements the Standard JSON Schema V1 protocol.\n */\nfunction hasStandardJsonSchema(\n  schema: StandardSchemaV1,\n): schema is StandardSchemaV1 & StandardJSONSchemaV1 {\n  const props = schema[\"~standard\"];\n  return (\n    props != null &&\n    typeof props === \"object\" &&\n    \"jsonSchema\" in props &&\n    props.jsonSchema != null &&\n    typeof props.jsonSchema === \"object\" &&\n    \"input\" in props.jsonSchema &&\n    typeof props.jsonSchema.input === \"function\"\n  );\n}\n\n/**\n * Convert any StandardSchemaV1-compatible schema to a JSON Schema object.\n *\n * Strategy:\n * 1. If the schema implements Standard JSON Schema V1 (`~standard.jsonSchema`),\n *    call `schema['~standard'].jsonSchema.input({ target: 'draft-07' })`.\n * 2. If the schema is a Zod v3 schema (`~standard.vendor === 'zod'`), use the\n *    injected `zodToJsonSchema()` function.\n * 3. Otherwise throw a descriptive error.\n */\nexport function schemaToJsonSchema(\n  schema: StandardSchemaV1,\n  options?: SchemaToJsonSchemaOptions,\n): Record<string, unknown> {\n  // 1. Standard JSON Schema V1\n  if (hasStandardJsonSchema(schema)) {\n    return schema[\"~standard\"].jsonSchema.input({ target: \"draft-07\" });\n  }\n\n  // 2. Zod v3 fallback\n  const vendor = schema[\"~standard\"].vendor;\n  if (vendor === \"zod\" && options?.zodToJsonSchema) {\n    return options.zodToJsonSchema(schema, { $refStrategy: \"none\" });\n  }\n\n  throw new Error(\n    `Cannot convert schema to JSON Schema. The schema (vendor: \"${vendor}\") does not implement Standard JSON Schema V1 ` +\n      `and no zodToJsonSchema fallback is available. ` +\n      `Use a library that supports Standard JSON Schema (e.g., Zod 3.24+, Valibot v1+, ArkType v2+) ` +\n      `or pass a zodToJsonSchema function in options.`,\n  );\n}\n"],"mappings":";;;;AA6BA,SAAS,sBACP,QACmD;CACnD,MAAM,QAAQ,OAAO;AACrB,QACE,SAAS,QACT,OAAO,UAAU,YACjB,gBAAgB,SAChB,MAAM,cAAc,QACpB,OAAO,MAAM,eAAe,YAC5B,WAAW,MAAM,cACjB,OAAO,MAAM,WAAW,UAAU;;;;;;;;;;;;AActC,SAAgB,mBACd,QACA,SACyB;AAEzB,KAAI,sBAAsB,OAAO,CAC/B,QAAO,OAAO,aAAa,WAAW,MAAM,EAAE,QAAQ,YAAY,CAAC;CAIrE,MAAM,SAAS,OAAO,aAAa;AACnC,KAAI,WAAW,SAAS,SAAS,gBAC/B,QAAO,QAAQ,gBAAgB,QAAQ,EAAE,cAAc,QAAQ,CAAC;AAGlE,OAAM,IAAI,MACR,8DAA8D,OAAO,yOAItE"}