@geekmidas/schema 10.0.0-alpha.16 → 10.0.0-alpha.160
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.
- package/dist/conversion.cjs +31 -29
- package/dist/conversion.cjs.map +1 -1
- package/dist/conversion.d.cts +20 -7
- package/dist/conversion.d.cts.map +1 -1
- package/dist/conversion.d.mts +20 -7
- package/dist/conversion.d.mts.map +1 -1
- package/dist/conversion.mjs +29 -28
- package/dist/conversion.mjs.map +1 -1
- package/package.json +4 -5
package/dist/conversion.cjs
CHANGED
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
//#region src/conversion.ts
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
zod: async (schema) => {
|
|
14
|
-
try {
|
|
15
|
-
const { z } = await import("zod/v4").catch(() => ({ z: {} }));
|
|
16
|
-
if ("toJSONSchema" in z && typeof z.toJSONSchema === "function") return z.toJSONSchema(schema);
|
|
17
|
-
const { zodToJsonSchema } = await import("zod-to-json-schema");
|
|
18
|
-
return zodToJsonSchema(schema, { removeAdditionalStrategy: "strict" });
|
|
19
|
-
} catch {
|
|
20
|
-
return { type: "object" };
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
valibot: async (schema) => {
|
|
24
|
-
const { toJsonSchema } = await import("@valibot/to-json-schema");
|
|
25
|
-
return toJsonSchema(schema);
|
|
3
|
+
/**
|
|
4
|
+
* A schema that does not implement Standard JSON Schema
|
|
5
|
+
* (`~standard.jsonSchema`), so nothing can say what JSON Schema it is.
|
|
6
|
+
*/
|
|
7
|
+
var SchemaHasNoJsonSchema = class extends Error {
|
|
8
|
+
vendor;
|
|
9
|
+
constructor(vendor) {
|
|
10
|
+
super(`This ${vendor ? `'${vendor}' ` : ""}schema does not implement Standard JSON Schema (\`~standard.jsonSchema\`), so it cannot be converted to JSON Schema. Use a library that implements it — zod ≥ 4.2 does on every schema — or wrap the schema in its library's adapter, e.g. valibot's \`toStandardJsonSchema(schema)\` from \`@valibot/to-json-schema\`.`);
|
|
11
|
+
this.vendor = vendor;
|
|
12
|
+
this.name = "SchemaHasNoJsonSchema";
|
|
26
13
|
}
|
|
27
14
|
};
|
|
15
|
+
/** Whether a schema implements Standard JSON Schema — can say its JSON Schema. */
|
|
16
|
+
function hasJsonSchema(schema) {
|
|
17
|
+
return typeof schema["~standard"]?.jsonSchema?.output === "function";
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A schema's JSON Schema, as the schema itself says — Standard JSON Schema's
|
|
21
|
+
* `~standard.jsonSchema.output`, in draft 2020-12 — with its `$defs` intact.
|
|
22
|
+
*
|
|
23
|
+
* Converted the way the package validates: through the schema's own
|
|
24
|
+
* `~standard`, never a converter per vendor. A converter behind an `import()`
|
|
25
|
+
* is a specifier every bundler resolves, so each one would be a package every
|
|
26
|
+
* app has to install whether or not it uses that library.
|
|
27
|
+
*/
|
|
28
|
+
function toJsonSchema(schema) {
|
|
29
|
+
if (!hasJsonSchema(schema)) throw new SchemaHasNoJsonSchema(schema["~standard"]?.vendor);
|
|
30
|
+
return schema["~standard"].jsonSchema.output({ target: "draft-2020-12" });
|
|
31
|
+
}
|
|
28
32
|
function extractAndConvertDefs(jsonSchema, componentCollector) {
|
|
29
33
|
if (!jsonSchema || typeof jsonSchema !== "object") return jsonSchema;
|
|
30
34
|
const processSchema = (schema) => {
|
|
@@ -53,10 +57,7 @@ function extractAndConvertDefs(jsonSchema, componentCollector) {
|
|
|
53
57
|
}
|
|
54
58
|
async function convertStandardSchemaToJsonSchema(schema, componentCollector) {
|
|
55
59
|
if (!schema) return;
|
|
56
|
-
|
|
57
|
-
if (!isSchemaVendor(vendor)) throw new Error(`Unsupported or missing vendor "${vendor}" for Standard Schema. Supported vendors are: ${Object.keys(StandardSchemaJsonSchema).join(", ")}`);
|
|
58
|
-
const toJSONSchema = StandardSchemaJsonSchema[vendor];
|
|
59
|
-
return extractAndConvertDefs(await toJSONSchema(schema), componentCollector);
|
|
60
|
+
return extractAndConvertDefs(toJsonSchema(schema), componentCollector);
|
|
60
61
|
}
|
|
61
62
|
async function getZodMetadata(schema) {
|
|
62
63
|
const { ZodObject } = await import("zod/v4");
|
|
@@ -96,12 +97,13 @@ async function convertSchemaWithComponents(schema, componentCollector) {
|
|
|
96
97
|
return jsonSchema;
|
|
97
98
|
}
|
|
98
99
|
//#endregion
|
|
99
|
-
exports.
|
|
100
|
-
exports.StandardSchemaJsonSchema = StandardSchemaJsonSchema;
|
|
100
|
+
exports.SchemaHasNoJsonSchema = SchemaHasNoJsonSchema;
|
|
101
101
|
exports.convertSchemaWithComponents = convertSchemaWithComponents;
|
|
102
102
|
exports.convertStandardSchemaToJsonSchema = convertStandardSchemaToJsonSchema;
|
|
103
103
|
exports.getRegisteredZodJsonSchemas = getRegisteredZodJsonSchemas;
|
|
104
104
|
exports.getSchemaMetadata = getSchemaMetadata;
|
|
105
105
|
exports.getZodMetadata = getZodMetadata;
|
|
106
|
+
exports.hasJsonSchema = hasJsonSchema;
|
|
107
|
+
exports.toJsonSchema = toJsonSchema;
|
|
106
108
|
|
|
107
109
|
//# sourceMappingURL=conversion.cjs.map
|
package/dist/conversion.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversion.cjs","names":[],"sources":["../src/conversion.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"conversion.cjs","names":[],"sources":["../src/conversion.ts"],"sourcesContent":["import type {\n\tStandardJSONSchemaV1,\n\tStandardSchemaV1,\n} from '@standard-schema/spec';\n\n/**\n * A schema that does not implement Standard JSON Schema\n * (`~standard.jsonSchema`), so nothing can say what JSON Schema it is.\n */\nexport class SchemaHasNoJsonSchema extends Error {\n\tconstructor(readonly vendor: string | undefined) {\n\t\tsuper(\n\t\t\t`This ${vendor ? `'${vendor}' ` : ''}schema does not implement Standard ` +\n\t\t\t\t`JSON Schema (\\`~standard.jsonSchema\\`), so it cannot be converted to ` +\n\t\t\t\t`JSON Schema. Use a library that implements it — zod ≥ 4.2 does on ` +\n\t\t\t\t`every schema — or wrap the schema in its library's adapter, e.g. ` +\n\t\t\t\t`valibot's \\`toStandardJsonSchema(schema)\\` from \\`@valibot/to-json-schema\\`.`,\n\t\t);\n\t\tthis.name = 'SchemaHasNoJsonSchema';\n\t}\n}\n\n/** Whether a schema implements Standard JSON Schema — can say its JSON Schema. */\nexport function hasJsonSchema(\n\tschema: StandardSchemaV1,\n): schema is StandardSchemaV1 & StandardJSONSchemaV1 {\n\tconst standard = schema['~standard'] as Partial<StandardJSONSchemaV1.Props>;\n\treturn typeof standard?.jsonSchema?.output === 'function';\n}\n\n/**\n * A schema's JSON Schema, as the schema itself says — Standard JSON Schema's\n * `~standard.jsonSchema.output`, in draft 2020-12 — with its `$defs` intact.\n *\n * Converted the way the package validates: through the schema's own\n * `~standard`, never a converter per vendor. A converter behind an `import()`\n * is a specifier every bundler resolves, so each one would be a package every\n * app has to install whether or not it uses that library.\n */\nexport function toJsonSchema(schema: StandardSchemaV1): any {\n\tif (!hasJsonSchema(schema)) {\n\t\tthrow new SchemaHasNoJsonSchema(schema['~standard']?.vendor);\n\t}\n\treturn schema['~standard'].jsonSchema.output({ target: 'draft-2020-12' });\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\t// Extract and convert $defs to components\n\treturn extractAndConvertDefs(toJsonSchema(schema), 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":";;;;;;AASA,IAAa,wBAAb,cAA2C,MAAM;CAC3B;CAArB,YAAY,QAAqC;EAChD,MACC,QAAQ,SAAS,IAAI,OAAO,MAAM,GAAG,wTAKtC;EAPoB,KAAA,SAAA;EAQpB,KAAK,OAAO;CACb;AACD;;AAGA,SAAgB,cACf,QACoD;CAEpD,OAAO,OADU,OAAO,YACF,EAAE,YAAY,WAAW;AAChD;;;;;;;;;;AAWA,SAAgB,aAAa,QAA+B;CAC3D,IAAI,CAAC,cAAc,MAAM,GACxB,MAAM,IAAI,sBAAsB,OAAO,YAAY,EAAE,MAAM;CAE5D,OAAO,OAAO,YAAY,CAAC,WAAW,OAAO,EAAE,QAAQ,gBAAgB,CAAC;AACzE;AAEA,SAAS,sBACR,YACA,oBAIM;CACN,IAAI,CAAC,cAAc,OAAO,eAAe,UACxC,OAAO;CAIR,MAAM,iBAAiB,WAAqB;EAC3C,IAAI,CAAC,UAAU,OAAO,WAAW,UAChC,OAAO;EAIR,IAAI,OAAO,QAAQ,OAAO,OAAO,SAAS,UAAU;GAEnD,IAAI,OAAO,KAAK,WAAW,UAAU,GAAG;IACvC,MAAM,UAAU,OAAO,KAAK,QAAQ,YAAY,EAAE;IAClD,OAAO,qBACJ,mBAAmB,aAAa,OAAO,IACvC;GACJ;GACA,OAAO;EACR;EAGA,IAAI,MAAM,QAAQ,MAAM,GACvB,OAAO,OAAO,IAAI,a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AM,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"}
|
package/dist/conversion.d.cts
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
|
-
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
1
|
+
import { StandardJSONSchemaV1, StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
2
|
//#region src/conversion.d.ts
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
/**
|
|
4
|
+
* A schema that does not implement Standard JSON Schema
|
|
5
|
+
* (`~standard.jsonSchema`), so nothing can say what JSON Schema it is.
|
|
6
|
+
*/
|
|
7
|
+
export declare class SchemaHasNoJsonSchema extends Error {
|
|
8
|
+
readonly vendor: string | undefined;
|
|
9
|
+
constructor(vendor: string | undefined);
|
|
6
10
|
}
|
|
7
|
-
|
|
8
|
-
export
|
|
9
|
-
|
|
11
|
+
/** Whether a schema implements Standard JSON Schema — can say its JSON Schema. */
|
|
12
|
+
export declare function hasJsonSchema(schema: StandardSchemaV1): schema is StandardSchemaV1 & StandardJSONSchemaV1;
|
|
13
|
+
/**
|
|
14
|
+
* A schema's JSON Schema, as the schema itself says — Standard JSON Schema's
|
|
15
|
+
* `~standard.jsonSchema.output`, in draft 2020-12 — with its `$defs` intact.
|
|
16
|
+
*
|
|
17
|
+
* Converted the way the package validates: through the schema's own
|
|
18
|
+
* `~standard`, never a converter per vendor. A converter behind an `import()`
|
|
19
|
+
* is a specifier every bundler resolves, so each one would be a package every
|
|
20
|
+
* app has to install whether or not it uses that library.
|
|
21
|
+
*/
|
|
22
|
+
export declare function toJsonSchema(schema: StandardSchemaV1): any;
|
|
10
23
|
export declare function convertStandardSchemaToJsonSchema(schema?: StandardSchemaV1, componentCollector?: {
|
|
11
24
|
addSchema(id: string, schema: any): void;
|
|
12
25
|
getReference(id: string): {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversion.d.cts","names":[],"sources":["../src/conversion.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"conversion.d.cts","names":[],"sources":["../src/conversion.ts"],"mappings":";;;;;;qBASa,8BAA8B;WACrB;EAArB,YAAqB;;;wBAaN,cACf,QAAQ,mBACN,UAAU,mBAAmB;;;;;;;;;;wBAchB,aAAa,QAAQ;wBAsEf,kCACrB,SAAS,kBACT;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B;wBASmB,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"}
|
package/dist/conversion.d.mts
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
|
-
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
1
|
+
import { StandardJSONSchemaV1, StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
2
|
//#region src/conversion.d.ts
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
/**
|
|
4
|
+
* A schema that does not implement Standard JSON Schema
|
|
5
|
+
* (`~standard.jsonSchema`), so nothing can say what JSON Schema it is.
|
|
6
|
+
*/
|
|
7
|
+
export declare class SchemaHasNoJsonSchema extends Error {
|
|
8
|
+
readonly vendor: string | undefined;
|
|
9
|
+
constructor(vendor: string | undefined);
|
|
6
10
|
}
|
|
7
|
-
|
|
8
|
-
export
|
|
9
|
-
|
|
11
|
+
/** Whether a schema implements Standard JSON Schema — can say its JSON Schema. */
|
|
12
|
+
export declare function hasJsonSchema(schema: StandardSchemaV1): schema is StandardSchemaV1 & StandardJSONSchemaV1;
|
|
13
|
+
/**
|
|
14
|
+
* A schema's JSON Schema, as the schema itself says — Standard JSON Schema's
|
|
15
|
+
* `~standard.jsonSchema.output`, in draft 2020-12 — with its `$defs` intact.
|
|
16
|
+
*
|
|
17
|
+
* Converted the way the package validates: through the schema's own
|
|
18
|
+
* `~standard`, never a converter per vendor. A converter behind an `import()`
|
|
19
|
+
* is a specifier every bundler resolves, so each one would be a package every
|
|
20
|
+
* app has to install whether or not it uses that library.
|
|
21
|
+
*/
|
|
22
|
+
export declare function toJsonSchema(schema: StandardSchemaV1): any;
|
|
10
23
|
export declare function convertStandardSchemaToJsonSchema(schema?: StandardSchemaV1, componentCollector?: {
|
|
11
24
|
addSchema(id: string, schema: any): void;
|
|
12
25
|
getReference(id: string): {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversion.d.mts","names":[],"sources":["../src/conversion.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"conversion.d.mts","names":[],"sources":["../src/conversion.ts"],"mappings":";;;;;;qBASa,8BAA8B;WACrB;EAArB,YAAqB;;;wBAaN,cACf,QAAQ,mBACN,UAAU,mBAAmB;;;;;;;;;;wBAchB,aAAa,QAAQ;wBAsEf,kCACrB,SAAS,kBACT;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B;wBASmB,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"}
|
package/dist/conversion.mjs
CHANGED
|
@@ -1,29 +1,33 @@
|
|
|
1
1
|
//#region src/conversion.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
zod: async (schema) => {
|
|
13
|
-
try {
|
|
14
|
-
const { z } = await import("zod/v4").catch(() => ({ z: {} }));
|
|
15
|
-
if ("toJSONSchema" in z && typeof z.toJSONSchema === "function") return z.toJSONSchema(schema);
|
|
16
|
-
const { zodToJsonSchema } = await import("zod-to-json-schema");
|
|
17
|
-
return zodToJsonSchema(schema, { removeAdditionalStrategy: "strict" });
|
|
18
|
-
} catch {
|
|
19
|
-
return { type: "object" };
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
valibot: async (schema) => {
|
|
23
|
-
const { toJsonSchema } = await import("@valibot/to-json-schema");
|
|
24
|
-
return toJsonSchema(schema);
|
|
2
|
+
/**
|
|
3
|
+
* A schema that does not implement Standard JSON Schema
|
|
4
|
+
* (`~standard.jsonSchema`), so nothing can say what JSON Schema it is.
|
|
5
|
+
*/
|
|
6
|
+
var SchemaHasNoJsonSchema = class extends Error {
|
|
7
|
+
vendor;
|
|
8
|
+
constructor(vendor) {
|
|
9
|
+
super(`This ${vendor ? `'${vendor}' ` : ""}schema does not implement Standard JSON Schema (\`~standard.jsonSchema\`), so it cannot be converted to JSON Schema. Use a library that implements it — zod ≥ 4.2 does on every schema — or wrap the schema in its library's adapter, e.g. valibot's \`toStandardJsonSchema(schema)\` from \`@valibot/to-json-schema\`.`);
|
|
10
|
+
this.vendor = vendor;
|
|
11
|
+
this.name = "SchemaHasNoJsonSchema";
|
|
25
12
|
}
|
|
26
13
|
};
|
|
14
|
+
/** Whether a schema implements Standard JSON Schema — can say its JSON Schema. */
|
|
15
|
+
function hasJsonSchema(schema) {
|
|
16
|
+
return typeof schema["~standard"]?.jsonSchema?.output === "function";
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A schema's JSON Schema, as the schema itself says — Standard JSON Schema's
|
|
20
|
+
* `~standard.jsonSchema.output`, in draft 2020-12 — with its `$defs` intact.
|
|
21
|
+
*
|
|
22
|
+
* Converted the way the package validates: through the schema's own
|
|
23
|
+
* `~standard`, never a converter per vendor. A converter behind an `import()`
|
|
24
|
+
* is a specifier every bundler resolves, so each one would be a package every
|
|
25
|
+
* app has to install whether or not it uses that library.
|
|
26
|
+
*/
|
|
27
|
+
function toJsonSchema(schema) {
|
|
28
|
+
if (!hasJsonSchema(schema)) throw new SchemaHasNoJsonSchema(schema["~standard"]?.vendor);
|
|
29
|
+
return schema["~standard"].jsonSchema.output({ target: "draft-2020-12" });
|
|
30
|
+
}
|
|
27
31
|
function extractAndConvertDefs(jsonSchema, componentCollector) {
|
|
28
32
|
if (!jsonSchema || typeof jsonSchema !== "object") return jsonSchema;
|
|
29
33
|
const processSchema = (schema) => {
|
|
@@ -52,10 +56,7 @@ function extractAndConvertDefs(jsonSchema, componentCollector) {
|
|
|
52
56
|
}
|
|
53
57
|
async function convertStandardSchemaToJsonSchema(schema, componentCollector) {
|
|
54
58
|
if (!schema) return;
|
|
55
|
-
|
|
56
|
-
if (!isSchemaVendor(vendor)) throw new Error(`Unsupported or missing vendor "${vendor}" for Standard Schema. Supported vendors are: ${Object.keys(StandardSchemaJsonSchema).join(", ")}`);
|
|
57
|
-
const toJSONSchema = StandardSchemaJsonSchema[vendor];
|
|
58
|
-
return extractAndConvertDefs(await toJSONSchema(schema), componentCollector);
|
|
59
|
+
return extractAndConvertDefs(toJsonSchema(schema), componentCollector);
|
|
59
60
|
}
|
|
60
61
|
async function getZodMetadata(schema) {
|
|
61
62
|
const { ZodObject } = await import("zod/v4");
|
|
@@ -95,6 +96,6 @@ async function convertSchemaWithComponents(schema, componentCollector) {
|
|
|
95
96
|
return jsonSchema;
|
|
96
97
|
}
|
|
97
98
|
//#endregion
|
|
98
|
-
export {
|
|
99
|
+
export { SchemaHasNoJsonSchema, convertSchemaWithComponents, convertStandardSchemaToJsonSchema, getRegisteredZodJsonSchemas, getSchemaMetadata, getZodMetadata, hasJsonSchema, toJsonSchema };
|
|
99
100
|
|
|
100
101
|
//# sourceMappingURL=conversion.mjs.map
|
package/dist/conversion.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversion.mjs","names":[],"sources":["../src/conversion.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"conversion.mjs","names":[],"sources":["../src/conversion.ts"],"sourcesContent":["import type {\n\tStandardJSONSchemaV1,\n\tStandardSchemaV1,\n} from '@standard-schema/spec';\n\n/**\n * A schema that does not implement Standard JSON Schema\n * (`~standard.jsonSchema`), so nothing can say what JSON Schema it is.\n */\nexport class SchemaHasNoJsonSchema extends Error {\n\tconstructor(readonly vendor: string | undefined) {\n\t\tsuper(\n\t\t\t`This ${vendor ? `'${vendor}' ` : ''}schema does not implement Standard ` +\n\t\t\t\t`JSON Schema (\\`~standard.jsonSchema\\`), so it cannot be converted to ` +\n\t\t\t\t`JSON Schema. Use a library that implements it — zod ≥ 4.2 does on ` +\n\t\t\t\t`every schema — or wrap the schema in its library's adapter, e.g. ` +\n\t\t\t\t`valibot's \\`toStandardJsonSchema(schema)\\` from \\`@valibot/to-json-schema\\`.`,\n\t\t);\n\t\tthis.name = 'SchemaHasNoJsonSchema';\n\t}\n}\n\n/** Whether a schema implements Standard JSON Schema — can say its JSON Schema. */\nexport function hasJsonSchema(\n\tschema: StandardSchemaV1,\n): schema is StandardSchemaV1 & StandardJSONSchemaV1 {\n\tconst standard = schema['~standard'] as Partial<StandardJSONSchemaV1.Props>;\n\treturn typeof standard?.jsonSchema?.output === 'function';\n}\n\n/**\n * A schema's JSON Schema, as the schema itself says — Standard JSON Schema's\n * `~standard.jsonSchema.output`, in draft 2020-12 — with its `$defs` intact.\n *\n * Converted the way the package validates: through the schema's own\n * `~standard`, never a converter per vendor. A converter behind an `import()`\n * is a specifier every bundler resolves, so each one would be a package every\n * app has to install whether or not it uses that library.\n */\nexport function toJsonSchema(schema: StandardSchemaV1): any {\n\tif (!hasJsonSchema(schema)) {\n\t\tthrow new SchemaHasNoJsonSchema(schema['~standard']?.vendor);\n\t}\n\treturn schema['~standard'].jsonSchema.output({ target: 'draft-2020-12' });\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\t// Extract and convert $defs to components\n\treturn extractAndConvertDefs(toJsonSchema(schema), 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":";;;;;AASA,IAAa,wBAAb,cAA2C,MAAM;CAC3B;CAArB,YAAY,QAAqC;EAChD,MACC,QAAQ,SAAS,IAAI,OAAO,MAAM,GAAG,wTAKtC;EAPoB,KAAA,SAAA;EAQpB,KAAK,OAAO;CACb;AACD;;AAGA,SAAgB,cACf,QACoD;CAEpD,OAAO,OADU,OAAO,YACF,EAAE,YAAY,WAAW;AAChD;;;;;;;;;;AAWA,SAAgB,aAAa,QAA+B;CAC3D,IAAI,CAAC,cAAc,MAAM,GACxB,MAAM,IAAI,sBAAsB,OAAO,YAAY,EAAE,MAAM;CAE5D,OAAO,OAAO,YAAY,CAAC,WAAW,OAAO,EAAE,QAAQ,gBAAgB,CAAC;AACzE;AAEA,SAAS,sBACR,YACA,oBAIM;CACN,IAAI,CAAC,cAAc,OAAO,eAAe,UACxC,OAAO;CAIR,MAAM,iBAAiB,WAAqB;E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C7B,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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/schema",
|
|
3
|
-
"version": "10.0.0-alpha.
|
|
3
|
+
"version": "10.0.0-alpha.160",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -64,11 +64,10 @@
|
|
|
64
64
|
"valibot": "~1.5.0"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"
|
|
68
|
-
"zod": ">=4.6.5",
|
|
69
|
-
"zod-to-json-schema": ">=3.25.2"
|
|
67
|
+
"zod": ">=4.6.5"
|
|
70
68
|
},
|
|
71
69
|
"scripts": {
|
|
72
|
-
"ts": "tsc --noEmit --skipLibCheck src/**/*.ts"
|
|
70
|
+
"ts": "tsc --noEmit --skipLibCheck src/**/*.ts",
|
|
71
|
+
"build": "tsdown --config ../../tsdown.config.ts --filter ."
|
|
73
72
|
}
|
|
74
73
|
}
|