@power-plant/schema 0.0.2
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/LICENSE +201 -0
- package/README.md +45 -0
- package/dist/bundle.cjs +1 -0
- package/dist/bundle.d.cts +18 -0
- package/dist/bundle.d.cts.map +1 -0
- package/dist/bundle.d.mts +18 -0
- package/dist/bundle.d.mts.map +1 -0
- package/dist/bundle.mjs +2 -0
- package/dist/bundle.mjs.map +1 -0
- package/dist/codegen.cjs +77 -0
- package/dist/codegen.d.cts +49 -0
- package/dist/codegen.d.cts.map +1 -0
- package/dist/codegen.d.mts +49 -0
- package/dist/codegen.d.mts.map +1 -0
- package/dist/codegen.mjs +78 -0
- package/dist/codegen.mjs.map +1 -0
- package/dist/constants-Cx9_Shfk.d.cts +32 -0
- package/dist/constants-Cx9_Shfk.d.cts.map +1 -0
- package/dist/constants-Cx9_Shfk.d.mts +32 -0
- package/dist/constants-Cx9_Shfk.d.mts.map +1 -0
- package/dist/constants.cjs +1 -0
- package/dist/constants.d.cts +2 -0
- package/dist/constants.d.mts +2 -0
- package/dist/constants.mjs +2 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/extract.cjs +1 -0
- package/dist/extract.d.cts +130 -0
- package/dist/extract.d.cts.map +1 -0
- package/dist/extract.d.mts +130 -0
- package/dist/extract.d.mts.map +1 -0
- package/dist/extract.mjs +1 -0
- package/dist/helpers.cjs +1 -0
- package/dist/helpers.d.cts +123 -0
- package/dist/helpers.d.cts.map +1 -0
- package/dist/helpers.d.mts +123 -0
- package/dist/helpers.d.mts.map +1 -0
- package/dist/helpers.mjs +2 -0
- package/dist/helpers.mjs.map +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +37 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +37 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1 -0
- package/dist/metadata.cjs +1 -0
- package/dist/metadata.d.cts +31 -0
- package/dist/metadata.d.cts.map +1 -0
- package/dist/metadata.d.mts +31 -0
- package/dist/metadata.d.mts.map +1 -0
- package/dist/metadata.mjs +2 -0
- package/dist/metadata.mjs.map +1 -0
- package/dist/persistence-B9pzElsC.mjs +2 -0
- package/dist/persistence-B9pzElsC.mjs.map +1 -0
- package/dist/persistence-CLni2zCP.cjs +1 -0
- package/dist/persistence.cjs +1 -0
- package/dist/persistence.d.cts +58 -0
- package/dist/persistence.d.cts.map +1 -0
- package/dist/persistence.d.mts +58 -0
- package/dist/persistence.d.mts.map +1 -0
- package/dist/persistence.mjs +1 -0
- package/dist/resolve.cjs +1 -0
- package/dist/resolve.d.cts +45 -0
- package/dist/resolve.d.cts.map +1 -0
- package/dist/resolve.d.mts +45 -0
- package/dist/resolve.d.mts.map +1 -0
- package/dist/resolve.mjs +2 -0
- package/dist/resolve.mjs.map +1 -0
- package/dist/type-checks.cjs +1 -0
- package/dist/type-checks.d.cts +324 -0
- package/dist/type-checks.d.cts.map +1 -0
- package/dist/type-checks.d.mts +324 -0
- package/dist/type-checks.d.mts.map +1 -0
- package/dist/type-checks.mjs +2 -0
- package/dist/type-checks.mjs.map +1 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.cts +1254 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +1254 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +1 -0
- package/package.json +254 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants-Cx9_Shfk.d.cts","names":[],"sources":["../src/constants.ts"],"mappings":";cAoBa,4BAAA;AAAA,cAiBA,mBAAA;EAAA;;;;;;;;cAUA,2BAAA;AAAA,cAQA,iBAAA;AAAA,cAMA,yBAAA;;;;;cAoBA,sBAAA,EAAsB,GAAA;;AAlCnC;;;cA8Ca,sBAAA,EAAsB,GAAA;AAxCzB;AAEV;;;AAFU,cAyDG,4BAAA,EAA4B,GAAA"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//#region src/constants.d.ts
|
|
2
|
+
declare const VALID_SOURCE_FILE_EXTENSIONS: string[];
|
|
3
|
+
declare const JsonSchemaTypeNames: {
|
|
4
|
+
readonly STRING: "string";
|
|
5
|
+
readonly NUMBER: "number";
|
|
6
|
+
readonly INTEGER: "integer";
|
|
7
|
+
readonly BOOLEAN: "boolean";
|
|
8
|
+
readonly NULL: "null";
|
|
9
|
+
readonly OBJECT: "object";
|
|
10
|
+
readonly ARRAY: "array";
|
|
11
|
+
};
|
|
12
|
+
declare const JSON_SCHEMA_PRIMITIVE_TYPES: readonly ["string", "number", "integer", "boolean", "null"];
|
|
13
|
+
declare const JSON_SCHEMA_TYPES: readonly ["string", "number", "integer", "boolean", "null", "array", "object"];
|
|
14
|
+
declare const JSON_SCHEMA_METADATA_KEYS: ("title" | "description" | "docs" | "examples" | "alias" | "tags" | "deprecated" | "hidden" | "ignore" | "internal" | "runtime" | "readOnly" | "writeOnly")[];
|
|
15
|
+
/**
|
|
16
|
+
* Keywords whose values are a flat record of named JSON Schema fragments.
|
|
17
|
+
* Each child schema is merged recursively with its counterpart.
|
|
18
|
+
*/
|
|
19
|
+
declare const SCHEMA_RECORD_KEYWORDS: Set<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Keywords whose value is a single JSON Schema fragment that should be
|
|
22
|
+
* recursively merged when both sides define it.
|
|
23
|
+
*/
|
|
24
|
+
declare const SCHEMA_SINGLE_KEYWORDS: Set<string>;
|
|
25
|
+
/**
|
|
26
|
+
* Keywords whose values are arrays of JSON Schema fragments that should be
|
|
27
|
+
* concatenated (rather than overridden) during a merge.
|
|
28
|
+
*/
|
|
29
|
+
declare const SCHEMA_ARRAY_CONCAT_KEYWORDS: Set<string>;
|
|
30
|
+
//#endregion
|
|
31
|
+
export { SCHEMA_ARRAY_CONCAT_KEYWORDS as a, VALID_SOURCE_FILE_EXTENSIONS as c, JsonSchemaTypeNames as i, JSON_SCHEMA_PRIMITIVE_TYPES as n, SCHEMA_RECORD_KEYWORDS as o, JSON_SCHEMA_TYPES as r, SCHEMA_SINGLE_KEYWORDS as s, JSON_SCHEMA_METADATA_KEYS as t };
|
|
32
|
+
//# sourceMappingURL=constants-Cx9_Shfk.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants-Cx9_Shfk.d.mts","names":[],"sources":["../src/constants.ts"],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=[`ts`,`cts`,`mts`,`tsx`,`js`,`cjs`,`mjs`,`jsx`,`json`,`jsonc`,`json5`,`yaml`,`yml`,`toml`],t={STRING:`string`,NUMBER:`number`,INTEGER:`integer`,BOOLEAN:`boolean`,NULL:`null`,OBJECT:`object`,ARRAY:`array`},n=[t.STRING,t.NUMBER,t.INTEGER,t.BOOLEAN,t.NULL],r=[...n,t.ARRAY,t.OBJECT],i=[`docs`,`deprecated`,`title`,`description`,`examples`,`hidden`,`ignore`,`internal`,`runtime`,`readOnly`,`writeOnly`,`alias`,`tags`],a=new Set([`properties`,`patternProperties`,`$defs`,`definitions`,`dependentSchemas`]),o=new Set([`if`,`then`,`else`,`not`,`contains`,`items`,`additionalProperties`,`unevaluatedProperties`,`propertyNames`,`unevaluatedItems`]),s=new Set([`allOf`,`anyOf`,`oneOf`]);exports.JSON_SCHEMA_METADATA_KEYS=i,exports.JSON_SCHEMA_PRIMITIVE_TYPES=n,exports.JSON_SCHEMA_TYPES=r,exports.JsonSchemaTypeNames=t,exports.SCHEMA_ARRAY_CONCAT_KEYWORDS=s,exports.SCHEMA_RECORD_KEYWORDS=a,exports.SCHEMA_SINGLE_KEYWORDS=o,exports.VALID_SOURCE_FILE_EXTENSIONS=e;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as SCHEMA_ARRAY_CONCAT_KEYWORDS, c as VALID_SOURCE_FILE_EXTENSIONS, i as JsonSchemaTypeNames, n as JSON_SCHEMA_PRIMITIVE_TYPES, o as SCHEMA_RECORD_KEYWORDS, r as JSON_SCHEMA_TYPES, s as SCHEMA_SINGLE_KEYWORDS, t as JSON_SCHEMA_METADATA_KEYS } from "./constants-Cx9_Shfk.cjs";
|
|
2
|
+
export { JSON_SCHEMA_METADATA_KEYS, JSON_SCHEMA_PRIMITIVE_TYPES, JSON_SCHEMA_TYPES, JsonSchemaTypeNames, SCHEMA_ARRAY_CONCAT_KEYWORDS, SCHEMA_RECORD_KEYWORDS, SCHEMA_SINGLE_KEYWORDS, VALID_SOURCE_FILE_EXTENSIONS };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as SCHEMA_ARRAY_CONCAT_KEYWORDS, c as VALID_SOURCE_FILE_EXTENSIONS, i as JsonSchemaTypeNames, n as JSON_SCHEMA_PRIMITIVE_TYPES, o as SCHEMA_RECORD_KEYWORDS, r as JSON_SCHEMA_TYPES, s as SCHEMA_SINGLE_KEYWORDS, t as JSON_SCHEMA_METADATA_KEYS } from "./constants-Cx9_Shfk.mjs";
|
|
2
|
+
export { JSON_SCHEMA_METADATA_KEYS, JSON_SCHEMA_PRIMITIVE_TYPES, JSON_SCHEMA_TYPES, JsonSchemaTypeNames, SCHEMA_ARRAY_CONCAT_KEYWORDS, SCHEMA_RECORD_KEYWORDS, SCHEMA_SINGLE_KEYWORDS, VALID_SOURCE_FILE_EXTENSIONS };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
const e=[`ts`,`cts`,`mts`,`tsx`,`js`,`cjs`,`mjs`,`jsx`,`json`,`jsonc`,`json5`,`yaml`,`yml`,`toml`],t={STRING:`string`,NUMBER:`number`,INTEGER:`integer`,BOOLEAN:`boolean`,NULL:`null`,OBJECT:`object`,ARRAY:`array`},n=[t.STRING,t.NUMBER,t.INTEGER,t.BOOLEAN,t.NULL],r=[...n,t.ARRAY,t.OBJECT],i=[`docs`,`deprecated`,`title`,`description`,`examples`,`hidden`,`ignore`,`internal`,`runtime`,`readOnly`,`writeOnly`,`alias`,`tags`],a=new Set([`properties`,`patternProperties`,`$defs`,`definitions`,`dependentSchemas`]),o=new Set([`if`,`then`,`else`,`not`,`contains`,`items`,`additionalProperties`,`unevaluatedProperties`,`propertyNames`,`unevaluatedItems`]),s=new Set([`allOf`,`anyOf`,`oneOf`]);export{i as JSON_SCHEMA_METADATA_KEYS,n as JSON_SCHEMA_PRIMITIVE_TYPES,r as JSON_SCHEMA_TYPES,t as JsonSchemaTypeNames,s as SCHEMA_ARRAY_CONCAT_KEYWORDS,a as SCHEMA_RECORD_KEYWORDS,o as SCHEMA_SINGLE_KEYWORDS,e as VALID_SOURCE_FILE_EXTENSIONS};
|
|
2
|
+
//# sourceMappingURL=constants.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.mjs","names":[],"sources":[],"mappings":""}
|
package/dist/extract.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require("./type-checks.cjs");const e=require("./persistence-CLni2zCP.cjs");exports.bundleReferences=e.o,exports.extract=e.s,exports.extractHash=e.c,exports.extractJsonSchema=e.l,exports.extractResolvedVariant=e.u,exports.extractSchema=e.d,exports.extractSchemaWithSource=e.f,exports.extractSource=e.p,exports.extractVariant=e.m;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { BundleOptions } from "./bundle.cjs";
|
|
2
|
+
import { ExtractedSchema, JsonSchema, SchemaInput, SchemaInputVariant, SchemaOf, SchemaSource, SchemaSourceInput, SchemaSourceVariant } from "./types.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/extract.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Bundles all external references in a JSON Schema into a single schema document by collecting all reference targets and rewriting the references to point to the bundled definitions. This ensures that the resulting schema is self-contained and can be used independently without relying on external documents.
|
|
7
|
+
*
|
|
8
|
+
* @param schema - The JSON Schema to bundle references for.
|
|
9
|
+
* @returns A new JSON Schema with all references bundled and rewritten to point to the bundled definitions.
|
|
10
|
+
*/
|
|
11
|
+
declare function bundleReferences(schema: JsonSchema): JsonSchema;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a hash string for a given schema definition input.
|
|
14
|
+
*/
|
|
15
|
+
declare function extractHash(variant: SchemaInputVariant, input: SchemaInput): string;
|
|
16
|
+
/**
|
|
17
|
+
* Extracts a JSON Schema from Zod, Standard Schema, Valibot, untyped, or JSON Schema inputs.
|
|
18
|
+
*
|
|
19
|
+
* @param schema - The schema input to extract a JSON Schema from.
|
|
20
|
+
* @returns The extracted JSON Schema, or `undefined` if the input is not a supported schema type.
|
|
21
|
+
*/
|
|
22
|
+
declare function extractJsonSchema(schema: unknown): JsonSchema | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Resolves the concrete source variant for a schema source input.
|
|
25
|
+
*
|
|
26
|
+
* @param input - The schema source input to inspect.
|
|
27
|
+
* @returns The resolved schema source variant.
|
|
28
|
+
* @throws Will throw an error when the input cannot be mapped to a supported source variant.
|
|
29
|
+
*/
|
|
30
|
+
declare function extractResolvedVariant(input: SchemaSourceInput): SchemaSourceVariant;
|
|
31
|
+
/**
|
|
32
|
+
* Determines the top-level input variant for schema extraction.
|
|
33
|
+
*
|
|
34
|
+
* @param input - The schema input to classify.
|
|
35
|
+
* @returns The resolved schema input variant.
|
|
36
|
+
*/
|
|
37
|
+
declare function extractVariant(input: SchemaInput): SchemaInputVariant;
|
|
38
|
+
/**
|
|
39
|
+
* Extracts and normalizes a JSON Schema from a concrete schema source input.
|
|
40
|
+
*
|
|
41
|
+
* @param input - The schema source input to extract from.
|
|
42
|
+
* @param variant - Optional source variant override. When omitted, the variant is inferred from the input.
|
|
43
|
+
* @returns A promise that resolves to a bundled JSON Schema.
|
|
44
|
+
* @throws Will throw an error if no valid JSON Schema can be extracted from the input.
|
|
45
|
+
*/
|
|
46
|
+
declare function extractSchema(input: SchemaSourceInput, variant?: SchemaInputVariant): Promise<JsonSchema>;
|
|
47
|
+
/**
|
|
48
|
+
* Builds source metadata for a schema input using a known source variant.
|
|
49
|
+
*
|
|
50
|
+
* @param variant - The schema source variant associated with the input.
|
|
51
|
+
* @param input - The schema source input to wrap.
|
|
52
|
+
* @returns The normalized schema source payload, including the source hash and variant.
|
|
53
|
+
* @throws Will throw an error if the provided variant is unsupported.
|
|
54
|
+
*/
|
|
55
|
+
declare function extractSource(variant: SchemaSourceVariant, input: SchemaSourceInput): SchemaSource;
|
|
56
|
+
/**
|
|
57
|
+
* Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, or a {@link FileReferenceInput} to an exported TypeScript type definition or any of the previous options. If the input is a {@link FileReferenceInput} (e.g. a file path with an export), the source code will be bundled with [esbuild](esbuild.github.io) using [ts-json-schema-generator](https://github.com/vega/ts-json-schema-generator) to obtain the actual schema definition before extraction.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* Resolve a schema definition from a JSON schema file
|
|
62
|
+
* const schema1 = await extract(context, "./schemas/my-json-schema.json");
|
|
63
|
+
* // Resolve a schema definition from a TypeScript module export
|
|
64
|
+
* const schema2 = await extract(context, "./schemas.ts#MySchema");
|
|
65
|
+
* // Resolve a schema definition from a JSON Schema object
|
|
66
|
+
* const schema3 = await extract(context, schemaObject);
|
|
67
|
+
* // Resolve a schema definition from a Zod schema
|
|
68
|
+
* const schema4 = await extract(context, zodSchema);
|
|
69
|
+
* // Resolve a schema definition from a Valibot schema
|
|
70
|
+
* const schema5 = await extract(context, valibotSchema);
|
|
71
|
+
* // Resolve a schema definition from an untyped schema
|
|
72
|
+
* const schema6 = await extract(context, untypedSchema);
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @see https://zod.dev/
|
|
76
|
+
* @see https://valibot.dev/
|
|
77
|
+
* @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec
|
|
78
|
+
* @see https://json-schema.org/
|
|
79
|
+
* @see https://ajv.js.org/json-type-definition.html
|
|
80
|
+
* @see https://github.com/vega/ts-json-schema-generator
|
|
81
|
+
* @see https://github.com/unjs/untyped
|
|
82
|
+
* @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
|
|
83
|
+
*
|
|
84
|
+
* @param input - The schema definition input to extract, which can be a Zod schema, a Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, or a {@link FileReferenceInput} to an exported TypeScript type definition or any of the previous options.
|
|
85
|
+
* @param options - Optional overrides for the configuration used during extraction.
|
|
86
|
+
* @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object.
|
|
87
|
+
* @throws Will throw an error if the input is not a valid schema definition or if the extraction process fails to produce a valid schema.
|
|
88
|
+
*/
|
|
89
|
+
declare function extractSchemaWithSource<TSpec = any>(input: SchemaInput, options?: ExtractOptions): Promise<ExtractedSchema<TSpec>>;
|
|
90
|
+
type ExtractOptions = BundleOptions & {
|
|
91
|
+
cachePath?: string;
|
|
92
|
+
skipCache?: boolean;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, or a {@link FileReferenceInput} to an exported TypeScript type definition or any of the previous options. If the input is a {@link FileReferenceInput} (e.g. a file path with an export), the source code will be bundled with [esbuild](esbuild.github.io) using [ts-json-schema-generator](https://github.com/vega/ts-json-schema-generator) to obtain the actual schema definition before extraction.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* Resolve a schema definition from a JSON schema file
|
|
100
|
+
* const schema1 = await extract(context, "./schemas/my-json-schema.json");
|
|
101
|
+
* // Resolve a schema definition from a TypeScript module export
|
|
102
|
+
* const schema2 = await extract(context, "./schemas.ts#MySchema");
|
|
103
|
+
* // Resolve a schema definition from a JSON Schema object
|
|
104
|
+
* const schema3 = await extract(context, schemaObject);
|
|
105
|
+
* // Resolve a schema definition from a Zod schema
|
|
106
|
+
* const schema4 = await extract(context, zodSchema);
|
|
107
|
+
* // Resolve a schema definition from a Valibot schema
|
|
108
|
+
* const schema5 = await extract(context, valibotSchema);
|
|
109
|
+
* // Resolve a schema definition from an untyped schema
|
|
110
|
+
* const schema6 = await extract(context, untypedSchema);
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @see https://zod.dev/
|
|
114
|
+
* @see https://valibot.dev/
|
|
115
|
+
* @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec
|
|
116
|
+
* @see https://json-schema.org/
|
|
117
|
+
* @see https://ajv.js.org/json-type-definition.html
|
|
118
|
+
* @see https://github.com/vega/ts-json-schema-generator
|
|
119
|
+
* @see https://github.com/unjs/untyped
|
|
120
|
+
* @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
|
|
121
|
+
*
|
|
122
|
+
* @param input - The schema definition input to extract, which can be a Zod schema, a Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, or a {@link FileReferenceInput} to an exported TypeScript type definition or any of the previous options.
|
|
123
|
+
* @param options - Optional overrides for the configuration used during extraction.
|
|
124
|
+
* @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object.
|
|
125
|
+
* @throws Will throw an error if the input is not a valid schema definition or if the extraction process fails to produce a valid schema.
|
|
126
|
+
*/
|
|
127
|
+
declare function extract<TSpec = any>(input: SchemaInput<TSpec>, options?: ExtractOptions): Promise<SchemaOf<TSpec>>;
|
|
128
|
+
//#endregion
|
|
129
|
+
export { ExtractOptions, bundleReferences, extract, extractHash, extractJsonSchema, extractResolvedVariant, extractSchema, extractSchemaWithSource, extractSource, extractVariant };
|
|
130
|
+
//# sourceMappingURL=extract.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract.d.cts","names":[],"sources":["../src/extract.ts"],"mappings":";;;;;;AAiRA;;;;iBAAgB,gBAAA,CAAiB,MAAA,EAAQ,UAAA,GAAa,UAAU;;;;iBAgMhD,WAAA,CACd,OAAA,EAAS,kBAAA,EACT,KAAA,EAAO,WAAW;AAFpB;;;;;;AAAA,iBAkDgB,iBAAA,CAAkB,MAAA,YAAkB,UAAU;;;AAhD1C;AAgDpB;;;;iBAoCgB,sBAAA,CACd,KAAA,EAAO,iBAAA,GACN,mBAAmB;AAFtB;;;;;;AAAA,iBA4BgB,cAAA,CAAe,KAAA,EAAO,WAAA,GAAc,kBAAkB;;AA1BhD;AA0BtB;;;;;;iBAsBsB,aAAA,CACpB,KAAA,EAAO,iBAAA,EACP,OAAA,GAAU,kBAAA,GACT,OAAA,CAAQ,UAAA;;AAzB2D;AAsBtE;;;;;;iBAsCgB,aAAA,CACd,OAAA,EAAS,mBAAA,EACT,KAAA,EAAO,iBAAA,GACN,YAAA;;;;;;;;;;AAtCkB;AAmCrB;;;;;;;;;;;;;;AAGe;AAuEf;;;;;;;;iBAAsB,uBAAA,cACpB,KAAA,EAAO,WAAA,EACP,OAAA,GAAS,cAAA,GACR,OAAA,CAAQ,eAAA,CAAgB,KAAA;AAAA,KA0Hf,cAAA,GAAiB,aAAa;EACxC,SAAA;EACA,SAAA;AAAA;;;;;;;AA5H8B;AA0HhC;;;;;;;;AAEW;AAoCX;;;;;;;;;;;;;;;;;iBAAsB,OAAA,cACpB,KAAA,EAAO,WAAA,CAAY,KAAA,GACnB,OAAA,GAAS,cAAA,GACR,OAAA,CAAQ,QAAA,CAAS,KAAA"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { BundleOptions } from "./bundle.mjs";
|
|
2
|
+
import { ExtractedSchema, JsonSchema, SchemaInput, SchemaInputVariant, SchemaOf, SchemaSource, SchemaSourceInput, SchemaSourceVariant } from "./types.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/extract.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Bundles all external references in a JSON Schema into a single schema document by collecting all reference targets and rewriting the references to point to the bundled definitions. This ensures that the resulting schema is self-contained and can be used independently without relying on external documents.
|
|
7
|
+
*
|
|
8
|
+
* @param schema - The JSON Schema to bundle references for.
|
|
9
|
+
* @returns A new JSON Schema with all references bundled and rewritten to point to the bundled definitions.
|
|
10
|
+
*/
|
|
11
|
+
declare function bundleReferences(schema: JsonSchema): JsonSchema;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a hash string for a given schema definition input.
|
|
14
|
+
*/
|
|
15
|
+
declare function extractHash(variant: SchemaInputVariant, input: SchemaInput): string;
|
|
16
|
+
/**
|
|
17
|
+
* Extracts a JSON Schema from Zod, Standard Schema, Valibot, untyped, or JSON Schema inputs.
|
|
18
|
+
*
|
|
19
|
+
* @param schema - The schema input to extract a JSON Schema from.
|
|
20
|
+
* @returns The extracted JSON Schema, or `undefined` if the input is not a supported schema type.
|
|
21
|
+
*/
|
|
22
|
+
declare function extractJsonSchema(schema: unknown): JsonSchema | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Resolves the concrete source variant for a schema source input.
|
|
25
|
+
*
|
|
26
|
+
* @param input - The schema source input to inspect.
|
|
27
|
+
* @returns The resolved schema source variant.
|
|
28
|
+
* @throws Will throw an error when the input cannot be mapped to a supported source variant.
|
|
29
|
+
*/
|
|
30
|
+
declare function extractResolvedVariant(input: SchemaSourceInput): SchemaSourceVariant;
|
|
31
|
+
/**
|
|
32
|
+
* Determines the top-level input variant for schema extraction.
|
|
33
|
+
*
|
|
34
|
+
* @param input - The schema input to classify.
|
|
35
|
+
* @returns The resolved schema input variant.
|
|
36
|
+
*/
|
|
37
|
+
declare function extractVariant(input: SchemaInput): SchemaInputVariant;
|
|
38
|
+
/**
|
|
39
|
+
* Extracts and normalizes a JSON Schema from a concrete schema source input.
|
|
40
|
+
*
|
|
41
|
+
* @param input - The schema source input to extract from.
|
|
42
|
+
* @param variant - Optional source variant override. When omitted, the variant is inferred from the input.
|
|
43
|
+
* @returns A promise that resolves to a bundled JSON Schema.
|
|
44
|
+
* @throws Will throw an error if no valid JSON Schema can be extracted from the input.
|
|
45
|
+
*/
|
|
46
|
+
declare function extractSchema(input: SchemaSourceInput, variant?: SchemaInputVariant): Promise<JsonSchema>;
|
|
47
|
+
/**
|
|
48
|
+
* Builds source metadata for a schema input using a known source variant.
|
|
49
|
+
*
|
|
50
|
+
* @param variant - The schema source variant associated with the input.
|
|
51
|
+
* @param input - The schema source input to wrap.
|
|
52
|
+
* @returns The normalized schema source payload, including the source hash and variant.
|
|
53
|
+
* @throws Will throw an error if the provided variant is unsupported.
|
|
54
|
+
*/
|
|
55
|
+
declare function extractSource(variant: SchemaSourceVariant, input: SchemaSourceInput): SchemaSource;
|
|
56
|
+
/**
|
|
57
|
+
* Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, or a {@link FileReferenceInput} to an exported TypeScript type definition or any of the previous options. If the input is a {@link FileReferenceInput} (e.g. a file path with an export), the source code will be bundled with [esbuild](esbuild.github.io) using [ts-json-schema-generator](https://github.com/vega/ts-json-schema-generator) to obtain the actual schema definition before extraction.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* Resolve a schema definition from a JSON schema file
|
|
62
|
+
* const schema1 = await extract(context, "./schemas/my-json-schema.json");
|
|
63
|
+
* // Resolve a schema definition from a TypeScript module export
|
|
64
|
+
* const schema2 = await extract(context, "./schemas.ts#MySchema");
|
|
65
|
+
* // Resolve a schema definition from a JSON Schema object
|
|
66
|
+
* const schema3 = await extract(context, schemaObject);
|
|
67
|
+
* // Resolve a schema definition from a Zod schema
|
|
68
|
+
* const schema4 = await extract(context, zodSchema);
|
|
69
|
+
* // Resolve a schema definition from a Valibot schema
|
|
70
|
+
* const schema5 = await extract(context, valibotSchema);
|
|
71
|
+
* // Resolve a schema definition from an untyped schema
|
|
72
|
+
* const schema6 = await extract(context, untypedSchema);
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @see https://zod.dev/
|
|
76
|
+
* @see https://valibot.dev/
|
|
77
|
+
* @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec
|
|
78
|
+
* @see https://json-schema.org/
|
|
79
|
+
* @see https://ajv.js.org/json-type-definition.html
|
|
80
|
+
* @see https://github.com/vega/ts-json-schema-generator
|
|
81
|
+
* @see https://github.com/unjs/untyped
|
|
82
|
+
* @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
|
|
83
|
+
*
|
|
84
|
+
* @param input - The schema definition input to extract, which can be a Zod schema, a Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, or a {@link FileReferenceInput} to an exported TypeScript type definition or any of the previous options.
|
|
85
|
+
* @param options - Optional overrides for the configuration used during extraction.
|
|
86
|
+
* @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object.
|
|
87
|
+
* @throws Will throw an error if the input is not a valid schema definition or if the extraction process fails to produce a valid schema.
|
|
88
|
+
*/
|
|
89
|
+
declare function extractSchemaWithSource<TSpec = any>(input: SchemaInput, options?: ExtractOptions): Promise<ExtractedSchema<TSpec>>;
|
|
90
|
+
type ExtractOptions = BundleOptions & {
|
|
91
|
+
cachePath?: string;
|
|
92
|
+
skipCache?: boolean;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, or a {@link FileReferenceInput} to an exported TypeScript type definition or any of the previous options. If the input is a {@link FileReferenceInput} (e.g. a file path with an export), the source code will be bundled with [esbuild](esbuild.github.io) using [ts-json-schema-generator](https://github.com/vega/ts-json-schema-generator) to obtain the actual schema definition before extraction.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* Resolve a schema definition from a JSON schema file
|
|
100
|
+
* const schema1 = await extract(context, "./schemas/my-json-schema.json");
|
|
101
|
+
* // Resolve a schema definition from a TypeScript module export
|
|
102
|
+
* const schema2 = await extract(context, "./schemas.ts#MySchema");
|
|
103
|
+
* // Resolve a schema definition from a JSON Schema object
|
|
104
|
+
* const schema3 = await extract(context, schemaObject);
|
|
105
|
+
* // Resolve a schema definition from a Zod schema
|
|
106
|
+
* const schema4 = await extract(context, zodSchema);
|
|
107
|
+
* // Resolve a schema definition from a Valibot schema
|
|
108
|
+
* const schema5 = await extract(context, valibotSchema);
|
|
109
|
+
* // Resolve a schema definition from an untyped schema
|
|
110
|
+
* const schema6 = await extract(context, untypedSchema);
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @see https://zod.dev/
|
|
114
|
+
* @see https://valibot.dev/
|
|
115
|
+
* @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec
|
|
116
|
+
* @see https://json-schema.org/
|
|
117
|
+
* @see https://ajv.js.org/json-type-definition.html
|
|
118
|
+
* @see https://github.com/vega/ts-json-schema-generator
|
|
119
|
+
* @see https://github.com/unjs/untyped
|
|
120
|
+
* @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
|
|
121
|
+
*
|
|
122
|
+
* @param input - The schema definition input to extract, which can be a Zod schema, a Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, or a {@link FileReferenceInput} to an exported TypeScript type definition or any of the previous options.
|
|
123
|
+
* @param options - Optional overrides for the configuration used during extraction.
|
|
124
|
+
* @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object.
|
|
125
|
+
* @throws Will throw an error if the input is not a valid schema definition or if the extraction process fails to produce a valid schema.
|
|
126
|
+
*/
|
|
127
|
+
declare function extract<TSpec = any>(input: SchemaInput<TSpec>, options?: ExtractOptions): Promise<SchemaOf<TSpec>>;
|
|
128
|
+
//#endregion
|
|
129
|
+
export { ExtractOptions, bundleReferences, extract, extractHash, extractJsonSchema, extractResolvedVariant, extractSchema, extractSchemaWithSource, extractSource, extractVariant };
|
|
130
|
+
//# sourceMappingURL=extract.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract.d.mts","names":[],"sources":["../src/extract.ts"],"mappings":""}
|
package/dist/extract.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import"./type-checks.mjs";import{c as e,d as t,f as n,l as r,m as i,o as a,p as o,s,u as c}from"./persistence-B9pzElsC.mjs";export{a as bundleReferences,s as extract,e as extractHash,r as extractJsonSchema,c as extractResolvedVariant,t as extractSchema,n as extractSchemaWithSource,o as extractSource,i as extractVariant};
|
package/dist/helpers.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./constants.cjs"),t=require("./type-checks.cjs"),n=require("./metadata.cjs");let r=require("@stryke/type-checks"),i=require("@stryke/path/find"),a=require("@stryke/helpers/get-unique");function o(e){let n=t.isSchema(e)?e.schema:e;if(!t.isJsonSchema(n))throw TypeError(`The provided input is not a valid JSON Schema`);return n}function s(e){let n=o(e);if(!t.isJsonSchemaObject(n))throw TypeError(`The provided input is not a valid JSON Schema object`);return n}function c(e){let t=s(e);return(0,r.isSetObject)(t.properties)?Object.entries(t.properties).reduce((e,[n,r])=>(r.name=n,r.required=!h(t,n),r.default=t.default?.[n]??r.default,e[n]=r,e),{}):{}}function l(e){return Object.values(c(e))}function u(e,t){let n=s(e);if(!(0,r.isSetObject)(n.properties))throw TypeError(`The provided schema does not have any properties`);let i=n.properties[t];if(!i)throw TypeError(`The provided schema does not have a property named "${t}"`);return{...i,name:t,required:!h(n,t),default:n.default?.[t]??i?.default}}function d(e,t,n){let r=s(e);r.properties??={},r.required??=[],n.name=t,r.properties[t]=n,r.required.includes(t)||r.required.push(t),r.required.length===0&&delete r.required}function f(n,i){let o={...n};for(let[n,s]of Object.entries(i)){let i=o[n];if(n===`required`)o[n]=(0,a.getUnique)([...Array.isArray(i)?i:[],...Array.isArray(s)?s:[]]);else if(e.SCHEMA_RECORD_KEYWORDS.has(n)&&(0,r.isSetObject)(i)&&(0,r.isSetObject)(s)){let e={...i};for(let[n,r]of Object.entries(s)){let i=e[n];e[n]=t.isJsonSchema(i)&&t.isJsonSchema(r)?f(i,r):r}o[n]=e}else e.SCHEMA_ARRAY_CONCAT_KEYWORDS.has(n)&&Array.isArray(i)&&Array.isArray(s)?o[n]=[...i,...s]:e.SCHEMA_SINGLE_KEYWORDS.has(n)&&t.isJsonSchema(i)&&t.isJsonSchema(s)?o[n]=f(i,s):o[n]=s}return o}function p(...e){let t=e.map(e=>o(e));return t.length===0?{}:t.reduce((e,t)=>{let n=e.type,r=t.type;return n&&r&&n!==r?t:f(e,t)})}function m(e){return(0,r.isSetObject)(e)?e.nullable===!0?!0:n.readSchemaTypes(e).includes(`null`):!1}function h(e,t){if(!e.properties?.[t])throw Error(`The property "${t}" does not exist in the parent schema.`);return!(e.required??[]).includes(t)}function g(t){return e.VALID_SOURCE_FILE_EXTENSIONS.includes((0,i.findFileExtensionSafe)(t))}exports.addProperty=d,exports.getJsonSchema=o,exports.getJsonSchemaObject=s,exports.getProperties=c,exports.getPropertiesList=l,exports.getProperty=u,exports.isPropertyOptional=h,exports.isSchemaNullable=m,exports.isValidSchemaInputFile=g,exports.merge=p;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { JsonSchema, JsonSchemaObject, Schema } from "./types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/helpers.d.ts
|
|
4
|
+
type GetPropertyResult = JsonSchema & {
|
|
5
|
+
name: string;
|
|
6
|
+
required: boolean;
|
|
7
|
+
default?: unknown;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Retrieves the JSON Schema from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
11
|
+
*
|
|
12
|
+
* @remarks
|
|
13
|
+
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
14
|
+
*
|
|
15
|
+
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
16
|
+
* @returns The JSON Schema object.
|
|
17
|
+
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
18
|
+
*/
|
|
19
|
+
declare function getJsonSchema(input: Schema | JsonSchema): JsonSchema;
|
|
20
|
+
/**
|
|
21
|
+
* Retrieves the JSON Schema in Object form from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema object, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
25
|
+
*
|
|
26
|
+
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
27
|
+
* @returns The JSON Schema object.
|
|
28
|
+
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
29
|
+
*/
|
|
30
|
+
declare function getJsonSchemaObject(input: Schema | JsonSchema): JsonSchemaObject;
|
|
31
|
+
/**
|
|
32
|
+
* Extracts object properties from a JSON Schema object form.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* This function returns an empty object if the schema is not an object form or if it has no properties.
|
|
36
|
+
*
|
|
37
|
+
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
38
|
+
* @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.
|
|
39
|
+
*/
|
|
40
|
+
declare function getProperties(obj: Schema | JsonSchemaObject): Record<string, GetPropertyResult>;
|
|
41
|
+
/**
|
|
42
|
+
* Returns object properties as an array.
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* This is a convenience function that extracts properties using `getProperties` and returns them as an array.
|
|
46
|
+
*
|
|
47
|
+
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
48
|
+
* @returns An array of JSON Schema fragments representing the properties, each including metadata.
|
|
49
|
+
*/
|
|
50
|
+
declare function getPropertiesList(obj: Schema | JsonSchemaObject): GetPropertyResult[];
|
|
51
|
+
/**
|
|
52
|
+
* Extracts object properties from a JSON Schema object form.
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* This function returns an empty object if the schema is not an object form or if it has no properties.
|
|
56
|
+
*
|
|
57
|
+
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
58
|
+
* @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.
|
|
59
|
+
*/
|
|
60
|
+
declare function getProperty<TSchema extends JsonSchemaObject>(obj: Schema<TSchema> | TSchema, name: keyof TSchema["properties"] & string): GetPropertyResult;
|
|
61
|
+
/**
|
|
62
|
+
* Adds a property to a JSON Schema object form.
|
|
63
|
+
*
|
|
64
|
+
* @remarks
|
|
65
|
+
* This function modifies the provided schema in place by adding a new property with the specified name and schema. It also updates the `required` array based on the `optional` flag of the property. If the property is marked as optional, it will be removed from the `required` array; otherwise, it will be added to it.
|
|
66
|
+
*
|
|
67
|
+
* @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.
|
|
68
|
+
* @param name - The name of the property to add.
|
|
69
|
+
* @param property - The JSON Schema fragment representing the property's schema, including metadata.
|
|
70
|
+
* @throws Will throw an error if the provided schema is not an object form.
|
|
71
|
+
*/
|
|
72
|
+
declare function addProperty(obj: Schema | JsonSchemaObject, name: string, property: JsonSchema): void;
|
|
73
|
+
/**
|
|
74
|
+
* Merges multiple JSON Schemas into one.
|
|
75
|
+
*
|
|
76
|
+
* @remarks
|
|
77
|
+
* This function takes multiple JSON Schemas or Schema wrappers and merges them
|
|
78
|
+
* into a single JSON Schema object. Later schemas in the argument list take
|
|
79
|
+
* precedence over earlier ones for scalar conflicts.
|
|
80
|
+
*
|
|
81
|
+
* Structured keywords are merged recursively:
|
|
82
|
+
* - Named child schemas (`properties`, `$defs`, etc.) are merged
|
|
83
|
+
* per-property via recursive calls to `merge`.
|
|
84
|
+
* - Composition arrays (`allOf`, `anyOf`, `oneOf`) are concatenated.
|
|
85
|
+
* - Single-schema keywords (`if`, `then`, `else`, `not`, `items`, etc.)
|
|
86
|
+
* are merged recursively when both sides define them.
|
|
87
|
+
* - `required` arrays are unioned and deduplicated.
|
|
88
|
+
*
|
|
89
|
+
* @param schemas - An array of JSON Schemas or Schema wrappers to merge.
|
|
90
|
+
* @returns A new JSON Schema that is the result of merging all input schemas.
|
|
91
|
+
*/
|
|
92
|
+
declare function merge(...schemas: (JsonSchema | Schema)[]): JsonSchema;
|
|
93
|
+
/**
|
|
94
|
+
* Returns whether a JSON Schema fragment accepts `null`.
|
|
95
|
+
*
|
|
96
|
+
* @remarks
|
|
97
|
+
* This is true if the schema has `nullable: true` or if its `type` includes `"null"`.
|
|
98
|
+
*
|
|
99
|
+
* @param schema - The JSON Schema fragment to check.
|
|
100
|
+
* @returns `true` if the schema accepts `null`, otherwise `false`.
|
|
101
|
+
*/
|
|
102
|
+
declare function isSchemaNullable(schema?: JsonSchema): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Returns whether an object property is optional (not listed in `required`).
|
|
105
|
+
*
|
|
106
|
+
* @remarks
|
|
107
|
+
* In JSON Schema, object properties are optional by default unless they are listed in the `required` array of the parent schema. This function checks whether a given property name is not included in the `required` array of its parent schema, indicating that it is optional.
|
|
108
|
+
*
|
|
109
|
+
* @param parent - The parent JSON Schema object containing the property.
|
|
110
|
+
* @param propertyName - The name of the property to check for optionality.
|
|
111
|
+
* @returns `true` if the property is optional, otherwise `false`.
|
|
112
|
+
*/
|
|
113
|
+
declare function isPropertyOptional(parent: JsonSchemaObject, propertyName: string): boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Checks if a given file name has a valid schema input file extension.
|
|
116
|
+
*
|
|
117
|
+
* @param fileName - The file name to check for a valid schema input extension.
|
|
118
|
+
* @returns `true` if the file name has a valid schema input extension, otherwise `false`.
|
|
119
|
+
*/
|
|
120
|
+
declare function isValidSchemaInputFile(fileName: string): boolean;
|
|
121
|
+
//#endregion
|
|
122
|
+
export { GetPropertyResult, addProperty, getJsonSchema, getJsonSchemaObject, getProperties, getPropertiesList, getProperty, isPropertyOptional, isSchemaNullable, isValidSchemaInputFile, merge };
|
|
123
|
+
//# sourceMappingURL=helpers.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.cts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;KAoCY,iBAAA,GAAoB,UAAU;EACxC,IAAA;EACA,QAAA;EACA,OAAA;AAAA;;;;;;;AAAO;AAaT;;;iBAAgB,aAAA,CAAc,KAAA,EAAO,MAAA,GAAS,UAAA,GAAa,UAAA;;;;;;;;;;AAAU;iBAmBrD,mBAAA,CACd,KAAA,EAAO,MAAA,GAAS,UAAA,GACf,gBAAA;;;;;;;;;;iBAkBa,aAAA,CACd,GAAA,EAAK,MAAA,GAAS,gBAAA,GACb,MAAA,SAAe,iBAAA;;;AApBC;AAkBnB;;;;;;iBA+BgB,iBAAA,CAAkB,GAAA,EAAK,MAAA,GAAS,gBAAA,GAAgB,iBAAA;;;;;;;;;AA7B7B;iBA0CnB,WAAA,iBAA4B,gBAAA,EAC1C,GAAA,EAAK,MAAA,CAAO,OAAA,IAAW,OAAA,EACvB,IAAA,QAAY,OAAA,0BACX,iBAAA;;;;;;;;;;;;iBAgCa,WAAA,CACd,GAAA,EAAK,MAAA,GAAS,gBAAA,EACd,IAAA,UACA,QAAA,EAAU,UAAA;AAnDoD;AAahE;;;;;;;;;;;;;;;;;;AAbgE,iBA4JhD,KAAA,IAAS,OAAA,GAAU,UAAA,GAAa,MAAA,MAAY,UAAA;;;AA5IxC;AAgCpB;;;;;;iBAwIgB,gBAAA,CAAiB,MAAmB,GAAV,UAAU;;;;;;;;;AArI9B;AAyGtB;iBAkDgB,kBAAA,CACd,MAAA,EAAQ,gBAAgB,EACxB,YAAA;;;;;;;iBAiBc,sBAAA,CAAuB,QAAgB"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { JsonSchema, JsonSchemaObject, Schema } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/helpers.d.ts
|
|
4
|
+
type GetPropertyResult = JsonSchema & {
|
|
5
|
+
name: string;
|
|
6
|
+
required: boolean;
|
|
7
|
+
default?: unknown;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Retrieves the JSON Schema from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
11
|
+
*
|
|
12
|
+
* @remarks
|
|
13
|
+
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
14
|
+
*
|
|
15
|
+
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
16
|
+
* @returns The JSON Schema object.
|
|
17
|
+
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
18
|
+
*/
|
|
19
|
+
declare function getJsonSchema(input: Schema | JsonSchema): JsonSchema;
|
|
20
|
+
/**
|
|
21
|
+
* Retrieves the JSON Schema in Object form from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema object, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
25
|
+
*
|
|
26
|
+
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
27
|
+
* @returns The JSON Schema object.
|
|
28
|
+
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
29
|
+
*/
|
|
30
|
+
declare function getJsonSchemaObject(input: Schema | JsonSchema): JsonSchemaObject;
|
|
31
|
+
/**
|
|
32
|
+
* Extracts object properties from a JSON Schema object form.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* This function returns an empty object if the schema is not an object form or if it has no properties.
|
|
36
|
+
*
|
|
37
|
+
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
38
|
+
* @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.
|
|
39
|
+
*/
|
|
40
|
+
declare function getProperties(obj: Schema | JsonSchemaObject): Record<string, GetPropertyResult>;
|
|
41
|
+
/**
|
|
42
|
+
* Returns object properties as an array.
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* This is a convenience function that extracts properties using `getProperties` and returns them as an array.
|
|
46
|
+
*
|
|
47
|
+
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
48
|
+
* @returns An array of JSON Schema fragments representing the properties, each including metadata.
|
|
49
|
+
*/
|
|
50
|
+
declare function getPropertiesList(obj: Schema | JsonSchemaObject): GetPropertyResult[];
|
|
51
|
+
/**
|
|
52
|
+
* Extracts object properties from a JSON Schema object form.
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* This function returns an empty object if the schema is not an object form or if it has no properties.
|
|
56
|
+
*
|
|
57
|
+
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
58
|
+
* @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.
|
|
59
|
+
*/
|
|
60
|
+
declare function getProperty<TSchema extends JsonSchemaObject>(obj: Schema<TSchema> | TSchema, name: keyof TSchema["properties"] & string): GetPropertyResult;
|
|
61
|
+
/**
|
|
62
|
+
* Adds a property to a JSON Schema object form.
|
|
63
|
+
*
|
|
64
|
+
* @remarks
|
|
65
|
+
* This function modifies the provided schema in place by adding a new property with the specified name and schema. It also updates the `required` array based on the `optional` flag of the property. If the property is marked as optional, it will be removed from the `required` array; otherwise, it will be added to it.
|
|
66
|
+
*
|
|
67
|
+
* @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.
|
|
68
|
+
* @param name - The name of the property to add.
|
|
69
|
+
* @param property - The JSON Schema fragment representing the property's schema, including metadata.
|
|
70
|
+
* @throws Will throw an error if the provided schema is not an object form.
|
|
71
|
+
*/
|
|
72
|
+
declare function addProperty(obj: Schema | JsonSchemaObject, name: string, property: JsonSchema): void;
|
|
73
|
+
/**
|
|
74
|
+
* Merges multiple JSON Schemas into one.
|
|
75
|
+
*
|
|
76
|
+
* @remarks
|
|
77
|
+
* This function takes multiple JSON Schemas or Schema wrappers and merges them
|
|
78
|
+
* into a single JSON Schema object. Later schemas in the argument list take
|
|
79
|
+
* precedence over earlier ones for scalar conflicts.
|
|
80
|
+
*
|
|
81
|
+
* Structured keywords are merged recursively:
|
|
82
|
+
* - Named child schemas (`properties`, `$defs`, etc.) are merged
|
|
83
|
+
* per-property via recursive calls to `merge`.
|
|
84
|
+
* - Composition arrays (`allOf`, `anyOf`, `oneOf`) are concatenated.
|
|
85
|
+
* - Single-schema keywords (`if`, `then`, `else`, `not`, `items`, etc.)
|
|
86
|
+
* are merged recursively when both sides define them.
|
|
87
|
+
* - `required` arrays are unioned and deduplicated.
|
|
88
|
+
*
|
|
89
|
+
* @param schemas - An array of JSON Schemas or Schema wrappers to merge.
|
|
90
|
+
* @returns A new JSON Schema that is the result of merging all input schemas.
|
|
91
|
+
*/
|
|
92
|
+
declare function merge(...schemas: (JsonSchema | Schema)[]): JsonSchema;
|
|
93
|
+
/**
|
|
94
|
+
* Returns whether a JSON Schema fragment accepts `null`.
|
|
95
|
+
*
|
|
96
|
+
* @remarks
|
|
97
|
+
* This is true if the schema has `nullable: true` or if its `type` includes `"null"`.
|
|
98
|
+
*
|
|
99
|
+
* @param schema - The JSON Schema fragment to check.
|
|
100
|
+
* @returns `true` if the schema accepts `null`, otherwise `false`.
|
|
101
|
+
*/
|
|
102
|
+
declare function isSchemaNullable(schema?: JsonSchema): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Returns whether an object property is optional (not listed in `required`).
|
|
105
|
+
*
|
|
106
|
+
* @remarks
|
|
107
|
+
* In JSON Schema, object properties are optional by default unless they are listed in the `required` array of the parent schema. This function checks whether a given property name is not included in the `required` array of its parent schema, indicating that it is optional.
|
|
108
|
+
*
|
|
109
|
+
* @param parent - The parent JSON Schema object containing the property.
|
|
110
|
+
* @param propertyName - The name of the property to check for optionality.
|
|
111
|
+
* @returns `true` if the property is optional, otherwise `false`.
|
|
112
|
+
*/
|
|
113
|
+
declare function isPropertyOptional(parent: JsonSchemaObject, propertyName: string): boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Checks if a given file name has a valid schema input file extension.
|
|
116
|
+
*
|
|
117
|
+
* @param fileName - The file name to check for a valid schema input extension.
|
|
118
|
+
* @returns `true` if the file name has a valid schema input extension, otherwise `false`.
|
|
119
|
+
*/
|
|
120
|
+
declare function isValidSchemaInputFile(fileName: string): boolean;
|
|
121
|
+
//#endregion
|
|
122
|
+
export { GetPropertyResult, addProperty, getJsonSchema, getJsonSchemaObject, getProperties, getPropertiesList, getProperty, isPropertyOptional, isSchemaNullable, isValidSchemaInputFile, merge };
|
|
123
|
+
//# sourceMappingURL=helpers.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.mts","names":[],"sources":["../src/helpers.ts"],"mappings":""}
|
package/dist/helpers.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{SCHEMA_ARRAY_CONCAT_KEYWORDS as e,SCHEMA_RECORD_KEYWORDS as t,SCHEMA_SINGLE_KEYWORDS as n,VALID_SOURCE_FILE_EXTENSIONS as r}from"./constants.mjs";import{isJsonSchema as i,isJsonSchemaObject as a,isSchema as o}from"./type-checks.mjs";import{readSchemaTypes as s}from"./metadata.mjs";import{isSetObject as c}from"@stryke/type-checks";import{findFileExtensionSafe as l}from"@stryke/path/find";import{getUnique as u}from"@stryke/helpers/get-unique";function d(e){let t=o(e)?e.schema:e;if(!i(t))throw TypeError(`The provided input is not a valid JSON Schema`);return t}function f(e){let t=d(e);if(!a(t))throw TypeError(`The provided input is not a valid JSON Schema object`);return t}function p(e){let t=f(e);return c(t.properties)?Object.entries(t.properties).reduce((e,[n,r])=>(r.name=n,r.required=!b(t,n),r.default=t.default?.[n]??r.default,e[n]=r,e),{}):{}}function m(e){return Object.values(p(e))}function h(e,t){let n=f(e);if(!c(n.properties))throw TypeError(`The provided schema does not have any properties`);let r=n.properties[t];if(!r)throw TypeError(`The provided schema does not have a property named "${t}"`);return{...r,name:t,required:!b(n,t),default:n.default?.[t]??r?.default}}function g(e,t,n){let r=f(e);r.properties??={},r.required??=[],n.name=t,r.properties[t]=n,r.required.includes(t)||r.required.push(t),r.required.length===0&&delete r.required}function _(r,a){let o={...r};for(let[r,s]of Object.entries(a)){let a=o[r];if(r===`required`)o[r]=u([...Array.isArray(a)?a:[],...Array.isArray(s)?s:[]]);else if(t.has(r)&&c(a)&&c(s)){let e={...a};for(let[t,n]of Object.entries(s)){let r=e[t];e[t]=i(r)&&i(n)?_(r,n):n}o[r]=e}else e.has(r)&&Array.isArray(a)&&Array.isArray(s)?o[r]=[...a,...s]:n.has(r)&&i(a)&&i(s)?o[r]=_(a,s):o[r]=s}return o}function v(...e){let t=e.map(e=>d(e));return t.length===0?{}:t.reduce((e,t)=>{let n=e.type,r=t.type;return n&&r&&n!==r?t:_(e,t)})}function y(e){return c(e)?e.nullable===!0?!0:s(e).includes(`null`):!1}function b(e,t){if(!e.properties?.[t])throw Error(`The property "${t}" does not exist in the parent schema.`);return!(e.required??[]).includes(t)}function x(e){return r.includes(l(e))}export{g as addProperty,d as getJsonSchema,f as getJsonSchemaObject,p as getProperties,m as getPropertiesList,h as getProperty,b as isPropertyOptional,y as isSchemaNullable,x as isValidSchemaInputFile,v as merge};
|
|
2
|
+
//# sourceMappingURL=helpers.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.mjs","names":[],"sources":[],"mappings":""}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require("./types.cjs");const e=require("./constants.cjs"),t=require("./type-checks.cjs"),n=require("./bundle.cjs"),r=require("./resolve.cjs"),i=require("./persistence-CLni2zCP.cjs"),a=require("./metadata.cjs"),o=require("./helpers.cjs"),s=require("./codegen.cjs");exports.JSON_SCHEMA_METADATA_KEYS=e.JSON_SCHEMA_METADATA_KEYS,exports.JSON_SCHEMA_PRIMITIVE_TYPES=e.JSON_SCHEMA_PRIMITIVE_TYPES,exports.JSON_SCHEMA_TYPES=e.JSON_SCHEMA_TYPES,exports.JsonSchemaTypeNames=e.JsonSchemaTypeNames,exports.SCHEMA_ARRAY_CONCAT_KEYWORDS=e.SCHEMA_ARRAY_CONCAT_KEYWORDS,exports.SCHEMA_RECORD_KEYWORDS=e.SCHEMA_RECORD_KEYWORDS,exports.SCHEMA_SINGLE_KEYWORDS=e.SCHEMA_SINGLE_KEYWORDS,exports.VALID_SOURCE_FILE_EXTENSIONS=e.VALID_SOURCE_FILE_EXTENSIONS,exports.addProperty=o.addProperty,exports.applyJsonSchemaMetadata=a.applyJsonSchemaMetadata,exports.bundle=n.bundle,exports.bundleReferences=i.o,exports.extract=i.s,exports.extractHash=i.c,exports.extractJsonSchema=i.l,exports.extractResolvedVariant=i.u,exports.extractSchema=i.d,exports.extractSchemaWithSource=i.f,exports.extractSource=i.p,exports.extractVariant=i.m,exports.generateParserCode=s.generateParserCode,exports.getCacheDirectory=i.t,exports.getCacheFilePath=i.n,exports.getJsonSchema=o.getJsonSchema,exports.getJsonSchemaObject=o.getJsonSchemaObject,exports.getJsonSchemaType=s.getJsonSchemaType,exports.getPrimarySchemaType=a.getPrimarySchemaType,exports.getProperties=o.getProperties,exports.getPropertiesList=o.getPropertiesList,exports.getProperty=o.getProperty,exports.isFileReference=t.isFileReference,exports.isJsonSchema=t.isJsonSchema,exports.isJsonSchemaAllOf=t.isJsonSchemaAllOf,exports.isJsonSchemaAny=t.isJsonSchemaAny,exports.isJsonSchemaAnyOf=t.isJsonSchemaAnyOf,exports.isJsonSchemaArray=t.isJsonSchemaArray,exports.isJsonSchemaBigint=t.isJsonSchemaBigint,exports.isJsonSchemaBoolean=t.isJsonSchemaBoolean,exports.isJsonSchemaDate=t.isJsonSchemaDate,exports.isJsonSchemaDecimal=t.isJsonSchemaDecimal,exports.isJsonSchemaEnum=t.isJsonSchemaEnum,exports.isJsonSchemaInteger=t.isJsonSchemaInteger,exports.isJsonSchemaKeywords=t.isJsonSchemaKeywords,exports.isJsonSchemaLiteral=t.isJsonSchemaLiteral,exports.isJsonSchemaMap=t.isJsonSchemaMap,exports.isJsonSchemaNativeEnum=t.isJsonSchemaNativeEnum,exports.isJsonSchemaNever=t.isJsonSchemaNever,exports.isJsonSchemaNull=t.isJsonSchemaNull,exports.isJsonSchemaNullable=t.isJsonSchemaNullable,exports.isJsonSchemaNumber=t.isJsonSchemaNumber,exports.isJsonSchemaObject=t.isJsonSchemaObject,exports.isJsonSchemaPrimitiveType=t.isJsonSchemaPrimitiveType,exports.isJsonSchemaPrimitiveUnion=t.isJsonSchemaPrimitiveUnion,exports.isJsonSchemaRecord=t.isJsonSchemaRecord,exports.isJsonSchemaRef=t.isJsonSchemaRef,exports.isJsonSchemaSet=t.isJsonSchemaSet,exports.isJsonSchemaString=t.isJsonSchemaString,exports.isJsonSchemaTuple=t.isJsonSchemaTuple,exports.isJsonSchemaType=t.isJsonSchemaType,exports.isJsonSchemaUndefined=t.isJsonSchemaUndefined,exports.isJsonSchemaUnion=t.isJsonSchemaUnion,exports.isJsonSchemaUnknown=t.isJsonSchemaUnknown,exports.isNullOnlyJsonSchema=t.isNullOnlyJsonSchema,exports.isPropertyOptional=o.isPropertyOptional,exports.isSchema=t.isSchema,exports.isSchemaNullable=o.isSchemaNullable,exports.isSchemaObject=t.isSchemaObject,exports.isSchemaWithSource=t.isSchemaWithSource,exports.isStandardSchema=t.isStandardSchema,exports.isUntypedInput=t.isUntypedInput,exports.isUntypedInputStrict=t.isUntypedInputStrict,exports.isUntypedSchema=t.isUntypedSchema,exports.isUntypedSchemaStrict=t.isUntypedSchemaStrict,exports.isValibotSchema=t.isValibotSchema,exports.isValidSchemaInputFile=o.isValidSchemaInputFile,exports.merge=o.merge,exports.readSchema=i.r,exports.readSchemaSafe=i.i,exports.readSchemaTypes=a.readSchemaTypes,exports.resolve=r.resolve,exports.resolveModule=r.resolveModule,exports.resolveSafe=r.resolveSafe,exports.resolveTSType=r.resolveTSType,exports.stringifyType=s.stringifyType,exports.stringifyValue=s.stringifyValue,exports.writeSchema=i.a;
|