@geekmidas/schema 10.0.0-alpha.117 → 10.0.0-alpha.119

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -53,7 +53,9 @@ function extractAndConvertDefs(jsonSchema, componentCollector) {
53
53
  }
54
54
  async function convertStandardSchemaToJsonSchema(schema, componentCollector) {
55
55
  if (!schema) return;
56
- const vendor = schema["~standard"]?.vendor;
56
+ const standard = schema["~standard"];
57
+ const vendor = standard?.vendor;
58
+ if (!isSchemaVendor(vendor) && typeof standard?.jsonSchema?.output === "function") return extractAndConvertDefs(standard.jsonSchema.output({ target: "draft-2020-12" }), componentCollector);
57
59
  if (!isSchemaVendor(vendor)) throw new Error(`Unsupported or missing vendor "${vendor}" for Standard Schema. Supported vendors are: ${Object.keys(StandardSchemaJsonSchema).join(", ")}`);
58
60
  const toJSONSchema = StandardSchemaJsonSchema[vendor];
59
61
  return extractAndConvertDefs(await toJSONSchema(schema), componentCollector);
@@ -1 +1 @@
1
- {"version":3,"file":"conversion.cjs","names":[],"sources":["../src/conversion.ts"],"sourcesContent":["import type { StandardSchemaV1 } from '@standard-schema/spec';\n\nexport enum SchemaVendor {\n\tzod = 'zod',\n\tvalibot = 'valibot',\n}\n\nexport type VendorConvertor = (schema: any) => Promise<any>;\nexport type VendorConvertors = Record<SchemaVendor, VendorConvertor>;\n\nfunction isSchemaVendor(vendor?: string): vendor is SchemaVendor {\n\tif (!vendor) {\n\t\treturn false;\n\t}\n\n\treturn Object.values(SchemaVendor).includes(vendor as SchemaVendor);\n}\n\nexport const StandardSchemaJsonSchema: VendorConvertors = {\n\tzod: async (schema): Promise<any> => {\n\t\ttry {\n\t\t\tconst { z } = await import('zod/v4').catch(() => ({ z: {} }));\n\n\t\t\tif ('toJSONSchema' in z && typeof z.toJSONSchema === 'function') {\n\t\t\t\treturn z.toJSONSchema(schema);\n\t\t\t}\n\t\t\tconst { zodToJsonSchema } = await import('zod-to-json-schema');\n\n\t\t\tconst result = zodToJsonSchema(schema, {\n\t\t\t\tremoveAdditionalStrategy: 'strict',\n\t\t\t});\n\n\t\t\treturn result;\n\t\t} catch {\n\t\t\t// Fallback to basic conversion if zod-to-json-schema is not available\n\t\t\treturn { type: 'object' };\n\t\t}\n\t},\n\tvalibot: async (schema): Promise<any> => {\n\t\tconst { toJsonSchema } = await import('@valibot/to-json-schema');\n\t\treturn toJsonSchema(schema as any);\n\t},\n};\n\nfunction extractAndConvertDefs(\n\tjsonSchema: any,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): any {\n\tif (!jsonSchema || typeof jsonSchema !== 'object') {\n\t\treturn jsonSchema;\n\t}\n\n\t// Process the schema recursively to update references\n\tconst processSchema = (schema: any): any => {\n\t\tif (!schema || typeof schema !== 'object') {\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle $ref\n\t\tif (schema.$ref && typeof schema.$ref === 'string') {\n\t\t\t// Convert #/$defs/X to #/components/schemas/X\n\t\t\tif (schema.$ref.startsWith('#/$defs/')) {\n\t\t\t\tconst refName = schema.$ref.replace('#/$defs/', '');\n\t\t\t\treturn componentCollector\n\t\t\t\t\t? componentCollector.getReference(refName)\n\t\t\t\t\t: schema;\n\t\t\t}\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle arrays\n\t\tif (Array.isArray(schema)) {\n\t\t\treturn schema.map(processSchema);\n\t\t}\n\n\t\t// Process all properties recursively\n\t\tconst processed: any = {};\n\t\tfor (const [key, value] of Object.entries(schema)) {\n\t\t\t// `$defs` has been extracted. `$schema` names the dialect the converter\n\t\t\t// wrote, which the document declares once for every schema in it; a\n\t\t\t// copy on each one is noise the document does not need.\n\t\t\tif (key === '$defs' || key === '$schema') {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tprocessed[key] = processSchema(value);\n\t\t}\n\t\treturn processed;\n\t};\n\n\t// Extract $defs if present\n\tif (jsonSchema.$defs && componentCollector) {\n\t\tfor (const [defName, defSchema] of Object.entries(jsonSchema.$defs)) {\n\t\t\t// Process the definition recursively to handle nested $refs\n\t\t\tconst processedDefSchema = processSchema(defSchema);\n\t\t\t// Add each definition to the component collector\n\t\t\tcomponentCollector.addSchema(defName, processedDefSchema);\n\t\t}\n\t}\n\n\t// Process the schema and remove $defs\n\tconst { $defs, ...schemaWithoutDefs } = jsonSchema;\n\treturn processSchema(schemaWithoutDefs);\n}\n\nexport async function convertStandardSchemaToJsonSchema(\n\tschema?: StandardSchemaV1,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): Promise<any> {\n\tif (!schema) {\n\t\treturn undefined;\n\t}\n\n\tconst vendor = schema['~standard']?.vendor;\n\tif (!isSchemaVendor(vendor)) {\n\t\tthrow new Error(\n\t\t\t`Unsupported or missing vendor \"${vendor}\" for Standard Schema. Supported vendors are: ${Object.keys(StandardSchemaJsonSchema).join(', ')}`,\n\t\t);\n\t}\n\n\tconst toJSONSchema = StandardSchemaJsonSchema[vendor];\n\tconst jsonSchema = await toJSONSchema(schema);\n\n\t// Extract and convert $defs to components\n\treturn extractAndConvertDefs(jsonSchema, componentCollector);\n}\n\nexport async function getZodMetadata(\n\tschema: StandardSchemaV1,\n): Promise<SchemaMeta | undefined> {\n\tconst { ZodObject } = await import('zod/v4');\n\n\tif (schema instanceof ZodObject) {\n\t\treturn schema.meta();\n\t}\n\n\treturn undefined;\n}\n\n/**\n * Return JSON Schema for every schema registered in zod v4's global registry\n * via `.meta({ id })`. Returns an empty object when zod v4 is unavailable or\n * no schemas are registered.\n *\n * `unrepresentable: 'any'` keeps a single unrepresentable schema from\n * collapsing the whole result.\n */\nexport async function getRegisteredZodJsonSchemas(): Promise<\n\tRecord<string, any>\n> {\n\ttry {\n\t\tconst { z } = await import('zod/v4').catch(() => ({ z: null as any }));\n\t\tif (!z?.toJSONSchema || !z.globalRegistry) {\n\t\t\treturn {};\n\t\t}\n\t\tconst result = z.toJSONSchema(z.globalRegistry, {\n\t\t\tunrepresentable: 'any',\n\t\t});\n\t\treturn (result?.schemas ?? {}) as Record<string, any>;\n\t} catch {\n\t\treturn {};\n\t}\n}\n\nexport async function getSchemaMetadata(\n\tschema: StandardSchemaV1,\n): Promise<SchemaMeta | undefined> {\n\tconst vendor = schema['~standard']?.vendor;\n\n\tif (vendor === 'zod') {\n\t\treturn getZodMetadata(schema);\n\t}\n\n\treturn undefined;\n}\n\ninterface SchemaMeta {\n\tid?: string;\n}\n\nexport async function convertSchemaWithComponents(\n\tschema: StandardSchemaV1 | undefined,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): Promise<any> {\n\tif (!schema) {\n\t\treturn undefined;\n\t}\n\n\t// Convert to JSON Schema with component collector to handle $defs\n\tconst jsonSchema = await convertStandardSchemaToJsonSchema(\n\t\tschema,\n\t\tcomponentCollector,\n\t);\n\n\tif (!componentCollector) {\n\t\treturn jsonSchema;\n\t}\n\n\t// Check if this schema has metadata with an ID\n\tconst metadata = await getSchemaMetadata(schema);\n\n\t// Also check if the JSON Schema itself has an id field (from Zod's meta)\n\tconst schemaId = metadata?.id || jsonSchema?.id;\n\n\tif (schemaId) {\n\t\t// Zod 4.6 already turns a registered schema into a `$ref` to its own\n\t\t// `$defs` entry, which the conversion above moved into components. Adding\n\t\t// that reference under the same id would replace the definition with a\n\t\t// pointer to itself — `User: { $ref: '#/components/schemas/User' }` — and\n\t\t// leave the document with no User at all.\n\t\tif (typeof jsonSchema?.$ref === 'string') {\n\t\t\treturn componentCollector.getReference(schemaId);\n\t\t}\n\t\t// Remove the id from the schema before adding to components\n\t\tconst { id, ...schemaWithoutId } = jsonSchema;\n\t\t// Add this schema to components and return a reference\n\t\tcomponentCollector.addSchema(schemaId, schemaWithoutId);\n\t\treturn componentCollector.getReference(schemaId);\n\t}\n\n\treturn jsonSchema;\n}\n"],"mappings":";;AAEA,IAAY,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AAM,YAAW,MAHM,kBAAkB,MAAM,EAAA,EAGpB,MAAM,YAAY;CAE7C,IAAI,UAAU;EAMb,IAAI,OAAO,YAAY,SAAS,UAC/B,OAAO,mBAAmB,aAAa,QAAQ;EAGhD,MAAM,EAAE,IAAI,GAAG,oBAAoB;EAEnC,mBAAmB,UAAU,UAAU,eAAe;EACtD,OAAO,mBAAmB,aAAa,QAAQ;CAChD;CAEA,OAAO;AACR"}
1
+ {"version":3,"file":"conversion.cjs","names":[],"sources":["../src/conversion.ts"],"sourcesContent":["import type {\n\tStandardJSONSchemaV1,\n\tStandardSchemaV1,\n} from '@standard-schema/spec';\n\nexport enum SchemaVendor {\n\tzod = 'zod',\n\tvalibot = 'valibot',\n}\n\nexport type VendorConvertor = (schema: any) => Promise<any>;\nexport type VendorConvertors = Record<SchemaVendor, VendorConvertor>;\n\nfunction isSchemaVendor(vendor?: string): vendor is SchemaVendor {\n\tif (!vendor) {\n\t\treturn false;\n\t}\n\n\treturn Object.values(SchemaVendor).includes(vendor as SchemaVendor);\n}\n\nexport const StandardSchemaJsonSchema: VendorConvertors = {\n\tzod: async (schema): Promise<any> => {\n\t\ttry {\n\t\t\tconst { z } = await import('zod/v4').catch(() => ({ z: {} }));\n\n\t\t\tif ('toJSONSchema' in z && typeof z.toJSONSchema === 'function') {\n\t\t\t\treturn z.toJSONSchema(schema);\n\t\t\t}\n\t\t\tconst { zodToJsonSchema } = await import('zod-to-json-schema');\n\n\t\t\tconst result = zodToJsonSchema(schema, {\n\t\t\t\tremoveAdditionalStrategy: 'strict',\n\t\t\t});\n\n\t\t\treturn result;\n\t\t} catch {\n\t\t\t// Fallback to basic conversion if zod-to-json-schema is not available\n\t\t\treturn { type: 'object' };\n\t\t}\n\t},\n\tvalibot: async (schema): Promise<any> => {\n\t\tconst { toJsonSchema } = await import('@valibot/to-json-schema');\n\t\treturn toJsonSchema(schema as any);\n\t},\n};\n\nfunction extractAndConvertDefs(\n\tjsonSchema: any,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): any {\n\tif (!jsonSchema || typeof jsonSchema !== 'object') {\n\t\treturn jsonSchema;\n\t}\n\n\t// Process the schema recursively to update references\n\tconst processSchema = (schema: any): any => {\n\t\tif (!schema || typeof schema !== 'object') {\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle $ref\n\t\tif (schema.$ref && typeof schema.$ref === 'string') {\n\t\t\t// Convert #/$defs/X to #/components/schemas/X\n\t\t\tif (schema.$ref.startsWith('#/$defs/')) {\n\t\t\t\tconst refName = schema.$ref.replace('#/$defs/', '');\n\t\t\t\treturn componentCollector\n\t\t\t\t\t? componentCollector.getReference(refName)\n\t\t\t\t\t: schema;\n\t\t\t}\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle arrays\n\t\tif (Array.isArray(schema)) {\n\t\t\treturn schema.map(processSchema);\n\t\t}\n\n\t\t// Process all properties recursively\n\t\tconst processed: any = {};\n\t\tfor (const [key, value] of Object.entries(schema)) {\n\t\t\t// `$defs` has been extracted. `$schema` names the dialect the converter\n\t\t\t// wrote, which the document declares once for every schema in it; a\n\t\t\t// copy on each one is noise the document does not need.\n\t\t\tif (key === '$defs' || key === '$schema') {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tprocessed[key] = processSchema(value);\n\t\t}\n\t\treturn processed;\n\t};\n\n\t// Extract $defs if present\n\tif (jsonSchema.$defs && componentCollector) {\n\t\tfor (const [defName, defSchema] of Object.entries(jsonSchema.$defs)) {\n\t\t\t// Process the definition recursively to handle nested $refs\n\t\t\tconst processedDefSchema = processSchema(defSchema);\n\t\t\t// Add each definition to the component collector\n\t\t\tcomponentCollector.addSchema(defName, processedDefSchema);\n\t\t}\n\t}\n\n\t// Process the schema and remove $defs\n\tconst { $defs, ...schemaWithoutDefs } = jsonSchema;\n\treturn processSchema(schemaWithoutDefs);\n}\n\nexport async function convertStandardSchemaToJsonSchema(\n\tschema?: StandardSchemaV1,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): Promise<any> {\n\tif (!schema) {\n\t\treturn undefined;\n\t}\n\n\tconst standard = schema['~standard'] as StandardSchemaV1.Props &\n\t\tPartial<StandardJSONSchemaV1.Props>;\n\tconst vendor = standard?.vendor;\n\t// A schema that converts itself (Standard JSON Schema) needs no vendor\n\t// convertor — one written by hand, with no schema library behind it, can\n\t// still be documented.\n\tif (\n\t\t!isSchemaVendor(vendor) &&\n\t\ttypeof standard?.jsonSchema?.output === 'function'\n\t) {\n\t\treturn extractAndConvertDefs(\n\t\t\tstandard.jsonSchema.output({ target: 'draft-2020-12' }),\n\t\t\tcomponentCollector,\n\t\t);\n\t}\n\tif (!isSchemaVendor(vendor)) {\n\t\tthrow new Error(\n\t\t\t`Unsupported or missing vendor \"${vendor}\" for Standard Schema. Supported vendors are: ${Object.keys(StandardSchemaJsonSchema).join(', ')}`,\n\t\t);\n\t}\n\n\tconst toJSONSchema = StandardSchemaJsonSchema[vendor];\n\tconst jsonSchema = await toJSONSchema(schema);\n\n\t// Extract and convert $defs to components\n\treturn extractAndConvertDefs(jsonSchema, componentCollector);\n}\n\nexport async function getZodMetadata(\n\tschema: StandardSchemaV1,\n): Promise<SchemaMeta | undefined> {\n\tconst { ZodObject } = await import('zod/v4');\n\n\tif (schema instanceof ZodObject) {\n\t\treturn schema.meta();\n\t}\n\n\treturn undefined;\n}\n\n/**\n * Return JSON Schema for every schema registered in zod v4's global registry\n * via `.meta({ id })`. Returns an empty object when zod v4 is unavailable or\n * no schemas are registered.\n *\n * `unrepresentable: 'any'` keeps a single unrepresentable schema from\n * collapsing the whole result.\n */\nexport async function getRegisteredZodJsonSchemas(): Promise<\n\tRecord<string, any>\n> {\n\ttry {\n\t\tconst { z } = await import('zod/v4').catch(() => ({ z: null as any }));\n\t\tif (!z?.toJSONSchema || !z.globalRegistry) {\n\t\t\treturn {};\n\t\t}\n\t\tconst result = z.toJSONSchema(z.globalRegistry, {\n\t\t\tunrepresentable: 'any',\n\t\t});\n\t\treturn (result?.schemas ?? {}) as Record<string, any>;\n\t} catch {\n\t\treturn {};\n\t}\n}\n\nexport async function getSchemaMetadata(\n\tschema: StandardSchemaV1,\n): Promise<SchemaMeta | undefined> {\n\tconst vendor = schema['~standard']?.vendor;\n\n\tif (vendor === 'zod') {\n\t\treturn getZodMetadata(schema);\n\t}\n\n\treturn undefined;\n}\n\ninterface SchemaMeta {\n\tid?: string;\n}\n\nexport async function convertSchemaWithComponents(\n\tschema: StandardSchemaV1 | undefined,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): Promise<any> {\n\tif (!schema) {\n\t\treturn undefined;\n\t}\n\n\t// Convert to JSON Schema with component collector to handle $defs\n\tconst jsonSchema = await convertStandardSchemaToJsonSchema(\n\t\tschema,\n\t\tcomponentCollector,\n\t);\n\n\tif (!componentCollector) {\n\t\treturn jsonSchema;\n\t}\n\n\t// Check if this schema has metadata with an ID\n\tconst metadata = await getSchemaMetadata(schema);\n\n\t// Also check if the JSON Schema itself has an id field (from Zod's meta)\n\tconst schemaId = metadata?.id || jsonSchema?.id;\n\n\tif (schemaId) {\n\t\t// Zod 4.6 already turns a registered schema into a `$ref` to its own\n\t\t// `$defs` entry, which the conversion above moved into components. Adding\n\t\t// that reference under the same id would replace the definition with a\n\t\t// pointer to itself — `User: { $ref: '#/components/schemas/User' }` — and\n\t\t// leave the document with no User at all.\n\t\tif (typeof jsonSchema?.$ref === 'string') {\n\t\t\treturn componentCollector.getReference(schemaId);\n\t\t}\n\t\t// Remove the id from the schema before adding to components\n\t\tconst { id, ...schemaWithoutId } = jsonSchema;\n\t\t// Add this schema to components and return a reference\n\t\tcomponentCollector.addSchema(schemaId, schemaWithoutId);\n\t\treturn componentCollector.getReference(schemaId);\n\t}\n\n\treturn jsonSchema;\n}\n"],"mappings":";;AAKA,IAAY,eAAL,yBAA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;AAMA,eAAsB,4BACrB,QACA,oBAIe;CACf,IAAI,CAAC,QACJ;CAID,MAAM,aAAa,MAAM,kCACxB,QACA,kBACD;CAEA,IAAI,CAAC,oBACJ,OAAO;CAOR,MAAM,YAAW,MAHM,kBAAkB,MAAM,EAAA,EAGpB,MAAM,YAAY;CAE7C,IAAI,UAAU;EAMb,IAAI,OAAO,YAAY,SAAS,UAC/B,OAAO,mBAAmB,aAAa,QAAQ;EAGhD,MAAM,EAAE,IAAI,GAAG,oBAAoB;EAEnC,mBAAmB,UAAU,UAAU,eAAe;EACtD,OAAO,mBAAmB,aAAa,QAAQ;CAChD;CAEA,OAAO;AACR"}
@@ -1 +1 @@
1
- {"version":3,"file":"conversion.d.cts","names":[],"sources":["../src/conversion.ts"],"mappings":";;oBAEY;EACX;EACA;;YAGW,mBAAmB,gBAAgB;YACnC,mBAAmB,OAAO,cAAc;qBAUvC,0BAA0B;wBAyFjB,kCACrB,SAAS,kBACT;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B;wBAmBmB,eACrB,QAAQ,mBACN,QAAQ;;;;;;;;;wBAkBW,+BAA+B,QACpD;wBAgBqB,kBACrB,QAAQ,mBACN,QAAQ;UAUD;EACT;;wBAGqB,4BACrB,QAAQ,8BACR;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B"}
1
+ {"version":3,"file":"conversion.d.cts","names":[],"sources":["../src/conversion.ts"],"mappings":";;oBAKY;EACX;EACA;;YAGW,mBAAmB,gBAAgB;YACnC,mBAAmB,OAAO,cAAc;qBAUvC,0BAA0B;wBAyFjB,kCACrB,SAAS,kBACT;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B;wBAiCmB,eACrB,QAAQ,mBACN,QAAQ;;;;;;;;;wBAkBW,+BAA+B,QACpD;wBAgBqB,kBACrB,QAAQ,mBACN,QAAQ;UAUD;EACT;;wBAGqB,4BACrB,QAAQ,8BACR;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B"}
@@ -1 +1 @@
1
- {"version":3,"file":"conversion.d.mts","names":[],"sources":["../src/conversion.ts"],"mappings":";;oBAEY;EACX;EACA;;YAGW,mBAAmB,gBAAgB;YACnC,mBAAmB,OAAO,cAAc;qBAUvC,0BAA0B;wBAyFjB,kCACrB,SAAS,kBACT;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B;wBAmBmB,eACrB,QAAQ,mBACN,QAAQ;;;;;;;;;wBAkBW,+BAA+B,QACpD;wBAgBqB,kBACrB,QAAQ,mBACN,QAAQ;UAUD;EACT;;wBAGqB,4BACrB,QAAQ,8BACR;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B"}
1
+ {"version":3,"file":"conversion.d.mts","names":[],"sources":["../src/conversion.ts"],"mappings":";;oBAKY;EACX;EACA;;YAGW,mBAAmB,gBAAgB;YACnC,mBAAmB,OAAO,cAAc;qBAUvC,0BAA0B;wBAyFjB,kCACrB,SAAS,kBACT;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B;wBAiCmB,eACrB,QAAQ,mBACN,QAAQ;;;;;;;;;wBAkBW,+BAA+B,QACpD;wBAgBqB,kBACrB,QAAQ,mBACN,QAAQ;UAUD;EACT;;wBAGqB,4BACrB,QAAQ,8BACR;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B"}
@@ -52,7 +52,9 @@ function extractAndConvertDefs(jsonSchema, componentCollector) {
52
52
  }
53
53
  async function convertStandardSchemaToJsonSchema(schema, componentCollector) {
54
54
  if (!schema) return;
55
- const vendor = schema["~standard"]?.vendor;
55
+ const standard = schema["~standard"];
56
+ const vendor = standard?.vendor;
57
+ if (!isSchemaVendor(vendor) && typeof standard?.jsonSchema?.output === "function") return extractAndConvertDefs(standard.jsonSchema.output({ target: "draft-2020-12" }), componentCollector);
56
58
  if (!isSchemaVendor(vendor)) throw new Error(`Unsupported or missing vendor "${vendor}" for Standard Schema. Supported vendors are: ${Object.keys(StandardSchemaJsonSchema).join(", ")}`);
57
59
  const toJSONSchema = StandardSchemaJsonSchema[vendor];
58
60
  return extractAndConvertDefs(await toJSONSchema(schema), componentCollector);
@@ -1 +1 @@
1
- {"version":3,"file":"conversion.mjs","names":[],"sources":["../src/conversion.ts"],"sourcesContent":["import type { StandardSchemaV1 } from '@standard-schema/spec';\n\nexport enum SchemaVendor {\n\tzod = 'zod',\n\tvalibot = 'valibot',\n}\n\nexport type VendorConvertor = (schema: any) => Promise<any>;\nexport type VendorConvertors = Record<SchemaVendor, VendorConvertor>;\n\nfunction isSchemaVendor(vendor?: string): vendor is SchemaVendor {\n\tif (!vendor) {\n\t\treturn false;\n\t}\n\n\treturn Object.values(SchemaVendor).includes(vendor as SchemaVendor);\n}\n\nexport const StandardSchemaJsonSchema: VendorConvertors = {\n\tzod: async (schema): Promise<any> => {\n\t\ttry {\n\t\t\tconst { z } = await import('zod/v4').catch(() => ({ z: {} }));\n\n\t\t\tif ('toJSONSchema' in z && typeof z.toJSONSchema === 'function') {\n\t\t\t\treturn z.toJSONSchema(schema);\n\t\t\t}\n\t\t\tconst { zodToJsonSchema } = await import('zod-to-json-schema');\n\n\t\t\tconst result = zodToJsonSchema(schema, {\n\t\t\t\tremoveAdditionalStrategy: 'strict',\n\t\t\t});\n\n\t\t\treturn result;\n\t\t} catch {\n\t\t\t// Fallback to basic conversion if zod-to-json-schema is not available\n\t\t\treturn { type: 'object' };\n\t\t}\n\t},\n\tvalibot: async (schema): Promise<any> => {\n\t\tconst { toJsonSchema } = await import('@valibot/to-json-schema');\n\t\treturn toJsonSchema(schema as any);\n\t},\n};\n\nfunction extractAndConvertDefs(\n\tjsonSchema: any,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): any {\n\tif (!jsonSchema || typeof jsonSchema !== 'object') {\n\t\treturn jsonSchema;\n\t}\n\n\t// Process the schema recursively to update references\n\tconst processSchema = (schema: any): any => {\n\t\tif (!schema || typeof schema !== 'object') {\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle $ref\n\t\tif (schema.$ref && typeof schema.$ref === 'string') {\n\t\t\t// Convert #/$defs/X to #/components/schemas/X\n\t\t\tif (schema.$ref.startsWith('#/$defs/')) {\n\t\t\t\tconst refName = schema.$ref.replace('#/$defs/', '');\n\t\t\t\treturn componentCollector\n\t\t\t\t\t? componentCollector.getReference(refName)\n\t\t\t\t\t: schema;\n\t\t\t}\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle arrays\n\t\tif (Array.isArray(schema)) {\n\t\t\treturn schema.map(processSchema);\n\t\t}\n\n\t\t// Process all properties recursively\n\t\tconst processed: any = {};\n\t\tfor (const [key, value] of Object.entries(schema)) {\n\t\t\t// `$defs` has been extracted. `$schema` names the dialect the converter\n\t\t\t// wrote, which the document declares once for every schema in it; a\n\t\t\t// copy on each one is noise the document does not need.\n\t\t\tif (key === '$defs' || key === '$schema') {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tprocessed[key] = processSchema(value);\n\t\t}\n\t\treturn processed;\n\t};\n\n\t// Extract $defs if present\n\tif (jsonSchema.$defs && componentCollector) {\n\t\tfor (const [defName, defSchema] of Object.entries(jsonSchema.$defs)) {\n\t\t\t// Process the definition recursively to handle nested $refs\n\t\t\tconst processedDefSchema = processSchema(defSchema);\n\t\t\t// Add each definition to the component collector\n\t\t\tcomponentCollector.addSchema(defName, processedDefSchema);\n\t\t}\n\t}\n\n\t// Process the schema and remove $defs\n\tconst { $defs, ...schemaWithoutDefs } = jsonSchema;\n\treturn processSchema(schemaWithoutDefs);\n}\n\nexport async function convertStandardSchemaToJsonSchema(\n\tschema?: StandardSchemaV1,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): Promise<any> {\n\tif (!schema) {\n\t\treturn undefined;\n\t}\n\n\tconst vendor = schema['~standard']?.vendor;\n\tif (!isSchemaVendor(vendor)) {\n\t\tthrow new Error(\n\t\t\t`Unsupported or missing vendor \"${vendor}\" for Standard Schema. Supported vendors are: ${Object.keys(StandardSchemaJsonSchema).join(', ')}`,\n\t\t);\n\t}\n\n\tconst toJSONSchema = StandardSchemaJsonSchema[vendor];\n\tconst jsonSchema = await toJSONSchema(schema);\n\n\t// Extract and convert $defs to components\n\treturn extractAndConvertDefs(jsonSchema, componentCollector);\n}\n\nexport async function getZodMetadata(\n\tschema: StandardSchemaV1,\n): Promise<SchemaMeta | undefined> {\n\tconst { ZodObject } = await import('zod/v4');\n\n\tif (schema instanceof ZodObject) {\n\t\treturn schema.meta();\n\t}\n\n\treturn undefined;\n}\n\n/**\n * Return JSON Schema for every schema registered in zod v4's global registry\n * via `.meta({ id })`. Returns an empty object when zod v4 is unavailable or\n * no schemas are registered.\n *\n * `unrepresentable: 'any'` keeps a single unrepresentable schema from\n * collapsing the whole result.\n */\nexport async function getRegisteredZodJsonSchemas(): Promise<\n\tRecord<string, any>\n> {\n\ttry {\n\t\tconst { z } = await import('zod/v4').catch(() => ({ z: null as any }));\n\t\tif (!z?.toJSONSchema || !z.globalRegistry) {\n\t\t\treturn {};\n\t\t}\n\t\tconst result = z.toJSONSchema(z.globalRegistry, {\n\t\t\tunrepresentable: 'any',\n\t\t});\n\t\treturn (result?.schemas ?? {}) as Record<string, any>;\n\t} catch {\n\t\treturn {};\n\t}\n}\n\nexport async function getSchemaMetadata(\n\tschema: StandardSchemaV1,\n): Promise<SchemaMeta | undefined> {\n\tconst vendor = schema['~standard']?.vendor;\n\n\tif (vendor === 'zod') {\n\t\treturn getZodMetadata(schema);\n\t}\n\n\treturn undefined;\n}\n\ninterface SchemaMeta {\n\tid?: string;\n}\n\nexport async function convertSchemaWithComponents(\n\tschema: StandardSchemaV1 | undefined,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): Promise<any> {\n\tif (!schema) {\n\t\treturn undefined;\n\t}\n\n\t// Convert to JSON Schema with component collector to handle $defs\n\tconst jsonSchema = await convertStandardSchemaToJsonSchema(\n\t\tschema,\n\t\tcomponentCollector,\n\t);\n\n\tif (!componentCollector) {\n\t\treturn jsonSchema;\n\t}\n\n\t// Check if this schema has metadata with an ID\n\tconst metadata = await getSchemaMetadata(schema);\n\n\t// Also check if the JSON Schema itself has an id field (from Zod's meta)\n\tconst schemaId = metadata?.id || jsonSchema?.id;\n\n\tif (schemaId) {\n\t\t// Zod 4.6 already turns a registered schema into a `$ref` to its own\n\t\t// `$defs` entry, which the conversion above moved into components. Adding\n\t\t// that reference under the same id would replace the definition with a\n\t\t// pointer to itself — `User: { $ref: '#/components/schemas/User' }` — and\n\t\t// leave the document with no User at all.\n\t\tif (typeof jsonSchema?.$ref === 'string') {\n\t\t\treturn componentCollector.getReference(schemaId);\n\t\t}\n\t\t// Remove the id from the schema before adding to components\n\t\tconst { id, ...schemaWithoutId } = jsonSchema;\n\t\t// Add this schema to components and return a reference\n\t\tcomponentCollector.addSchema(schemaId, schemaWithoutId);\n\t\treturn componentCollector.getReference(schemaId);\n\t}\n\n\treturn jsonSchema;\n}\n"],"mappings":";AAEA,IAAY,eAAL,yBAAA,cAAA;CACN,aAAA,SAAA;CACA,aAAA,aAAA;;AACD,EAAA,CAAA,CAAA;AAKA,SAAS,eAAe,QAAyC;CAChE,IAAI,CAAC,QACJ,OAAO;CAGR,OAAO,OAAO,OAAO,YAAY,CAAC,CAAC,SAAS,MAAsB;AACnE;AAEA,MAAa,2BAA6C;CACzD,KAAK,OAAO,WAAyB;EACpC,IAAI;GACH,MAAM,EAAE,MAAM,MAAM,OAAO,SAAS,CAAC,aAAa,EAAE,GAAG,CAAC,EAAE,EAAE;GAE5D,IAAI,kBAAkB,KAAK,OAAO,EAAE,iBAAiB,YACpD,OAAO,EAAE,aAAa,MAAM;GAE7B,MAAM,EAAE,oBAAoB,MAAM,OAAO;GAMzC,OAJe,gBAAgB,QAAQ,EACtC,0BAA0B,SAC3B,CAEY;EACb,QAAQ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OAAO,sBAAsB,MAHJ,aAAa,MAAM,GAGH,kBAAkB;AAC5D;AAEA,eAAsB,eACrB,QACkC;CAClC,MAAM,EAAE,cAAc,MAAM,OAAO;CAEnC,IAAI,kBAAkB,WACrB,OAAO,OAAO,KAAK;AAIrB;;;;;;;;;AAUA,eAAsB,8BAEpB;CACD,IAAI;EACH,MAAM,EAAE,MAAM,MAAM,OAAO,SAAS,CAAC,aAAa,EAAE,GAAG,KAAY,EAAE;EACrE,IAAI,CAAC,GAAG,gBAAgB,CAAC,EAAE,gBAC1B,OAAO,CAAC;EAKT,OAHe,EAAE,aAAa,EAAE,gBAAgB,EAC/C,iBAAiB,MAClB,CACa,CAAC,EAAE,WAAW,CAAC;CAC7B,QAAQ;EACP,OAAO,CAAC;CACT;AACD;AAEA,eAAsB,kBACrB,QACkC;CAGlC,IAFe,OAAO,YAAY,EAAE,WAErB,OACd,OAAO,eAAe,MAAM;AAI9B;AAMA,eAAsB,4BACrB,QACA,oBAIe;CACf,IAAI,CAAC,QACJ;CAID,MAAM,aAAa,MAAM,kCACxB,QACA,kBACD;CAEA,IAAI,CAAC,oBACJ,OAAO;CAOR,MAAM,YAAW,MAHM,kBAAkB,MAAM,EAAA,EAGpB,MAAM,YAAY;CAE7C,IAAI,UAAU;EAMb,IAAI,OAAO,YAAY,SAAS,UAC/B,OAAO,mBAAmB,aAAa,QAAQ;EAGhD,MAAM,EAAE,IAAI,GAAG,oBAAoB;EAEnC,mBAAmB,UAAU,UAAU,eAAe;EACtD,OAAO,mBAAmB,aAAa,QAAQ;CAChD;CAEA,OAAO;AACR"}
1
+ {"version":3,"file":"conversion.mjs","names":[],"sources":["../src/conversion.ts"],"sourcesContent":["import type {\n\tStandardJSONSchemaV1,\n\tStandardSchemaV1,\n} from '@standard-schema/spec';\n\nexport enum SchemaVendor {\n\tzod = 'zod',\n\tvalibot = 'valibot',\n}\n\nexport type VendorConvertor = (schema: any) => Promise<any>;\nexport type VendorConvertors = Record<SchemaVendor, VendorConvertor>;\n\nfunction isSchemaVendor(vendor?: string): vendor is SchemaVendor {\n\tif (!vendor) {\n\t\treturn false;\n\t}\n\n\treturn Object.values(SchemaVendor).includes(vendor as SchemaVendor);\n}\n\nexport const StandardSchemaJsonSchema: VendorConvertors = {\n\tzod: async (schema): Promise<any> => {\n\t\ttry {\n\t\t\tconst { z } = await import('zod/v4').catch(() => ({ z: {} }));\n\n\t\t\tif ('toJSONSchema' in z && typeof z.toJSONSchema === 'function') {\n\t\t\t\treturn z.toJSONSchema(schema);\n\t\t\t}\n\t\t\tconst { zodToJsonSchema } = await import('zod-to-json-schema');\n\n\t\t\tconst result = zodToJsonSchema(schema, {\n\t\t\t\tremoveAdditionalStrategy: 'strict',\n\t\t\t});\n\n\t\t\treturn result;\n\t\t} catch {\n\t\t\t// Fallback to basic conversion if zod-to-json-schema is not available\n\t\t\treturn { type: 'object' };\n\t\t}\n\t},\n\tvalibot: async (schema): Promise<any> => {\n\t\tconst { toJsonSchema } = await import('@valibot/to-json-schema');\n\t\treturn toJsonSchema(schema as any);\n\t},\n};\n\nfunction extractAndConvertDefs(\n\tjsonSchema: any,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): any {\n\tif (!jsonSchema || typeof jsonSchema !== 'object') {\n\t\treturn jsonSchema;\n\t}\n\n\t// Process the schema recursively to update references\n\tconst processSchema = (schema: any): any => {\n\t\tif (!schema || typeof schema !== 'object') {\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle $ref\n\t\tif (schema.$ref && typeof schema.$ref === 'string') {\n\t\t\t// Convert #/$defs/X to #/components/schemas/X\n\t\t\tif (schema.$ref.startsWith('#/$defs/')) {\n\t\t\t\tconst refName = schema.$ref.replace('#/$defs/', '');\n\t\t\t\treturn componentCollector\n\t\t\t\t\t? componentCollector.getReference(refName)\n\t\t\t\t\t: schema;\n\t\t\t}\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle arrays\n\t\tif (Array.isArray(schema)) {\n\t\t\treturn schema.map(processSchema);\n\t\t}\n\n\t\t// Process all properties recursively\n\t\tconst processed: any = {};\n\t\tfor (const [key, value] of Object.entries(schema)) {\n\t\t\t// `$defs` has been extracted. `$schema` names the dialect the converter\n\t\t\t// wrote, which the document declares once for every schema in it; a\n\t\t\t// copy on each one is noise the document does not need.\n\t\t\tif (key === '$defs' || key === '$schema') {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tprocessed[key] = processSchema(value);\n\t\t}\n\t\treturn processed;\n\t};\n\n\t// Extract $defs if present\n\tif (jsonSchema.$defs && componentCollector) {\n\t\tfor (const [defName, defSchema] of Object.entries(jsonSchema.$defs)) {\n\t\t\t// Process the definition recursively to handle nested $refs\n\t\t\tconst processedDefSchema = processSchema(defSchema);\n\t\t\t// Add each definition to the component collector\n\t\t\tcomponentCollector.addSchema(defName, processedDefSchema);\n\t\t}\n\t}\n\n\t// Process the schema and remove $defs\n\tconst { $defs, ...schemaWithoutDefs } = jsonSchema;\n\treturn processSchema(schemaWithoutDefs);\n}\n\nexport async function convertStandardSchemaToJsonSchema(\n\tschema?: StandardSchemaV1,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): Promise<any> {\n\tif (!schema) {\n\t\treturn undefined;\n\t}\n\n\tconst standard = schema['~standard'] as StandardSchemaV1.Props &\n\t\tPartial<StandardJSONSchemaV1.Props>;\n\tconst vendor = standard?.vendor;\n\t// A schema that converts itself (Standard JSON Schema) needs no vendor\n\t// convertor — one written by hand, with no schema library behind it, can\n\t// still be documented.\n\tif (\n\t\t!isSchemaVendor(vendor) &&\n\t\ttypeof standard?.jsonSchema?.output === 'function'\n\t) {\n\t\treturn extractAndConvertDefs(\n\t\t\tstandard.jsonSchema.output({ target: 'draft-2020-12' }),\n\t\t\tcomponentCollector,\n\t\t);\n\t}\n\tif (!isSchemaVendor(vendor)) {\n\t\tthrow new Error(\n\t\t\t`Unsupported or missing vendor \"${vendor}\" for Standard Schema. Supported vendors are: ${Object.keys(StandardSchemaJsonSchema).join(', ')}`,\n\t\t);\n\t}\n\n\tconst toJSONSchema = StandardSchemaJsonSchema[vendor];\n\tconst jsonSchema = await toJSONSchema(schema);\n\n\t// Extract and convert $defs to components\n\treturn extractAndConvertDefs(jsonSchema, componentCollector);\n}\n\nexport async function getZodMetadata(\n\tschema: StandardSchemaV1,\n): Promise<SchemaMeta | undefined> {\n\tconst { ZodObject } = await import('zod/v4');\n\n\tif (schema instanceof ZodObject) {\n\t\treturn schema.meta();\n\t}\n\n\treturn undefined;\n}\n\n/**\n * Return JSON Schema for every schema registered in zod v4's global registry\n * via `.meta({ id })`. Returns an empty object when zod v4 is unavailable or\n * no schemas are registered.\n *\n * `unrepresentable: 'any'` keeps a single unrepresentable schema from\n * collapsing the whole result.\n */\nexport async function getRegisteredZodJsonSchemas(): Promise<\n\tRecord<string, any>\n> {\n\ttry {\n\t\tconst { z } = await import('zod/v4').catch(() => ({ z: null as any }));\n\t\tif (!z?.toJSONSchema || !z.globalRegistry) {\n\t\t\treturn {};\n\t\t}\n\t\tconst result = z.toJSONSchema(z.globalRegistry, {\n\t\t\tunrepresentable: 'any',\n\t\t});\n\t\treturn (result?.schemas ?? {}) as Record<string, any>;\n\t} catch {\n\t\treturn {};\n\t}\n}\n\nexport async function getSchemaMetadata(\n\tschema: StandardSchemaV1,\n): Promise<SchemaMeta | undefined> {\n\tconst vendor = schema['~standard']?.vendor;\n\n\tif (vendor === 'zod') {\n\t\treturn getZodMetadata(schema);\n\t}\n\n\treturn undefined;\n}\n\ninterface SchemaMeta {\n\tid?: string;\n}\n\nexport async function convertSchemaWithComponents(\n\tschema: StandardSchemaV1 | undefined,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): Promise<any> {\n\tif (!schema) {\n\t\treturn undefined;\n\t}\n\n\t// Convert to JSON Schema with component collector to handle $defs\n\tconst jsonSchema = await convertStandardSchemaToJsonSchema(\n\t\tschema,\n\t\tcomponentCollector,\n\t);\n\n\tif (!componentCollector) {\n\t\treturn jsonSchema;\n\t}\n\n\t// Check if this schema has metadata with an ID\n\tconst metadata = await getSchemaMetadata(schema);\n\n\t// Also check if the JSON Schema itself has an id field (from Zod's meta)\n\tconst schemaId = metadata?.id || jsonSchema?.id;\n\n\tif (schemaId) {\n\t\t// Zod 4.6 already turns a registered schema into a `$ref` to its own\n\t\t// `$defs` entry, which the conversion above moved into components. Adding\n\t\t// that reference under the same id would replace the definition with a\n\t\t// pointer to itself — `User: { $ref: '#/components/schemas/User' }` — and\n\t\t// leave the document with no User at all.\n\t\tif (typeof jsonSchema?.$ref === 'string') {\n\t\t\treturn componentCollector.getReference(schemaId);\n\t\t}\n\t\t// Remove the id from the schema before adding to components\n\t\tconst { id, ...schemaWithoutId } = jsonSchema;\n\t\t// Add this schema to components and return a reference\n\t\tcomponentCollector.addSchema(schemaId, schemaWithoutId);\n\t\treturn componentCollector.getReference(schemaId);\n\t}\n\n\treturn jsonSchema;\n}\n"],"mappings":";AAKA,IAAY,eAAL,yBAAA,cAAA;CACN,aAAA,SAAA;CACA,aAAA,aAAA;;AACD,EAAA,CAAA,CAAA;AAKA,SAAS,eAAe,QAAyC;CAChE,IAAI,CAAC,QACJ,OAAO;CAGR,OAAO,OAAO,OAAO,YAAY,CAAC,CAAC,SAAS,MAAsB;AACnE;AAEA,MAAa,2BAA6C;CACzD,KAAK,OAAO,WAAyB;EACpC,IAAI;GACH,MAAM,EAAE,MAAM,MAAM,OAAO,SAAS,CAAC,aAAa,EAAE,GAAG,CAAC,EAAE,EAAE;GAE5D,IAAI,kBAAkB,KAAK,OAAO,EAAE,iBAAiB,YACpD,OAAO,EAAE,aAAa,MAAM;GAE7B,MAAM,EAAE,oBAAoB,MAAM,OAAO;GAMzC,OAJe,gBAAgB,QAAQ,EACtC,0BAA0B,SAC3B,CAEY;EACb,QAAQ;GAEP,OAAO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",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {