@powerlines/schema 0.11.40 → 0.11.41
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/codegen.cjs +12 -18
- package/dist/codegen.d.cts +7 -2
- package/dist/codegen.d.cts.map +1 -1
- package/dist/codegen.d.mts +7 -2
- package/dist/codegen.d.mts.map +1 -1
- package/dist/codegen.mjs +13 -18
- package/dist/codegen.mjs.map +1 -1
- package/dist/extract.cjs +3 -3
- package/dist/extract.d.cts.map +1 -1
- package/dist/extract.d.mts.map +1 -1
- package/dist/extract.mjs +4 -4
- package/dist/extract.mjs.map +1 -1
- package/dist/helpers.d.cts.map +1 -1
- package/dist/helpers.d.mts.map +1 -1
- package/dist/helpers.mjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/metadata.d.cts.map +1 -1
- package/dist/metadata.d.mts.map +1 -1
- package/dist/metadata.mjs.map +1 -1
- package/dist/type-checks.mjs.map +1 -1
- package/dist/types.d.cts +10 -10
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +11 -11
- package/dist/types.d.mts.map +1 -1
- package/dist/validate.cjs +38 -0
- package/dist/validate.d.cts +21 -0
- package/dist/validate.d.cts.map +1 -0
- package/dist/validate.d.mts +21 -0
- package/dist/validate.d.mts.map +1 -0
- package/dist/validate.mjs +34 -0
- package/dist/validate.mjs.map +1 -0
- package/package.json +6 -2
package/dist/codegen.cjs
CHANGED
|
@@ -3,16 +3,13 @@ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
|
3
3
|
const require_metadata = require('./metadata.cjs');
|
|
4
4
|
const require_type_checks = require('./type-checks.cjs');
|
|
5
5
|
const require_helpers = require('./helpers.cjs');
|
|
6
|
+
const require_validate = require('./validate.cjs');
|
|
6
7
|
let _stryke_convert_to_bool = require("@stryke/convert/to-bool");
|
|
7
8
|
let _stryke_type_checks_is_boolean = require("@stryke/type-checks/is-boolean");
|
|
8
9
|
let _stryke_type_checks_is_null = require("@stryke/type-checks/is-null");
|
|
9
10
|
let _stryke_type_checks_is_number = require("@stryke/type-checks/is-number");
|
|
10
11
|
let _stryke_type_checks_is_set_string = require("@stryke/type-checks/is-set-string");
|
|
11
12
|
let _stryke_type_checks_is_undefined = require("@stryke/type-checks/is-undefined");
|
|
12
|
-
let ajv = require("ajv");
|
|
13
|
-
ajv = require_runtime.__toESM(ajv, 1);
|
|
14
|
-
let ajv_formats_dist_formats_js = require("ajv-formats/dist/formats.js");
|
|
15
|
-
let ajv_dist_compile_codegen_index_js = require("ajv/dist/compile/codegen/index.js");
|
|
16
13
|
let ajv_dist_standalone_index_js = require("ajv/dist/standalone/index.js");
|
|
17
14
|
ajv_dist_standalone_index_js = require_runtime.__toESM(ajv_dist_standalone_index_js, 1);
|
|
18
15
|
|
|
@@ -34,14 +31,14 @@ function stringifyType(schema) {
|
|
|
34
31
|
if (primaryType === "integer" || primaryType === "number") return "number";
|
|
35
32
|
return primaryType;
|
|
36
33
|
}
|
|
37
|
-
if (Array.isArray(schema.enum)) return schema.enum.map((value) => JSON.stringify(value)).join(" | ");
|
|
34
|
+
if (schema.type === "array" && Array.isArray(schema.enum)) return schema.enum.map((value) => JSON.stringify(value)).join(" | ");
|
|
38
35
|
if (schema.const !== void 0) return JSON.stringify(schema.const);
|
|
39
36
|
if (schema.type === "array" || schema.items) return `${stringifyType(Array.isArray(schema.items) ? schema.items[0] : schema.items)}[]`;
|
|
40
37
|
if (schema.type === "object" || schema.properties || schema.additionalProperties) {
|
|
41
|
-
if (
|
|
38
|
+
if (require_type_checks.isJsonSchema(schema.additionalProperties)) return `{ [key: string]: ${stringifyType(schema.additionalProperties)} }`;
|
|
42
39
|
if (require_type_checks.isJsonSchemaObject(schema)) return `{ ${require_helpers.getPropertiesList(schema).map((property) => {
|
|
43
|
-
const suffix = property.
|
|
44
|
-
return `${property.
|
|
40
|
+
const suffix = schema.required?.includes(property.name) || property.nullable ? `${property.optional ? "?" : ""}${property.nullable ? " | null" : ""}` : "";
|
|
41
|
+
return `${property.name}${suffix}: ${stringifyType(property)}`;
|
|
45
42
|
}).join(";\n")} }`;
|
|
46
43
|
}
|
|
47
44
|
if (schema.oneOf || schema.anyOf) return (schema.oneOf ?? schema.anyOf ?? []).map((branch) => stringifyType(branch)).join(" | ");
|
|
@@ -52,17 +49,14 @@ function stringifyType(schema) {
|
|
|
52
49
|
* Generates standalone JSON Schema validation code using Ajv.
|
|
53
50
|
*/
|
|
54
51
|
async function generateCode(schemas, refsOrFuncts) {
|
|
55
|
-
|
|
56
|
-
schemas,
|
|
57
|
-
code: {
|
|
58
|
-
source: true,
|
|
59
|
-
esm: true
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
ajv$1.opts.code.formats ??= ajv_dist_compile_codegen_index_js._`await import("ajv-formats/dist/formats").${new ajv_dist_compile_codegen_index_js.Name("fullFormats")}`;
|
|
63
|
-
for (const formatName of ajv_formats_dist_formats_js.formatNames) ajv$1.addFormat(formatName, ajv_formats_dist_formats_js.fullFormats[formatName]);
|
|
64
|
-
return (0, ajv_dist_standalone_index_js.default)(ajv$1, refsOrFuncts);
|
|
52
|
+
return (0, ajv_dist_standalone_index_js.default)(require_validate.getValidator(schemas), refsOrFuncts);
|
|
65
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* A helper function to determine if a JSON Schema fragment is nullable, for use in code generation.
|
|
56
|
+
*
|
|
57
|
+
* @param schema - The JSON Schema fragment to check.
|
|
58
|
+
* @returns `true` if the schema is nullable, otherwise `false`. A schema is considered nullable if it has `nullable: true` or if its `type` includes `"null"`.
|
|
59
|
+
*/
|
|
66
60
|
function isNullableSchema(schema) {
|
|
67
61
|
return require_metadata.isSchemaNullable(schema);
|
|
68
62
|
}
|
package/dist/codegen.d.cts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { JsonSchema, JsonSchemaPrimitiveType } from "./types.cjs";
|
|
2
|
-
import { Options } from "ajv";
|
|
3
2
|
import standaloneCode from "ajv/dist/standalone/index.js";
|
|
4
3
|
|
|
5
4
|
//#region src/codegen.d.ts
|
|
@@ -14,7 +13,13 @@ declare function stringifyType<T = unknown>(schema?: JsonSchema<T>): string;
|
|
|
14
13
|
/**
|
|
15
14
|
* Generates standalone JSON Schema validation code using Ajv.
|
|
16
15
|
*/
|
|
17
|
-
declare function generateCode(schemas:
|
|
16
|
+
declare function generateCode<T = unknown>(schemas: JsonSchema<T>, refsOrFuncts?: Parameters<typeof standaloneCode>[1]): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* A helper function to determine if a JSON Schema fragment is nullable, for use in code generation.
|
|
19
|
+
*
|
|
20
|
+
* @param schema - The JSON Schema fragment to check.
|
|
21
|
+
* @returns `true` if the schema is nullable, otherwise `false`. A schema is considered nullable if it has `nullable: true` or if its `type` includes `"null"`.
|
|
22
|
+
*/
|
|
18
23
|
declare function isNullableSchema(schema?: JsonSchema): boolean;
|
|
19
24
|
//#endregion
|
|
20
25
|
export { generateCode, isNullableSchema, stringifyType, stringifyValue };
|
package/dist/codegen.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codegen.d.cts","names":[],"sources":["../src/codegen.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"codegen.d.cts","names":[],"sources":["../src/codegen.ts"],"mappings":";;;;;;AAkCA;iBAAgB,cAAA,CACd,KAAA,YACA,IAAA,GAAO,uBAAuB;;;;iBAoBhB,aAAA,aAAA,CAA2B,MAAA,GAAS,UAAU,CAAC,CAAA;;;AApBtB;iBA6FnB,YAAA,aAAA,CACpB,OAAA,EAAS,UAAA,CAAW,CAAA,GACpB,YAAA,GAAe,UAAA,QAAkB,cAAA,OAAkB,OAAA;;;;;;;iBAarC,gBAAA,CAAiB,MAAmB,GAAV,UAAU"}
|
package/dist/codegen.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { JsonSchema, JsonSchemaPrimitiveType } from "./types.mjs";
|
|
2
|
-
import { Options } from "ajv";
|
|
3
2
|
import standaloneCode from "ajv/dist/standalone/index.js";
|
|
4
3
|
|
|
5
4
|
//#region src/codegen.d.ts
|
|
@@ -14,7 +13,13 @@ declare function stringifyType<T = unknown>(schema?: JsonSchema<T>): string;
|
|
|
14
13
|
/**
|
|
15
14
|
* Generates standalone JSON Schema validation code using Ajv.
|
|
16
15
|
*/
|
|
17
|
-
declare function generateCode(schemas:
|
|
16
|
+
declare function generateCode<T = unknown>(schemas: JsonSchema<T>, refsOrFuncts?: Parameters<typeof standaloneCode>[1]): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* A helper function to determine if a JSON Schema fragment is nullable, for use in code generation.
|
|
19
|
+
*
|
|
20
|
+
* @param schema - The JSON Schema fragment to check.
|
|
21
|
+
* @returns `true` if the schema is nullable, otherwise `false`. A schema is considered nullable if it has `nullable: true` or if its `type` includes `"null"`.
|
|
22
|
+
*/
|
|
18
23
|
declare function isNullableSchema(schema?: JsonSchema): boolean;
|
|
19
24
|
//#endregion
|
|
20
25
|
export { generateCode, isNullableSchema, stringifyType, stringifyValue };
|
package/dist/codegen.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codegen.d.mts","names":[],"sources":["../src/codegen.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"codegen.d.mts","names":[],"sources":["../src/codegen.ts"],"mappings":";;;;;;AAkCA;iBAAgB,cAAA,CACd,KAAA,YACA,IAAA,GAAO,uBAAuB;;;;iBAoBhB,aAAA,aAAA,CAA2B,MAAA,GAAS,UAAU,CAAC,CAAA;;;AApBtB;iBA6FnB,YAAA,aAAA,CACpB,OAAA,EAAS,UAAA,CAAW,CAAA,GACpB,YAAA,GAAe,UAAA,QAAkB,cAAA,OAAkB,OAAA;;;;;;;iBAarC,gBAAA,CAAiB,MAAmB,GAAV,UAAU"}
|
package/dist/codegen.mjs
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import { getPrimarySchemaType, isSchemaNullable } from "./metadata.mjs";
|
|
2
|
-
import { isJsonSchemaObject } from "./type-checks.mjs";
|
|
2
|
+
import { isJsonSchema, isJsonSchemaObject } from "./type-checks.mjs";
|
|
3
3
|
import { getPropertiesList } from "./helpers.mjs";
|
|
4
|
+
import { getValidator } from "./validate.mjs";
|
|
4
5
|
import { toBool } from "@stryke/convert/to-bool";
|
|
5
6
|
import { isBoolean } from "@stryke/type-checks/is-boolean";
|
|
6
7
|
import { isNull } from "@stryke/type-checks/is-null";
|
|
7
8
|
import { isNumber } from "@stryke/type-checks/is-number";
|
|
8
9
|
import { isSetString } from "@stryke/type-checks/is-set-string";
|
|
9
10
|
import { isUndefined } from "@stryke/type-checks/is-undefined";
|
|
10
|
-
import Ajv from "ajv";
|
|
11
|
-
import { formatNames, fullFormats } from "ajv-formats/dist/formats.js";
|
|
12
|
-
import { Name, _ } from "ajv/dist/compile/codegen/index.js";
|
|
13
11
|
import standaloneCode from "ajv/dist/standalone/index.js";
|
|
14
12
|
|
|
15
13
|
//#region src/codegen.ts
|
|
@@ -30,14 +28,14 @@ function stringifyType(schema) {
|
|
|
30
28
|
if (primaryType === "integer" || primaryType === "number") return "number";
|
|
31
29
|
return primaryType;
|
|
32
30
|
}
|
|
33
|
-
if (Array.isArray(schema.enum)) return schema.enum.map((value) => JSON.stringify(value)).join(" | ");
|
|
31
|
+
if (schema.type === "array" && Array.isArray(schema.enum)) return schema.enum.map((value) => JSON.stringify(value)).join(" | ");
|
|
34
32
|
if (schema.const !== void 0) return JSON.stringify(schema.const);
|
|
35
33
|
if (schema.type === "array" || schema.items) return `${stringifyType(Array.isArray(schema.items) ? schema.items[0] : schema.items)}[]`;
|
|
36
34
|
if (schema.type === "object" || schema.properties || schema.additionalProperties) {
|
|
37
|
-
if (schema.additionalProperties
|
|
35
|
+
if (isJsonSchema(schema.additionalProperties)) return `{ [key: string]: ${stringifyType(schema.additionalProperties)} }`;
|
|
38
36
|
if (isJsonSchemaObject(schema)) return `{ ${getPropertiesList(schema).map((property) => {
|
|
39
|
-
const suffix = property.
|
|
40
|
-
return `${property.
|
|
37
|
+
const suffix = schema.required?.includes(property.name) || property.nullable ? `${property.optional ? "?" : ""}${property.nullable ? " | null" : ""}` : "";
|
|
38
|
+
return `${property.name}${suffix}: ${stringifyType(property)}`;
|
|
41
39
|
}).join(";\n")} }`;
|
|
42
40
|
}
|
|
43
41
|
if (schema.oneOf || schema.anyOf) return (schema.oneOf ?? schema.anyOf ?? []).map((branch) => stringifyType(branch)).join(" | ");
|
|
@@ -48,17 +46,14 @@ function stringifyType(schema) {
|
|
|
48
46
|
* Generates standalone JSON Schema validation code using Ajv.
|
|
49
47
|
*/
|
|
50
48
|
async function generateCode(schemas, refsOrFuncts) {
|
|
51
|
-
|
|
52
|
-
schemas,
|
|
53
|
-
code: {
|
|
54
|
-
source: true,
|
|
55
|
-
esm: true
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
ajv.opts.code.formats ??= _`await import("ajv-formats/dist/formats").${new Name("fullFormats")}`;
|
|
59
|
-
for (const formatName of formatNames) ajv.addFormat(formatName, fullFormats[formatName]);
|
|
60
|
-
return standaloneCode(ajv, refsOrFuncts);
|
|
49
|
+
return standaloneCode(getValidator(schemas), refsOrFuncts);
|
|
61
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* A helper function to determine if a JSON Schema fragment is nullable, for use in code generation.
|
|
53
|
+
*
|
|
54
|
+
* @param schema - The JSON Schema fragment to check.
|
|
55
|
+
* @returns `true` if the schema is nullable, otherwise `false`. A schema is considered nullable if it has `nullable: true` or if its `type` includes `"null"`.
|
|
56
|
+
*/
|
|
62
57
|
function isNullableSchema(schema) {
|
|
63
58
|
return isSchemaNullable(schema);
|
|
64
59
|
}
|
package/dist/codegen.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codegen.mjs","names":[],"sources":["../src/codegen.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { toBool } from \"@stryke/convert/to-bool\";\nimport { isBoolean } from \"@stryke/type-checks/is-boolean\";\nimport { isNull } from \"@stryke/type-checks/is-null\";\nimport { isNumber } from \"@stryke/type-checks/is-number\";\nimport { isSetString } from \"@stryke/type-checks/is-set-string\";\nimport { isUndefined } from \"@stryke/type-checks/is-undefined\";\nimport
|
|
1
|
+
{"version":3,"file":"codegen.mjs","names":[],"sources":["../src/codegen.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { toBool } from \"@stryke/convert/to-bool\";\nimport { isBoolean } from \"@stryke/type-checks/is-boolean\";\nimport { isNull } from \"@stryke/type-checks/is-null\";\nimport { isNumber } from \"@stryke/type-checks/is-number\";\nimport { isSetString } from \"@stryke/type-checks/is-set-string\";\nimport { isUndefined } from \"@stryke/type-checks/is-undefined\";\nimport standaloneCode from \"ajv/dist/standalone\";\nimport { getPropertiesList } from \"./helpers\";\nimport { getPrimarySchemaType, isSchemaNullable } from \"./metadata\";\nimport { isJsonSchema, isJsonSchemaObject } from \"./type-checks\";\nimport { JsonSchema, JsonSchemaPrimitiveType } from \"./types\";\nimport { getValidator } from \"./validate\";\n\n/**\n * Stringifies a value for generated TypeScript code.\n */\nexport function stringifyValue(\n value?: unknown,\n type?: JsonSchemaPrimitiveType | string\n): string {\n return isUndefined(value)\n ? \"undefined\"\n : isNull(value)\n ? \"null\"\n : type === \"boolean\" || isBoolean(value)\n ? String(toBool(value))\n : type === \"number\" || isNumber(value)\n ? Number.parseFloat(String(value)).toLocaleString(undefined, {\n maximumFractionDigits: 20\n })\n : type === \"integer\"\n ? Number.parseInt(String(value)).toLocaleString()\n : JSON.stringify(value);\n}\n\n/**\n * Stringifies a JSON Schema fragment into a TypeScript-like type string.\n */\nexport function stringifyType<T = unknown>(schema?: JsonSchema<T>): string {\n if (!schema) {\n return \"unknown\";\n }\n\n if (isSetString(schema.$ref)) {\n const match = /^#\\/(?:definitions|\\$defs)\\/(.+)$/.exec(schema.$ref);\n\n return match?.[1] ?? schema.$ref;\n }\n\n const primaryType = getPrimarySchemaType(schema);\n if (primaryType) {\n if (primaryType === \"integer\" || primaryType === \"number\") {\n return \"number\";\n }\n\n return primaryType;\n }\n\n if (schema.type === \"array\" && Array.isArray(schema.enum)) {\n return schema.enum.map(value => JSON.stringify(value)).join(\" | \");\n }\n\n if (schema.const !== undefined) {\n return JSON.stringify(schema.const);\n }\n\n if (schema.type === \"array\" || schema.items) {\n const items = Array.isArray(schema.items) ? schema.items[0] : schema.items;\n\n return `${stringifyType(items)}[]`;\n }\n\n if (\n schema.type === \"object\" ||\n schema.properties ||\n schema.additionalProperties\n ) {\n if (isJsonSchema(schema.additionalProperties)) {\n return `{ [key: string]: ${stringifyType(schema.additionalProperties)} }`;\n }\n\n if (isJsonSchemaObject(schema)) {\n return `{ ${getPropertiesList(schema as JsonSchema<Record<string, any>>)\n .map(property => {\n const suffix =\n schema.required?.includes(property.name) || property.nullable\n ? `${property.optional ? \"?\" : \"\"}${property.nullable ? \" | null\" : \"\"}`\n : \"\";\n\n return `${property.name}${suffix}: ${stringifyType(property)}`;\n })\n .join(\";\\n\")} }`;\n }\n }\n\n if (schema.oneOf || schema.anyOf) {\n return ((schema.oneOf ?? schema.anyOf ?? []) as JsonSchema<T>[])\n .map(branch => stringifyType(branch))\n .join(\" | \");\n }\n\n if (schema.allOf) {\n return \"object\";\n }\n\n return \"unknown\";\n}\n\n/**\n * Generates standalone JSON Schema validation code using Ajv.\n */\nexport async function generateCode<T = unknown>(\n schemas: JsonSchema<T>,\n refsOrFuncts?: Parameters<typeof standaloneCode>[1]\n) {\n const ajv = getValidator(schemas);\n\n return standaloneCode(ajv, refsOrFuncts);\n}\n\n/**\n * A helper function to determine if a JSON Schema fragment is nullable, for use in code generation.\n *\n * @param schema - The JSON Schema fragment to check.\n * @returns `true` if the schema is nullable, otherwise `false`. A schema is considered nullable if it has `nullable: true` or if its `type` includes `\"null\"`.\n */\nexport function isNullableSchema(schema?: JsonSchema): boolean {\n return isSchemaNullable(schema);\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAkCA,SAAgB,eACd,OACA,MACQ;CACR,OAAO,YAAY,KAAK,IACpB,cACA,OAAO,KAAK,IACV,SACA,SAAS,aAAa,UAAU,KAAK,IACnC,OAAO,OAAO,KAAK,CAAC,IACpB,SAAS,YAAY,SAAS,KAAK,IACjC,OAAO,WAAW,OAAO,KAAK,CAAC,EAAE,eAAe,QAAW,EACzD,uBAAuB,GACzB,CAAC,IACD,SAAS,YACP,OAAO,SAAS,OAAO,KAAK,CAAC,EAAE,eAAe,IAC9C,KAAK,UAAU,KAAK;AAClC;;;;AAKA,SAAgB,cAA2B,QAAgC;CACzE,IAAI,CAAC,QACH,OAAO;CAGT,IAAI,YAAY,OAAO,IAAI,GAGzB,OAFc,oCAAoC,KAAK,OAAO,IAEnD,IAAI,MAAM,OAAO;CAG9B,MAAM,cAAc,qBAAqB,MAAM;CAC/C,IAAI,aAAa;EACf,IAAI,gBAAgB,aAAa,gBAAgB,UAC/C,OAAO;EAGT,OAAO;CACT;CAEA,IAAI,OAAO,SAAS,WAAW,MAAM,QAAQ,OAAO,IAAI,GACtD,OAAO,OAAO,KAAK,KAAI,UAAS,KAAK,UAAU,KAAK,CAAC,EAAE,KAAK,KAAK;CAGnE,IAAI,OAAO,UAAU,QACnB,OAAO,KAAK,UAAU,OAAO,KAAK;CAGpC,IAAI,OAAO,SAAS,WAAW,OAAO,OAGpC,OAAO,GAAG,cAFI,MAAM,QAAQ,OAAO,KAAK,IAAI,OAAO,MAAM,KAAK,OAAO,KAExC,EAAE;CAGjC,IACE,OAAO,SAAS,YAChB,OAAO,cACP,OAAO,sBACP;EACA,IAAI,aAAa,OAAO,oBAAoB,GAC1C,OAAO,oBAAoB,cAAc,OAAO,oBAAoB,EAAE;EAGxE,IAAI,mBAAmB,MAAM,GAC3B,OAAO,KAAK,kBAAkB,MAAyC,EACpE,KAAI,aAAY;GACf,MAAM,SACJ,OAAO,UAAU,SAAS,SAAS,IAAI,KAAK,SAAS,WACjD,GAAG,SAAS,WAAW,MAAM,KAAK,SAAS,WAAW,YAAY,OAClE;GAEN,OAAO,GAAG,SAAS,OAAO,OAAO,IAAI,cAAc,QAAQ;EAC7D,CAAC,EACA,KAAK,KAAK,EAAE;CAEnB;CAEA,IAAI,OAAO,SAAS,OAAO,OACzB,QAAS,OAAO,SAAS,OAAO,SAAS,CAAC,GACvC,KAAI,WAAU,cAAc,MAAM,CAAC,EACnC,KAAK,KAAK;CAGf,IAAI,OAAO,OACT,OAAO;CAGT,OAAO;AACT;;;;AAKA,eAAsB,aACpB,SACA,cACA;CAGA,OAAO,eAFK,aAAa,OAED,GAAG,YAAY;AACzC;;;;;;;AAQA,SAAgB,iBAAiB,QAA8B;CAC7D,OAAO,iBAAiB,MAAM;AAChC"}
|
package/dist/extract.cjs
CHANGED
|
@@ -77,6 +77,7 @@ function convertUntypedInputToJsonSchema(input) {
|
|
|
77
77
|
}
|
|
78
78
|
if (require_type_checks.isUntypedSchema(value)) properties[key] = convertUntypedSchemaToJsonSchema(value);
|
|
79
79
|
}
|
|
80
|
+
if (!require_type_checks.isJsonSchemaObject(base)) throw new Error(`Failed to convert untyped input to JSON Schema. The base schema must be a valid JSON Schema object.`);
|
|
80
81
|
const mergedProperties = {
|
|
81
82
|
...(0, _stryke_type_checks_is_set_object.isSetObject)(base.properties) ? base.properties : {},
|
|
82
83
|
...properties
|
|
@@ -134,13 +135,12 @@ function extractReflection(reflection) {
|
|
|
134
135
|
* Extracts a JSON Schema from Zod, Standard Schema, untyped, or JSON Schema inputs.
|
|
135
136
|
*/
|
|
136
137
|
function extractJsonSchema(schema) {
|
|
137
|
-
if ((0, _stryke_type_checks_is_set_object.isSetObject)(schema) && ((0, _stryke_zod.isZod3Type)(schema) || (0, _stryke_json.isStandardJsonSchema)(schema) || require_type_checks.
|
|
138
|
+
if ((0, _stryke_type_checks_is_set_object.isSetObject)(schema) && ((0, _stryke_zod.isZod3Type)(schema) || (0, _stryke_json.isStandardJsonSchema)(schema) || require_type_checks.isUntypedInput(schema) || require_type_checks.isUntypedSchema(schema))) {
|
|
138
139
|
if ((0, _stryke_zod.isZod3Type)(schema)) return (0, _stryke_zod.extractJsonSchema)(schema);
|
|
139
140
|
if ((0, _stryke_json.isStandardJsonSchema)(schema)) return schema["~standard"].jsonSchema.input({ target: "draft-2020-12" });
|
|
140
141
|
if (require_type_checks.isUntypedInput(schema)) return convertUntypedInputToJsonSchema(schema);
|
|
141
142
|
if (require_type_checks.isUntypedSchema(schema)) return convertUntypedSchemaToJsonSchema(schema);
|
|
142
|
-
|
|
143
|
-
}
|
|
143
|
+
} else if (require_type_checks.isJsonSchema(schema)) return schema;
|
|
144
144
|
}
|
|
145
145
|
function extractResolvedVariant(input) {
|
|
146
146
|
if ((0, _stryke_type_checks_is_set_object.isSetObject)(input)) {
|
package/dist/extract.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extract.d.cts","names":[],"sources":["../src/extract.ts"],"mappings":";;;;;;;;
|
|
1
|
+
{"version":3,"file":"extract.d.cts","names":[],"sources":["../src/extract.ts"],"mappings":";;;;;;;;AA0NA;iBAAgB,WAAA,aAAA,CACd,OAAA,EAAS,kBAAA,EACT,KAAA,EAAO,WAAA,CAAY,CAAA;;;;iBAkCL,iBAAA,aAAA,CACd,UAAA,EAAY,IAAA,GACX,UAAA,CAAW,CAAA;;;;iBAWE,iBAAA,aAAA,CACd,MAAA,YACC,UAAU,CAAC,CAAA;AAAA,iBA6BE,sBAAA,CACd,KAAA,EAAO,iBAAA,GACN,mBAAmB;AAAA,iBAoBN,cAAA,aAAA,CACd,KAAA,EAAO,WAAA,CAAY,CAAA,IAClB,kBAAA;AAAA,iBAQmB,mBAAA,aAAA,CACpB,KAAA,EAAO,iBAAA,EACP,OAAA,GAAU,kBAAA,GACT,OAAA,CAAQ,UAAA,CAAW,CAAA;AAAA,iBA4BN,aAAA,CACd,OAAA,EAAS,mBAAA,EACT,KAAA,EAAO,iBAAA,GACN,YAAA;;AAhJoB;AAkCvB;;;;;;;;;;;;;;AAEe;AAWf;;;;;;;;;iBAiKsB,aAAA,aAAA,CACpB,OAAA,EAAS,OAAA,EACT,KAAA,EAAO,WAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,eAAA,CAAgB,CAAA;AAtI3B;;;;;;;;AAEsB;AAoBtB;;;;;;;;;;;;;;AAEqB;AAQrB;;;;;AAhCA,iBAiOsB,OAAA,aAAA,CACpB,OAAA,EAAS,OAAA,EACT,KAAA,EAAO,WAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,MAAA,CAAO,CAAA"}
|
package/dist/extract.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extract.d.mts","names":[],"sources":["../src/extract.ts"],"mappings":";;;;;;;;
|
|
1
|
+
{"version":3,"file":"extract.d.mts","names":[],"sources":["../src/extract.ts"],"mappings":";;;;;;;;AA0NA;iBAAgB,WAAA,aAAA,CACd,OAAA,EAAS,kBAAA,EACT,KAAA,EAAO,WAAA,CAAY,CAAA;;;;iBAkCL,iBAAA,aAAA,CACd,UAAA,EAAY,IAAA,GACX,UAAA,CAAW,CAAA;;;;iBAWE,iBAAA,aAAA,CACd,MAAA,YACC,UAAU,CAAC,CAAA;AAAA,iBA6BE,sBAAA,CACd,KAAA,EAAO,iBAAA,GACN,mBAAmB;AAAA,iBAoBN,cAAA,aAAA,CACd,KAAA,EAAO,WAAA,CAAY,CAAA,IAClB,kBAAA;AAAA,iBAQmB,mBAAA,aAAA,CACpB,KAAA,EAAO,iBAAA,EACP,OAAA,GAAU,kBAAA,GACT,OAAA,CAAQ,UAAA,CAAW,CAAA;AAAA,iBA4BN,aAAA,CACd,OAAA,EAAS,mBAAA,EACT,KAAA,EAAO,iBAAA,GACN,YAAA;;AAhJoB;AAkCvB;;;;;;;;;;;;;;AAEe;AAWf;;;;;;;;;iBAiKsB,aAAA,aAAA,CACpB,OAAA,EAAS,OAAA,EACT,KAAA,EAAO,WAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,eAAA,CAAgB,CAAA;AAtI3B;;;;;;;;AAEsB;AAoBtB;;;;;;;;;;;;;;AAEqB;AAQrB;;;;;AAhCA,iBAiOsB,OAAA,aAAA,CACpB,OAAA,EAAS,OAAA,EACT,KAAA,EAAO,WAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,MAAA,CAAO,CAAA"}
|
package/dist/extract.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isExtractedSchema, isJsonSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
|
|
1
|
+
import { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
|
|
2
2
|
import { getCacheDirectory, writeSchema } from "./persistence.mjs";
|
|
3
3
|
import { reflectionToJsonSchema } from "./reflection.mjs";
|
|
4
4
|
import { resolve } from "./resolve.mjs";
|
|
@@ -74,6 +74,7 @@ function convertUntypedInputToJsonSchema(input) {
|
|
|
74
74
|
}
|
|
75
75
|
if (isUntypedSchema(value)) properties[key] = convertUntypedSchemaToJsonSchema(value);
|
|
76
76
|
}
|
|
77
|
+
if (!isJsonSchemaObject(base)) throw new Error(`Failed to convert untyped input to JSON Schema. The base schema must be a valid JSON Schema object.`);
|
|
77
78
|
const mergedProperties = {
|
|
78
79
|
...isSetObject$1(base.properties) ? base.properties : {},
|
|
79
80
|
...properties
|
|
@@ -131,13 +132,12 @@ function extractReflection(reflection) {
|
|
|
131
132
|
* Extracts a JSON Schema from Zod, Standard Schema, untyped, or JSON Schema inputs.
|
|
132
133
|
*/
|
|
133
134
|
function extractJsonSchema(schema) {
|
|
134
|
-
if (isSetObject$1(schema) && (isZod3Type(schema) || isStandardJsonSchema(schema) ||
|
|
135
|
+
if (isSetObject$1(schema) && (isZod3Type(schema) || isStandardJsonSchema(schema) || isUntypedInput(schema) || isUntypedSchema(schema))) {
|
|
135
136
|
if (isZod3Type(schema)) return extractJsonSchema$1(schema);
|
|
136
137
|
if (isStandardJsonSchema(schema)) return schema["~standard"].jsonSchema.input({ target: "draft-2020-12" });
|
|
137
138
|
if (isUntypedInput(schema)) return convertUntypedInputToJsonSchema(schema);
|
|
138
139
|
if (isUntypedSchema(schema)) return convertUntypedSchemaToJsonSchema(schema);
|
|
139
|
-
|
|
140
|
-
}
|
|
140
|
+
} else if (isJsonSchema(schema)) return schema;
|
|
141
141
|
}
|
|
142
142
|
function extractResolvedVariant(input) {
|
|
143
143
|
if (isSetObject$1(input)) {
|
package/dist/extract.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extract.mjs","names":["isSetObject","extractJsonSchemaZod"],"sources":["../src/extract.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { Context } from \"@powerlines/core\";\nimport { isTypeDefinition } from \"@powerlines/core\";\nimport { rolldownPlugin } from \"@powerlines/deepkit/rolldown-plugin\";\nimport { isType, stringifyType, Type } from \"@powerlines/deepkit/vendor/type\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { murmurhash } from \"@stryke/hash\";\nimport { isStandardJsonSchema } from \"@stryke/json\";\nimport { joinPaths } from \"@stryke/path/join\";\nimport { isSetString } from \"@stryke/type-checks\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport {\n extractJsonSchema as extractJsonSchemaZod,\n isZod3Type\n} from \"@stryke/zod\";\nimport defu from \"defu\";\nimport type { BuildOptions } from \"rolldown\";\nimport * as z3 from \"zod/v3\";\nimport { getCacheDirectory, writeSchema } from \"./persistence\";\nimport { reflectionToJsonSchema } from \"./reflection\";\nimport { resolve } from \"./resolve\";\nimport {\n isExtractedSchema,\n isJsonSchema,\n isSchema,\n isUntypedInput,\n isUntypedSchema\n} from \"./type-checks\";\nimport {\n ExtractedSchema,\n JsonSchema,\n Schema,\n SchemaInput,\n SchemaInputVariant,\n SchemaSource,\n SchemaSourceInput,\n SchemaSourceVariant,\n TypeDefinitionReference,\n UntypedInputObject,\n UntypedSchema\n} from \"./types\";\n\nfunction convertNestedUntypedSchema(value: unknown): unknown {\n if (isUntypedSchema(value)) {\n return convertUntypedSchemaToJsonSchema(value);\n }\n\n if (isSetObject(value)) {\n if (isUntypedInput(value)) {\n return convertUntypedInputToJsonSchema(value);\n }\n\n const nested = value as Record<string, unknown>;\n if (\"$schema\" in nested && isUntypedSchema(nested.$schema)) {\n return convertUntypedSchemaToJsonSchema(nested.$schema);\n }\n }\n\n return value;\n}\n\nfunction convertNestedUntypedSchemaArray(value: unknown): unknown {\n if (!Array.isArray(value)) {\n return value;\n }\n\n return value.map(item => convertNestedUntypedSchema(item));\n}\n\nfunction convertUntypedSchemaToJsonSchema<T = unknown>(\n schema: UntypedSchema | Record<string, unknown>\n): JsonSchema<T> {\n const source = schema as Record<string, unknown>;\n const jsonSchema: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(source)) {\n if (\n key === \"tsType\" ||\n key === \"markdownType\" ||\n key === \"tags\" ||\n key === \"args\" ||\n key === \"resolve\"\n ) {\n continue;\n }\n\n if (key === \"id\" && isSetString(value)) {\n jsonSchema.$id = value;\n continue;\n }\n\n if (\n key === \"properties\" ||\n key === \"patternProperties\" ||\n key === \"dependentSchemas\" ||\n key === \"$defs\" ||\n key === \"definitions\"\n ) {\n if (!isSetObject(value)) {\n jsonSchema[key] = value;\n continue;\n }\n\n jsonSchema[key] = Object.fromEntries(\n Object.entries(value).map(([propertyKey, propertyValue]) => [\n propertyKey,\n convertNestedUntypedSchema(propertyValue)\n ])\n );\n continue;\n }\n\n if (\n key === \"items\" ||\n key === \"contains\" ||\n key === \"if\" ||\n key === \"then\" ||\n key === \"else\" ||\n key === \"not\" ||\n key === \"propertyNames\" ||\n key === \"additionalProperties\" ||\n key === \"unevaluatedProperties\"\n ) {\n jsonSchema[key] = convertNestedUntypedSchema(value);\n continue;\n }\n\n if (key === \"oneOf\" || key === \"anyOf\" || key === \"allOf\") {\n jsonSchema[key] = convertNestedUntypedSchemaArray(value);\n continue;\n }\n\n jsonSchema[key] = value;\n }\n\n return jsonSchema as JsonSchema<T>;\n}\n\nfunction convertUntypedInputToJsonSchema<T = unknown>(\n input: UntypedInputObject\n): JsonSchema<T> {\n const inputObject = input as Record<string, unknown>;\n const base = isUntypedSchema(inputObject.$schema)\n ? convertUntypedSchemaToJsonSchema<T>(inputObject.$schema)\n : ({} as JsonSchema<T>);\n const properties: Record<string, JsonSchema<T>> = {};\n\n for (const [key, value] of Object.entries(inputObject)) {\n if (key.startsWith(\"$\")) {\n continue;\n }\n\n if (!isSetObject(value)) {\n continue;\n }\n\n if (isUntypedInput(value)) {\n properties[key] = convertUntypedInputToJsonSchema<T>(value);\n continue;\n }\n\n const nested = value as Record<string, unknown>;\n if (\"$schema\" in nested && isUntypedSchema(nested.$schema)) {\n properties[key] = convertUntypedSchemaToJsonSchema<T>(nested.$schema);\n continue;\n }\n\n if (isUntypedSchema(value)) {\n properties[key] = convertUntypedSchemaToJsonSchema<T>(value);\n }\n }\n\n const baseProperties = isSetObject(base.properties)\n ? (base.properties as Record<string, JsonSchema<T>>)\n : {};\n const mergedProperties = {\n ...baseProperties,\n ...properties\n };\n\n return {\n ...base,\n type: base.type ?? \"object\",\n ...(Object.keys(mergedProperties).length > 0\n ? { properties: mergedProperties }\n : {})\n } as JsonSchema<T>;\n}\n\n/**\n * Creates a hash string for a given schema definition input.\n */\nexport function extractHash<T = unknown>(\n variant: SchemaInputVariant,\n input: SchemaInput<T>\n): string {\n if (isSetString(input)) {\n return murmurhash({ variant, input });\n } else if (isSetObject(input)) {\n if (isZod3Type(input)) {\n return murmurhash({ variant, input: input._def });\n } else if (isStandardJsonSchema(input)) {\n return murmurhash({ variant, input: input[\"~standard\"] });\n } else if (isJsonSchema(input)) {\n return murmurhash({ variant, input });\n } else if (isUntypedInput(input)) {\n return murmurhash({\n variant,\n input: convertUntypedInputToJsonSchema(input)\n });\n } else if (isUntypedSchema(input)) {\n return murmurhash({\n variant,\n input: convertUntypedSchemaToJsonSchema(input)\n });\n } else if (isType(input)) {\n return murmurhash({ variant, input: stringifyType(input) });\n }\n }\n\n throw new Error(\n `Failed to create an input hash for the provided schema definition input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object.`\n );\n}\n\n/**\n * Converts a reflected Deepkit {@link Type} into a JSON Schema (draft-07) representation.\n */\nexport function extractReflection<T = unknown>(\n reflection: Type\n): JsonSchema<T> | undefined {\n if (!isType(reflection)) {\n return undefined;\n }\n\n return reflectionToJsonSchema<T>(reflection);\n}\n\n/**\n * Extracts a JSON Schema from Zod, Standard Schema, untyped, or JSON Schema inputs.\n */\nexport function extractJsonSchema<T = unknown>(\n schema: unknown\n): JsonSchema<T> | undefined {\n if (\n isSetObject(schema) &&\n (isZod3Type(schema) ||\n isStandardJsonSchema(schema) ||\n isJsonSchema(schema) ||\n isUntypedInput(schema) ||\n isUntypedSchema(schema))\n ) {\n if (isZod3Type(schema)) {\n return extractJsonSchemaZod(schema) as JsonSchema<T>;\n }\n if (isStandardJsonSchema(schema)) {\n return schema[\"~standard\"].jsonSchema.input({\n target: \"draft-2020-12\"\n }) as JsonSchema<T>;\n }\n if (isUntypedInput(schema)) {\n return convertUntypedInputToJsonSchema<T>(schema);\n }\n if (isUntypedSchema(schema)) {\n return convertUntypedSchemaToJsonSchema<T>(schema);\n }\n return schema;\n }\n\n return undefined;\n}\n\nexport function extractResolvedVariant(\n input: SchemaSourceInput\n): SchemaSourceVariant {\n if (isSetObject(input)) {\n if (isZod3Type(input)) {\n return \"zod3\";\n } else if (isStandardJsonSchema(input)) {\n return \"standard-schema\";\n } else if (isJsonSchema(input)) {\n return \"json-schema\";\n } else if (isType(input)) {\n return \"reflection\";\n } else if (isUntypedInput(input) || isUntypedSchema(input)) {\n return \"untyped\";\n }\n }\n\n throw new Error(\n `Failed to determine the variant of the provided schema definition input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema.`\n );\n}\n\nexport function extractVariant<T = unknown>(\n input: SchemaInput<T>\n): SchemaInputVariant {\n if (isSetString(input) || isTypeDefinition(input)) {\n return \"type-definition\";\n }\n\n return extractResolvedVariant(input as SchemaSourceInput);\n}\n\nexport async function extractSchemaSchema<T = unknown>(\n input: SchemaSourceInput,\n variant?: SchemaInputVariant\n): Promise<JsonSchema<T>> {\n if (isExtractedSchema<T>(input)) {\n return input.schema;\n }\n\n const resolvedVariant = variant ?? extractResolvedVariant(input);\n\n let schema: JsonSchema<T> | undefined;\n if (\n resolvedVariant === \"zod3\" ||\n resolvedVariant === \"json-schema\" ||\n resolvedVariant === \"standard-schema\" ||\n resolvedVariant === \"untyped\"\n ) {\n schema = extractJsonSchema<T>(input);\n } else if (resolvedVariant === \"reflection\") {\n schema = extractReflection(input as Type);\n }\n\n if (schema) {\n return schema;\n }\n\n throw new Error(\n `Failed to extract a valid schema from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.`\n );\n}\n\nexport function extractSource(\n variant: SchemaSourceVariant,\n input: SchemaSourceInput\n): SchemaSource {\n if (variant === \"zod3\") {\n return {\n hash: extractHash(variant, input),\n variant: \"zod3\",\n schema: input as z3.ZodTypeAny\n };\n } else if (variant === \"untyped\") {\n return {\n hash: extractHash(variant, input),\n variant: \"untyped\",\n schema: input as UntypedInputObject | UntypedSchema\n };\n } else if (variant === \"standard-schema\") {\n return {\n hash: extractHash(variant, input),\n variant: \"standard-schema\",\n schema: input as StandardJSONSchemaV1\n };\n } else if (variant === \"json-schema\") {\n return {\n hash: extractHash(variant, input),\n variant: \"json-schema\",\n schema: input as JsonSchema\n };\n } else if (variant === \"reflection\") {\n return {\n hash: extractHash(variant, input),\n variant: \"reflection\",\n schema: input as Type\n };\n }\n\n throw new Error(\n `Failed to extract source information from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.`\n );\n}\n\n/**\n * Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a type definition reference (e.g. a file path with an export), it will be resolved and bundled using ESBuild to obtain the actual schema definition before extraction.\n *\n * @example\n * ```ts\n * // Resolve a schema definition from a file path\n * const schema1 = await extract(context, \"./schemas.ts#MySchema\");\n * // Resolve a schema definition from a JSON Schema object\n * const schema2 = await extract(context, schemaObject);\n * // Resolve a schema definition from a Zod schema\n * const schema3 = await extract(context, zodSchema);\n * // Resolve a schema definition from a reflected Deepkit Type object\n * const schema4 = await extract(context, reflectionType);\n * ```\n *\n * @see https://github.com/colinhacks/zod\n * @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec\n * @see https://json-schema.org/\n * @see https://ajv.js.org/json-type-definition.html\n * @see https://deepkit.io/en/documentation/runtime-types/reflection\n *\n * @param context - The context object providing access to the file system and cache path.\n * @param input - The schema definition input to extract, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a string or a type definition reference, it will be resolved and bundled to obtain the actual schema definition before extraction.\n * @param options - Optional overrides for the ESBuild configuration used during extraction. This can include custom plugins, loaders, or other build options to control how the schema definition is resolved and bundled when the input is a type definition reference.\n * @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object. The function will attempt to extract a valid JSON Schema from the provided input, and if successful, it will return the schema. If the extraction process fails or if the input is not a valid schema definition, it will throw an error indicating the failure.\n */\nexport async function extractSchema<T = unknown>(\n context: Context,\n input: SchemaInput,\n options: Partial<BuildOptions> = {}\n): Promise<ExtractedSchema<T>> {\n if (isExtractedSchema<T>(input)) {\n return input;\n }\n\n if (isSchema<T>(input)) {\n return {\n ...input,\n source: {\n hash: extractHash(\"json-schema\", input.schema),\n variant: \"json-schema\",\n schema: input.schema\n }\n };\n }\n\n let source: SchemaSource;\n\n const variant = extractVariant(input);\n const hash = extractHash(variant, input);\n\n if (variant === \"type-definition\") {\n const resolved = await resolve<SchemaSourceInput>(\n context,\n input as TypeDefinitionReference,\n defu(options, {\n plugins: [\n rolldownPlugin(context, {\n reflection: \"default\",\n level: \"all\"\n })\n ]\n })\n );\n\n source = extractSource(extractResolvedVariant(resolved), resolved);\n } else if (\n [\n \"json-schema\",\n \"standard-schema\",\n \"zod3\",\n \"untyped\",\n \"reflection\"\n ].includes(variant)\n ) {\n source = extractSource(variant, input as SchemaSourceInput);\n } else {\n throw new Error(\n `Invalid schema definition input \"${\n variant\n }\". The variant must be one of \"type-definition\", \"json-schema\", \"standard-schema\", \"zod3\", \"untyped\", or \"reflection\".`\n );\n }\n\n return {\n variant,\n source,\n schema: await extractSchemaSchema<T>(source.schema, source.variant),\n hash\n };\n}\n\n/**\n * Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a type definition reference (e.g. a file path with an export), it will be resolved and bundled using ESBuild to obtain the actual schema definition before extraction.\n *\n * @example\n * ```ts\n * // Resolve a schema definition from a file path\n * const schema1 = await extract(context, \"./schemas.ts#MySchema\");\n * // Resolve a schema definition from a JSON Schema object\n * const schema2 = await extract(context, schemaObject);\n * // Resolve a schema definition from a Zod schema\n * const schema3 = await extract(context, zodSchema);\n * // Resolve a schema definition from a reflected Deepkit Type object\n * const schema4 = await extract(context, reflectionType);\n * ```\n *\n * @see https://github.com/colinhacks/zod\n * @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec\n * @see https://json-schema.org/\n * @see https://ajv.js.org/json-type-definition.html\n * @see https://deepkit.io/en/documentation/runtime-types/reflection\n * @see https://github.com/unjs/untyped\n * @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html\n *\n * @param context - The context object providing access to the file system and cache path.\n * @param input - The schema definition input to extract, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.\n * @param options - Optional overrides for the ESBuild configuration used during extraction.\n * @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object.\n * @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.\n */\nexport async function extract<T = unknown>(\n context: Context,\n input: SchemaInput,\n options: Partial<BuildOptions> = {}\n): Promise<Schema<T>> {\n if (isExtractedSchema<T>(input) || isSchema<T>(input)) {\n return input;\n }\n\n let result: Schema<T> | undefined;\n\n const variant = extractVariant(input);\n const hash = extractHash(variant, input);\n\n const cacheFilePath = joinPaths(getCacheDirectory(context), `${hash}.json`);\n if (\n context.config.skipCache !== true &&\n context.fs.existsSync(cacheFilePath)\n ) {\n const schema = await context.fs.read(cacheFilePath);\n if (schema) {\n result = {\n variant,\n hash,\n schema: JSON.parse(schema) as JsonSchema\n };\n }\n }\n\n result ??= await extractSchema<T>(context, input, options);\n if (!result?.schema) {\n throw new Error(\n `Failed to extract a valid schema from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.`\n );\n }\n\n if (context.config.skipCache !== true) {\n await writeSchema(context, result);\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA2DA,SAAS,2BAA2B,OAAyB;CAC3D,IAAI,gBAAgB,KAAK,GACvB,OAAO,iCAAiC,KAAK;CAG/C,IAAIA,cAAY,KAAK,GAAG;EACtB,IAAI,eAAe,KAAK,GACtB,OAAO,gCAAgC,KAAK;EAG9C,MAAM,SAAS;EACf,IAAI,aAAa,UAAU,gBAAgB,OAAO,OAAO,GACvD,OAAO,iCAAiC,OAAO,OAAO;CAE1D;CAEA,OAAO;AACT;AAEA,SAAS,gCAAgC,OAAyB;CAChE,IAAI,CAAC,MAAM,QAAQ,KAAK,GACtB,OAAO;CAGT,OAAO,MAAM,KAAI,SAAQ,2BAA2B,IAAI,CAAC;AAC3D;AAEA,SAAS,iCACP,QACe;CACf,MAAM,SAAS;CACf,MAAM,aAAsC,CAAC;CAE7C,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,MAAM,GAAG;EACjD,IACE,QAAQ,YACR,QAAQ,kBACR,QAAQ,UACR,QAAQ,UACR,QAAQ,WAER;EAGF,IAAI,QAAQ,QAAQ,YAAY,KAAK,GAAG;GACtC,WAAW,MAAM;GACjB;EACF;EAEA,IACE,QAAQ,gBACR,QAAQ,uBACR,QAAQ,sBACR,QAAQ,WACR,QAAQ,eACR;GACA,IAAI,CAACA,cAAY,KAAK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iB,SAAS,OAAO,OAAO;CACzD,IAAI,CAAC,QAAQ,QACX,MAAM,IAAI,MACR,gMACF;CAGF,IAAI,QAAQ,OAAO,cAAc,MAC/B,MAAM,YAAY,SAAS,MAAM;CAGnC,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"extract.mjs","names":["isSetObject","extractJsonSchemaZod"],"sources":["../src/extract.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { Context } from \"@powerlines/core\";\nimport { isTypeDefinition } from \"@powerlines/core\";\nimport { rolldownPlugin } from \"@powerlines/deepkit/rolldown-plugin\";\nimport { isType, stringifyType, Type } from \"@powerlines/deepkit/vendor/type\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { murmurhash } from \"@stryke/hash\";\nimport { isStandardJsonSchema } from \"@stryke/json\";\nimport { joinPaths } from \"@stryke/path/join\";\nimport { isSetString } from \"@stryke/type-checks\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport {\n extractJsonSchema as extractJsonSchemaZod,\n isZod3Type\n} from \"@stryke/zod\";\nimport defu from \"defu\";\nimport type { BuildOptions } from \"rolldown\";\nimport * as z3 from \"zod/v3\";\nimport { getCacheDirectory, writeSchema } from \"./persistence\";\nimport { reflectionToJsonSchema } from \"./reflection\";\nimport { resolve } from \"./resolve\";\nimport {\n isExtractedSchema,\n isJsonSchema,\n isJsonSchemaObject,\n isSchema,\n isUntypedInput,\n isUntypedSchema\n} from \"./type-checks\";\nimport {\n ExtractedSchema,\n JsonSchema,\n Schema,\n SchemaInput,\n SchemaInputVariant,\n SchemaSource,\n SchemaSourceInput,\n SchemaSourceVariant,\n TypeDefinitionReference,\n UntypedInputObject,\n UntypedSchema\n} from \"./types\";\n\nfunction convertNestedUntypedSchema(value: unknown): unknown {\n if (isUntypedSchema(value)) {\n return convertUntypedSchemaToJsonSchema(value);\n }\n\n if (isSetObject(value)) {\n if (isUntypedInput(value)) {\n return convertUntypedInputToJsonSchema(value);\n }\n\n const nested = value as Record<string, unknown>;\n if (\"$schema\" in nested && isUntypedSchema(nested.$schema)) {\n return convertUntypedSchemaToJsonSchema(nested.$schema);\n }\n }\n\n return value;\n}\n\nfunction convertNestedUntypedSchemaArray(value: unknown): unknown {\n if (!Array.isArray(value)) {\n return value;\n }\n\n return value.map(item => convertNestedUntypedSchema(item));\n}\n\nfunction convertUntypedSchemaToJsonSchema<T = unknown>(\n schema: UntypedSchema | Record<string, unknown>\n): JsonSchema<T> {\n const source = schema as Record<string, unknown>;\n const jsonSchema: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(source)) {\n if (\n key === \"tsType\" ||\n key === \"markdownType\" ||\n key === \"tags\" ||\n key === \"args\" ||\n key === \"resolve\"\n ) {\n continue;\n }\n\n if (key === \"id\" && isSetString(value)) {\n jsonSchema.$id = value;\n continue;\n }\n\n if (\n key === \"properties\" ||\n key === \"patternProperties\" ||\n key === \"dependentSchemas\" ||\n key === \"$defs\" ||\n key === \"definitions\"\n ) {\n if (!isSetObject(value)) {\n jsonSchema[key] = value;\n continue;\n }\n\n jsonSchema[key] = Object.fromEntries(\n Object.entries(value).map(([propertyKey, propertyValue]) => [\n propertyKey,\n convertNestedUntypedSchema(propertyValue)\n ])\n );\n continue;\n }\n\n if (\n key === \"items\" ||\n key === \"contains\" ||\n key === \"if\" ||\n key === \"then\" ||\n key === \"else\" ||\n key === \"not\" ||\n key === \"propertyNames\" ||\n key === \"additionalProperties\" ||\n key === \"unevaluatedProperties\"\n ) {\n jsonSchema[key] = convertNestedUntypedSchema(value);\n continue;\n }\n\n if (key === \"oneOf\" || key === \"anyOf\" || key === \"allOf\") {\n jsonSchema[key] = convertNestedUntypedSchemaArray(value);\n continue;\n }\n\n jsonSchema[key] = value;\n }\n\n return jsonSchema as JsonSchema<T>;\n}\n\nfunction convertUntypedInputToJsonSchema<T = unknown>(\n input: UntypedInputObject\n): JsonSchema<T> {\n const inputObject = input as Record<string, unknown>;\n const base = (\n isUntypedSchema(inputObject.$schema)\n ? convertUntypedSchemaToJsonSchema<T>(inputObject.$schema)\n : {}\n ) as JsonSchema<T>;\n const properties: Record<string, JsonSchema<T>> = {};\n\n for (const [key, value] of Object.entries(inputObject)) {\n if (key.startsWith(\"$\")) {\n continue;\n }\n\n if (!isSetObject(value)) {\n continue;\n }\n\n if (isUntypedInput(value)) {\n properties[key] = convertUntypedInputToJsonSchema<T>(value);\n continue;\n }\n\n const nested = value as Record<string, unknown>;\n if (\"$schema\" in nested && isUntypedSchema(nested.$schema)) {\n properties[key] = convertUntypedSchemaToJsonSchema<T>(nested.$schema);\n continue;\n }\n\n if (isUntypedSchema(value)) {\n properties[key] = convertUntypedSchemaToJsonSchema<T>(value);\n }\n }\n\n if (!isJsonSchemaObject(base)) {\n throw new Error(\n `Failed to convert untyped input to JSON Schema. The base schema must be a valid JSON Schema object.`\n );\n }\n\n const baseProperties = isSetObject(base.properties)\n ? (base.properties as Record<string, JsonSchema<T>>)\n : {};\n const mergedProperties = {\n ...baseProperties,\n ...properties\n };\n\n return {\n ...base,\n type: base.type ?? \"object\",\n ...(Object.keys(mergedProperties).length > 0\n ? { properties: mergedProperties }\n : {})\n };\n}\n\n/**\n * Creates a hash string for a given schema definition input.\n */\nexport function extractHash<T = unknown>(\n variant: SchemaInputVariant,\n input: SchemaInput<T>\n): string {\n if (isSetString(input)) {\n return murmurhash({ variant, input });\n } else if (isSetObject(input)) {\n if (isZod3Type(input)) {\n return murmurhash({ variant, input: input._def });\n } else if (isStandardJsonSchema(input)) {\n return murmurhash({ variant, input: input[\"~standard\"] });\n } else if (isJsonSchema(input)) {\n return murmurhash({ variant, input });\n } else if (isUntypedInput(input)) {\n return murmurhash({\n variant,\n input: convertUntypedInputToJsonSchema(input)\n });\n } else if (isUntypedSchema(input)) {\n return murmurhash({\n variant,\n input: convertUntypedSchemaToJsonSchema(input)\n });\n } else if (isType(input)) {\n return murmurhash({ variant, input: stringifyType(input) });\n }\n }\n\n throw new Error(\n `Failed to create an input hash for the provided schema definition input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object.`\n );\n}\n\n/**\n * Converts a reflected Deepkit {@link Type} into a JSON Schema (draft-07) representation.\n */\nexport function extractReflection<T = unknown>(\n reflection: Type\n): JsonSchema<T> | undefined {\n if (!isType(reflection)) {\n return undefined;\n }\n\n return reflectionToJsonSchema<T>(reflection);\n}\n\n/**\n * Extracts a JSON Schema from Zod, Standard Schema, untyped, or JSON Schema inputs.\n */\nexport function extractJsonSchema<T = unknown>(\n schema: unknown\n): JsonSchema<T> | undefined {\n if (\n isSetObject(schema) &&\n (isZod3Type(schema) ||\n isStandardJsonSchema(schema) ||\n isUntypedInput(schema) ||\n isUntypedSchema(schema))\n ) {\n if (isZod3Type(schema)) {\n return extractJsonSchemaZod(schema) as JsonSchema<T>;\n }\n if (isStandardJsonSchema(schema)) {\n return schema[\"~standard\"].jsonSchema.input({\n target: \"draft-2020-12\"\n }) as JsonSchema<T>;\n }\n if (isUntypedInput(schema)) {\n return convertUntypedInputToJsonSchema<T>(schema);\n }\n if (isUntypedSchema(schema)) {\n return convertUntypedSchemaToJsonSchema<T>(schema);\n }\n } else if (isJsonSchema<T>(schema)) {\n return schema;\n }\n\n return undefined;\n}\n\nexport function extractResolvedVariant(\n input: SchemaSourceInput\n): SchemaSourceVariant {\n if (isSetObject(input)) {\n if (isZod3Type(input)) {\n return \"zod3\";\n } else if (isStandardJsonSchema(input)) {\n return \"standard-schema\";\n } else if (isJsonSchema(input)) {\n return \"json-schema\";\n } else if (isType(input)) {\n return \"reflection\";\n } else if (isUntypedInput(input) || isUntypedSchema(input)) {\n return \"untyped\";\n }\n }\n\n throw new Error(\n `Failed to determine the variant of the provided schema definition input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema.`\n );\n}\n\nexport function extractVariant<T = unknown>(\n input: SchemaInput<T>\n): SchemaInputVariant {\n if (isSetString(input) || isTypeDefinition(input)) {\n return \"type-definition\";\n }\n\n return extractResolvedVariant(input as SchemaSourceInput);\n}\n\nexport async function extractSchemaSchema<T = unknown>(\n input: SchemaSourceInput,\n variant?: SchemaInputVariant\n): Promise<JsonSchema<T>> {\n if (isExtractedSchema<T>(input)) {\n return input.schema;\n }\n\n const resolvedVariant = variant ?? extractResolvedVariant(input);\n\n let schema: JsonSchema<T> | undefined;\n if (\n resolvedVariant === \"zod3\" ||\n resolvedVariant === \"json-schema\" ||\n resolvedVariant === \"standard-schema\" ||\n resolvedVariant === \"untyped\"\n ) {\n schema = extractJsonSchema<T>(input);\n } else if (resolvedVariant === \"reflection\") {\n schema = extractReflection(input as Type);\n }\n\n if (schema) {\n return schema;\n }\n\n throw new Error(\n `Failed to extract a valid schema from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.`\n );\n}\n\nexport function extractSource(\n variant: SchemaSourceVariant,\n input: SchemaSourceInput\n): SchemaSource {\n if (variant === \"zod3\") {\n return {\n hash: extractHash(variant, input),\n variant: \"zod3\",\n schema: input as z3.ZodTypeAny\n };\n } else if (variant === \"untyped\") {\n return {\n hash: extractHash(variant, input),\n variant: \"untyped\",\n schema: input as UntypedInputObject | UntypedSchema\n };\n } else if (variant === \"standard-schema\") {\n return {\n hash: extractHash(variant, input),\n variant: \"standard-schema\",\n schema: input as StandardJSONSchemaV1\n };\n } else if (variant === \"json-schema\") {\n return {\n hash: extractHash(variant, input),\n variant: \"json-schema\",\n schema: input as JsonSchema\n };\n } else if (variant === \"reflection\") {\n return {\n hash: extractHash(variant, input),\n variant: \"reflection\",\n schema: input as Type\n };\n }\n\n throw new Error(\n `Failed to extract source information from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.`\n );\n}\n\n/**\n * Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a type definition reference (e.g. a file path with an export), it will be resolved and bundled using ESBuild to obtain the actual schema definition before extraction.\n *\n * @example\n * ```ts\n * // Resolve a schema definition from a file path\n * const schema1 = await extract(context, \"./schemas.ts#MySchema\");\n * // Resolve a schema definition from a JSON Schema object\n * const schema2 = await extract(context, schemaObject);\n * // Resolve a schema definition from a Zod schema\n * const schema3 = await extract(context, zodSchema);\n * // Resolve a schema definition from a reflected Deepkit Type object\n * const schema4 = await extract(context, reflectionType);\n * ```\n *\n * @see https://github.com/colinhacks/zod\n * @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec\n * @see https://json-schema.org/\n * @see https://ajv.js.org/json-type-definition.html\n * @see https://deepkit.io/en/documentation/runtime-types/reflection\n *\n * @param context - The context object providing access to the file system and cache path.\n * @param input - The schema definition input to extract, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a string or a type definition reference, it will be resolved and bundled to obtain the actual schema definition before extraction.\n * @param options - Optional overrides for the ESBuild configuration used during extraction. This can include custom plugins, loaders, or other build options to control how the schema definition is resolved and bundled when the input is a type definition reference.\n * @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object. The function will attempt to extract a valid JSON Schema from the provided input, and if successful, it will return the schema. If the extraction process fails or if the input is not a valid schema definition, it will throw an error indicating the failure.\n */\nexport async function extractSchema<T = unknown>(\n context: Context,\n input: SchemaInput,\n options: Partial<BuildOptions> = {}\n): Promise<ExtractedSchema<T>> {\n if (isExtractedSchema<T>(input)) {\n return input;\n }\n\n if (isSchema<T>(input)) {\n return {\n ...input,\n source: {\n hash: extractHash(\"json-schema\", input.schema),\n variant: \"json-schema\",\n schema: input.schema\n }\n };\n }\n\n let source: SchemaSource;\n\n const variant = extractVariant(input);\n const hash = extractHash(variant, input);\n\n if (variant === \"type-definition\") {\n const resolved = await resolve<SchemaSourceInput>(\n context,\n input as TypeDefinitionReference,\n defu(options, {\n plugins: [\n rolldownPlugin(context, {\n reflection: \"default\",\n level: \"all\"\n })\n ]\n })\n );\n\n source = extractSource(extractResolvedVariant(resolved), resolved);\n } else if (\n [\n \"json-schema\",\n \"standard-schema\",\n \"zod3\",\n \"untyped\",\n \"reflection\"\n ].includes(variant)\n ) {\n source = extractSource(variant, input as SchemaSourceInput);\n } else {\n throw new Error(\n `Invalid schema definition input \"${\n variant\n }\". The variant must be one of \"type-definition\", \"json-schema\", \"standard-schema\", \"zod3\", \"untyped\", or \"reflection\".`\n );\n }\n\n return {\n variant,\n source,\n schema: await extractSchemaSchema<T>(source.schema, source.variant),\n hash\n };\n}\n\n/**\n * Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object. If the input is a type definition reference (e.g. a file path with an export), it will be resolved and bundled using ESBuild to obtain the actual schema definition before extraction.\n *\n * @example\n * ```ts\n * // Resolve a schema definition from a file path\n * const schema1 = await extract(context, \"./schemas.ts#MySchema\");\n * // Resolve a schema definition from a JSON Schema object\n * const schema2 = await extract(context, schemaObject);\n * // Resolve a schema definition from a Zod schema\n * const schema3 = await extract(context, zodSchema);\n * // Resolve a schema definition from a reflected Deepkit Type object\n * const schema4 = await extract(context, reflectionType);\n * ```\n *\n * @see https://github.com/colinhacks/zod\n * @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec\n * @see https://json-schema.org/\n * @see https://ajv.js.org/json-type-definition.html\n * @see https://deepkit.io/en/documentation/runtime-types/reflection\n * @see https://github.com/unjs/untyped\n * @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html\n *\n * @param context - The context object providing access to the file system and cache path.\n * @param input - The schema definition input to extract, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.\n * @param options - Optional overrides for the ESBuild configuration used during extraction.\n * @returns A promise that resolves to the extracted and normalized schema as a JSON Schema object.\n * @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.\n */\nexport async function extract<T = unknown>(\n context: Context,\n input: SchemaInput,\n options: Partial<BuildOptions> = {}\n): Promise<Schema<T>> {\n if (isExtractedSchema<T>(input) || isSchema<T>(input)) {\n return input;\n }\n\n let result: Schema<T> | undefined;\n\n const variant = extractVariant(input);\n const hash = extractHash(variant, input);\n\n const cacheFilePath = joinPaths(getCacheDirectory(context), `${hash}.json`);\n if (\n context.config.skipCache !== true &&\n context.fs.existsSync(cacheFilePath)\n ) {\n const schema = await context.fs.read(cacheFilePath);\n if (schema) {\n result = {\n variant,\n hash,\n schema: JSON.parse(schema) as JsonSchema<T>\n };\n }\n }\n\n result ??= await extractSchema<T>(context, input, options);\n if (!result?.schema) {\n throw new Error(\n `Failed to extract a valid schema from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.`\n );\n }\n\n if (context.config.skipCache !== true) {\n await writeSchema(context, result);\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA4DA,SAAS,2BAA2B,OAAyB;CAC3D,IAAI,gBAAgB,KAAK,GACvB,OAAO,iCAAiC,KAAK;CAG/C,IAAIA,cAAY,KAAK,GAAG;EACtB,IAAI,eAAe,KAAK,GACtB,OAAO,gCAAgC,KAAK;EAG9C,MAAM,SAAS;EACf,IAAI,aAAa,UAAU,gBAAgB,OAAO,OAAO,GACvD,OAAO,iCAAiC,OAAO,OAAO;CAE1D;CAEA,OAAO;AACT;AAEA,SAAS,gCAAgC,OAAyB;CAChE,IAAI,CAAC,MAAM,QAAQ,KAAK,GACtB,OAAO;CAGT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;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,eAAsB,QACpB,SACA,OACA,UAAiC,CAAC,GACd;CACpB,IAAI,kBAAqB,KAAK,KAAK,SAAY,KAAK,GAClD,OAAO;CAGT,IAAI;CAEJ,MAAM,UAAU,eAAe,KAAK;CACpC,MAAM,OAAO,YAAY,SAAS,KAAK;CAEvC,MAAM,gBAAgB,UAAU,kBAAkB,OAAO,GAAG,GAAG,KAAK,MAAM;CAC1E,IACE,QAAQ,OAAO,cAAc,QAC7B,QAAQ,GAAG,WAAW,aAAa,GACnC;EACA,MAAM,SAAS,MAAM,QAAQ,GAAG,KAAK,aAAa;EAClD,IAAI,QACF,SAAS;GACP;GACA;GACA,QAAQ,KAAK,MAAM,MAAM;EAC3B;CAEJ;CAEA,WAAW,MAAM,cAAiB,SAAS,OAAO,OAAO;CACzD,IAAI,CAAC,QAAQ,QACX,MAAM,IAAI,MACR,gMACF;CAGF,IAAI,QAAQ,OAAO,cAAc,MAC/B,MAAM,YAAY,SAAS,MAAM;CAGnC,OAAO;AACT"}
|
package/dist/helpers.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.cts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;AAsCA;;;;;;;iBAAgB,aAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,IAAK,MAAA,SAAe,kBAAA,CAAmB,CAAA;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"helpers.d.cts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;AAsCA;;;;;;;iBAAgB,aAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,IAAK,MAAA,SAAe,kBAAA,CAAmB,CAAA;;;;;;;;;;iBA6BrD,iBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,IAAK,KAAA,CAAM,kBAAA,CAAmB,CAAA;;;;;;;;;AA/BU;AA6BtE;;iBAiBgB,WAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAEhC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,GAC5B,IAAA,UACA,QAAA,EAAU,kBAAA,CAAmB,CAAA;;;;;;;;;;iBA+Bf,YAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAAA,GAC7B,OAAA,GAAU,UAAA,CAAW,CAAA,IAAK,MAAA,CAAO,CAAA,OAAQ,UAAA,CAAW,CAAA"}
|
package/dist/helpers.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.mts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;AAsCA;;;;;;;iBAAgB,aAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,IAAK,MAAA,SAAe,kBAAA,CAAmB,CAAA;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"helpers.d.mts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;AAsCA;;;;;;;iBAAgB,aAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,IAAK,MAAA,SAAe,kBAAA,CAAmB,CAAA;;;;;;;;;;iBA6BrD,iBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,IAAK,KAAA,CAAM,kBAAA,CAAmB,CAAA;;;;;;;;;AA/BU;AA6BtE;;iBAiBgB,WAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAEhC,GAAA,EAAK,MAAA,CAAO,CAAA,IAAK,UAAA,CAAW,CAAA,GAC5B,IAAA,UACA,QAAA,EAAU,kBAAA,CAAmB,CAAA;;;;;;;;;;iBA+Bf,YAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAAA,GAC7B,OAAA,GAAU,UAAA,CAAW,CAAA,IAAK,MAAA,CAAO,CAAA,OAAQ,UAAA,CAAW,CAAA"}
|
package/dist/helpers.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.mjs","names":[],"sources":["../src/helpers.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isSetObject } from \"@stryke/type-checks\";\nimport { defu } from \"defu\";\nimport { isPropertyOptional, isSchemaNullable } from \"./metadata\";\nimport { isJsonSchemaObject, isSchema } from \"./type-checks\";\nimport {\n JsonSchema,\n JsonSchemaObject,\n JsonSchemaProperty,\n Schema\n} from \"./types\";\n\n/**\n * Extracts object properties from a JSON Schema object form.\n *\n * @remarks\n * This function returns an empty object if the schema is not an object form or if it has no properties.\n *\n * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.\n * @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.\n */\nexport function getProperties<\n T extends Record<string, any> = Record<string, any>\n>(obj: Schema<T> | JsonSchema<T>): Record<string, JsonSchemaProperty<T>> {\n const properties: Record<string, JsonSchemaProperty<T>> = {};\n const schema: JsonSchema<T> = isSchema<T>(obj) ? obj.schema : obj;\n if (!isJsonSchemaObject<T>(schema) || !isSetObject(schema.properties)) {\n return properties;\n }\n\n for (const [key, value] of Object.entries(schema.properties)) {\n properties[key] = {\n ...value,\n name: key,\n nullable
|
|
1
|
+
{"version":3,"file":"helpers.mjs","names":[],"sources":["../src/helpers.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isSetObject } from \"@stryke/type-checks\";\nimport { defu } from \"defu\";\nimport { isPropertyOptional, isSchemaNullable } from \"./metadata\";\nimport { isJsonSchemaObject, isSchema } from \"./type-checks\";\nimport {\n JsonSchema,\n JsonSchemaObject,\n JsonSchemaProperty,\n Schema\n} from \"./types\";\n\n/**\n * Extracts object properties from a JSON Schema object form.\n *\n * @remarks\n * This function returns an empty object if the schema is not an object form or if it has no properties.\n *\n * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.\n * @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.\n */\nexport function getProperties<\n T extends Record<string, any> = Record<string, any>\n>(obj: Schema<T> | JsonSchema<T>): Record<string, JsonSchemaProperty<T>> {\n const properties: Record<string, JsonSchemaProperty<T>> = {};\n const schema: JsonSchema<T> = isSchema<T>(obj) ? obj.schema : obj;\n if (!isJsonSchemaObject<T>(schema) || !isSetObject(schema.properties)) {\n return properties;\n }\n\n for (const [key, value] of Object.entries(schema.properties)) {\n properties[key] = {\n ...value,\n name: key,\n nullable:\n isSchemaNullable(value as JsonSchema<any>) ||\n isPropertyOptional(schema, key)\n };\n }\n\n return properties;\n}\n\n/**\n * Returns object properties as an array.\n *\n * @remarks\n * This is a convenience function that extracts properties using `getProperties` and returns them as an array.\n *\n * @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.\n * @returns An array of JSON Schema fragments representing the properties, each including metadata.\n */\nexport function getPropertiesList<\n T extends Record<string, any> = Record<string, any>\n>(obj: Schema<T> | JsonSchema<T>): Array<JsonSchemaProperty<T>> {\n return Object.values(getProperties<T>(obj));\n}\n\n/**\n * Adds a property to a JSON Schema object form.\n *\n * @remarks\n * 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.\n *\n * @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.\n * @param name - The name of the property to add.\n * @param property - The JSON Schema fragment representing the property's schema, including metadata.\n * @throws Will throw an error if the provided schema is not an object form.\n */\nexport function addProperty<\n T extends Record<string, any> = Record<string, any>\n>(\n obj: Schema<T> | JsonSchema<T>,\n name: string,\n property: JsonSchemaProperty<T>\n): void {\n const schema = (isSchema<T>(obj) ? obj.schema : obj) as JsonSchemaObject<T>;\n if (!isJsonSchemaObject<T>(schema)) {\n throw new Error(\"Cannot add property to non-object schema\");\n }\n\n schema.properties ??= {};\n schema.required ??= [];\n\n schema.properties[name] = { ...property, name };\n if (property?.optional) {\n schema.required = schema.required.filter(key => key !== name);\n } else if (!schema.required.includes(name)) {\n schema.required.push(name);\n }\n\n if (schema.required.length === 0) {\n delete schema.required;\n }\n}\n\n/**\n * Merges multiple JSON Schema object forms into one.\n *\n * @remarks\n * This function takes multiple JSON Schema objects or Schema wrappers and merges them into a single JSON Schema object. The merging process combines properties and metadata from all provided schemas, with later schemas in the arguments list taking precedence over earlier ones in case of conflicts. The resulting schema will include all unique properties and metadata from the input schemas.\n *\n * @param schemas - An array of JSON Schema objects or Schema wrappers to merge.\n * @returns A new JSON Schema object that is the result of merging all input schemas.\n */\nexport function mergeSchemas<\n T extends Record<string, any> = Record<string, any>\n>(...schemas: (JsonSchema<T> | Schema<T>)[]): JsonSchema<T> {\n const result: JsonSchema<T> = {} as JsonSchema<T>;\n for (const schema of schemas) {\n const jsonSchema: JsonSchema<T> = isSchema<T>(schema)\n ? schema.schema\n : schema;\n if (!isJsonSchemaObject<T>(jsonSchema)) {\n continue;\n }\n\n defu(result, jsonSchema);\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAsCA,SAAgB,cAEd,KAAuE;CACvE,MAAM,aAAoD,CAAC;CAC3D,MAAM,SAAwB,SAAY,GAAG,IAAI,IAAI,SAAS;CAC9D,IAAI,CAAC,mBAAsB,MAAM,KAAK,CAAC,YAAY,OAAO,UAAU,GAClE,OAAO;CAGT,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,UAAU,GACzD,WAAW,OAAO;EAChB,GAAG;EACH,MAAM;EACN,UACE,iBAAiB,KAAwB,KACzC,mBAAmB,QAAQ,GAAG;CAClC;CAGF,OAAO;AACT;;;;;;;;;;AAWA,SAAgB,kBAEd,KAA8D;CAC9D,OAAO,OAAO,OAAO,cAAiB,GAAG,CAAC;AAC5C;;;;;;;;;;;;AAaA,SAAgB,YAGd,KACA,MACA,UACM;CACN,MAAM,SAAU,SAAY,GAAG,IAAI,IAAI,SAAS;CAChD,IAAI,CAAC,mBAAsB,MAAM,GAC/B,MAAM,IAAI,MAAM,0CAA0C;CAG5D,OAAO,eAAe,CAAC;CACvB,OAAO,aAAa,CAAC;CAErB,OAAO,WAAW,QAAQ;EAAE,GAAG;EAAU;CAAK;CAC9C,IAAI,UAAU,UACZ,OAAO,WAAW,OAAO,SAAS,QAAO,QAAO,QAAQ,IAAI;MACvD,IAAI,CAAC,OAAO,SAAS,SAAS,IAAI,GACvC,OAAO,SAAS,KAAK,IAAI;CAG3B,IAAI,OAAO,SAAS,WAAW,GAC7B,OAAO,OAAO;AAElB;;;;;;;;;;AAWA,SAAgB,aAEd,GAAG,SAAuD;CAC1D,MAAM,SAAwB,CAAC;CAC/B,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,aAA4B,SAAY,MAAM,IAChD,OAAO,SACP;EACJ,IAAI,CAAC,mBAAsB,UAAU,GACnC;EAGF,OAAK,QAAQ,UAAU;CACzB;CAEA,OAAO;AACT"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BundleOptions, bundle } from "./bundle.cjs";
|
|
2
|
-
import { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource } from "./types.cjs";
|
|
2
|
+
import { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaNullable, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource } from "./types.cjs";
|
|
3
3
|
import { generateCode, isNullableSchema, stringifyType, stringifyValue } from "./codegen.cjs";
|
|
4
4
|
import { JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchemaTypes } from "./constants.cjs";
|
|
5
5
|
import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant } from "./extract.cjs";
|
|
@@ -9,4 +9,4 @@ import { getCacheDirectory, getCacheFilePath, readSchema, readSchemaSafe, writeS
|
|
|
9
9
|
import { reflectionToJsonSchema } from "./reflection.cjs";
|
|
10
10
|
import { resolve, resolveModule, resolveReflection } from "./resolve.cjs";
|
|
11
11
|
import { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.cjs";
|
|
12
|
-
export { BaseSchemaSource, BundleOptions, ExtractedSchema, JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonSchemaTypes, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, addProperty, applySchemaMetadata, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getPrimarySchemaType, getProperties, getPropertiesList, isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isNullableSchema, isPropertyOptional, isSchema, isSchemaNullable, isUntypedInput, isUntypedSchema, mergeSchemas, readSchema, readSchemaSafe, readSchemaTypes, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
|
|
12
|
+
export { BaseSchemaSource, BundleOptions, ExtractedSchema, JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchema, JsonSchemaLike, JsonSchemaNullable, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonSchemaTypes, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, addProperty, applySchemaMetadata, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getPrimarySchemaType, getProperties, getPropertiesList, isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isNullableSchema, isPropertyOptional, isSchema, isSchemaNullable, isUntypedInput, isUntypedSchema, mergeSchemas, readSchema, readSchemaSafe, readSchemaTypes, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BundleOptions, bundle } from "./bundle.mjs";
|
|
2
|
-
import { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource } from "./types.mjs";
|
|
2
|
+
import { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaNullable, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource } from "./types.mjs";
|
|
3
3
|
import { generateCode, isNullableSchema, stringifyType, stringifyValue } from "./codegen.mjs";
|
|
4
4
|
import { JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchemaTypes } from "./constants.mjs";
|
|
5
5
|
import { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant } from "./extract.mjs";
|
|
@@ -9,4 +9,4 @@ import { getCacheDirectory, getCacheFilePath, readSchema, readSchemaSafe, writeS
|
|
|
9
9
|
import { reflectionToJsonSchema } from "./reflection.mjs";
|
|
10
10
|
import { resolve, resolveModule, resolveReflection } from "./resolve.mjs";
|
|
11
11
|
import { isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
|
|
12
|
-
export { BaseSchemaSource, BundleOptions, ExtractedSchema, JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonSchemaTypes, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, addProperty, applySchemaMetadata, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getPrimarySchemaType, getProperties, getPropertiesList, isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isNullableSchema, isPropertyOptional, isSchema, isSchemaNullable, isUntypedInput, isUntypedSchema, mergeSchemas, readSchema, readSchemaSafe, readSchemaTypes, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
|
|
12
|
+
export { BaseSchemaSource, BundleOptions, ExtractedSchema, JSON_SCHEMA_DATA_TYPES, JSON_SCHEMA_METADATA_KEYS, JsonSchema, JsonSchemaLike, JsonSchemaNullable, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonSchemaTypes, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource, addProperty, applySchemaMetadata, bundle, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant, generateCode, getCacheDirectory, getCacheFilePath, getPrimarySchemaType, getProperties, getPropertiesList, isExtractedSchema, isJsonSchema, isJsonSchemaObject, isNullOnlyJsonSchema, isNullableSchema, isPropertyOptional, isSchema, isSchemaNullable, isUntypedInput, isUntypedSchema, mergeSchemas, readSchema, readSchemaSafe, readSchemaTypes, reflectionToJsonSchema, resolve, resolveModule, resolveReflection, stringifyType, stringifyValue, writeSchema };
|
package/dist/metadata.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metadata.d.cts","names":[],"sources":["../src/metadata.ts"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"metadata.d.cts","names":[],"sources":["../src/metadata.ts"],"mappings":";;;;;AAoCA;;;;;iBAAgB,mBAAA,gCAEI,cAAA,GAAiB,cAAA,CAAA,CACnC,MAAA,EAAQ,UAAA,CAAW,CAAA,GAAI,QAAA,EAAU,SAAA,eAAwB,UAAA,CAAW,CAAA;;;;;;;;;;iBA0BtD,gBAAA,aAAA,CAA8B,MAAA,GAAS,UAAU,CAAC,CAAA;;;;;;;;;AA1BK;AA0BvE;iBAwBgB,kBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,MAAA,EAAQ,gBAAA,CAAiB,CAAA,GAAI,YAAA;;;;;;;;AA1BqC;AAwBpE;iBAiBgB,eAAA,aAAA,CACd,MAAA,GAAS,UAAA,CAAW,CAAA,IACnB,uBAAA;;;;;;;iBAgBa,oBAAA,aAAA,CACd,MAAA,GAAS,UAAA,CAAW,CAAA,IACnB,uBAAA"}
|
package/dist/metadata.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metadata.d.mts","names":[],"sources":["../src/metadata.ts"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"metadata.d.mts","names":[],"sources":["../src/metadata.ts"],"mappings":";;;;;AAoCA;;;;;iBAAgB,mBAAA,gCAEI,cAAA,GAAiB,cAAA,CAAA,CACnC,MAAA,EAAQ,UAAA,CAAW,CAAA,GAAI,QAAA,EAAU,SAAA,eAAwB,UAAA,CAAW,CAAA;;;;;;;;;;iBA0BtD,gBAAA,aAAA,CAA8B,MAAA,GAAS,UAAU,CAAC,CAAA;;;;;;;;;AA1BK;AA0BvE;iBAwBgB,kBAAA,WACJ,MAAA,gBAAsB,MAAA,cAAA,CAChC,MAAA,EAAQ,gBAAA,CAAiB,CAAA,GAAI,YAAA;;;;;;;;AA1BqC;AAwBpE;iBAiBgB,eAAA,aAAA,CACd,MAAA,GAAS,UAAA,CAAW,CAAA,IACnB,uBAAA;;;;;;;iBAgBa,oBAAA,aAAA,CACd,MAAA,GAAS,UAAA,CAAW,CAAA,IACnB,uBAAA"}
|
package/dist/metadata.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metadata.mjs","names":["isSetObject"],"sources":["../src/metadata.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isSetString } from \"@stryke/type-checks\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { JSON_SCHEMA_METADATA_KEYS } from \"./constants\";\nimport {\n JsonSchema,\n JsonSchemaObject,\n JsonSchemaPrimitiveType,\n SchemaMetadata\n} from \"./types\";\n\n/**\n * Applies Powerlines schema metadata onto a JSON Schema fragment.\n *\n * @param schema - The JSON Schema fragment to apply metadata to.\n * @param metadata - The Powerlines schema metadata to apply.\n * @returns A new JSON Schema fragment with the metadata applied.\n */\nexport function applySchemaMetadata<\n T = unknown,\n TMetadata extends SchemaMetadata = SchemaMetadata\n>(schema: JsonSchema<T>, metadata: TMetadata | undefined): JsonSchema<T> {\n if (!metadata) {\n return schema;\n }\n\n const result: JsonSchema<T> = { ...schema };\n for (const key of JSON_SCHEMA_METADATA_KEYS) {\n const value = metadata[key];\n if (value !== undefined && value !== null) {\n result[key as keyof JsonSchema<T>]
|
|
1
|
+
{"version":3,"file":"metadata.mjs","names":["isSetObject"],"sources":["../src/metadata.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isSetString } from \"@stryke/type-checks\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { JSON_SCHEMA_METADATA_KEYS } from \"./constants\";\nimport {\n JsonSchema,\n JsonSchemaNullable,\n JsonSchemaObject,\n JsonSchemaPrimitiveType,\n SchemaMetadata\n} from \"./types\";\n\n/**\n * Applies Powerlines schema metadata onto a JSON Schema fragment.\n *\n * @param schema - The JSON Schema fragment to apply metadata to.\n * @param metadata - The Powerlines schema metadata to apply.\n * @returns A new JSON Schema fragment with the metadata applied.\n */\nexport function applySchemaMetadata<\n T = unknown,\n TMetadata extends SchemaMetadata = SchemaMetadata\n>(schema: JsonSchema<T>, metadata: TMetadata | undefined): JsonSchema<T> {\n if (!metadata) {\n return schema;\n }\n\n const result: JsonSchema<T> = { ...schema };\n for (const key of JSON_SCHEMA_METADATA_KEYS) {\n const value = metadata[key];\n if (value !== undefined && value !== null) {\n result[key as keyof JsonSchema<T>] =\n value as JsonSchema<T>[keyof JsonSchema<T>];\n }\n }\n\n return result;\n}\n\n/**\n * Returns whether a JSON Schema fragment accepts `null`.\n *\n * @remarks\n * This is true if the schema has `nullable: true` or if its `type` includes `\"null\"`.\n *\n * @param schema - The JSON Schema fragment to check.\n * @returns `true` if the schema accepts `null`, otherwise `false`.\n */\nexport function isSchemaNullable<T = unknown>(schema?: JsonSchema<T>): boolean {\n if (!isSetObject(schema)) {\n return false;\n }\n\n if ((schema as JsonSchemaNullable<T>).nullable === true) {\n return true;\n }\n\n const types = readSchemaTypes(schema);\n\n return types.includes(\"null\");\n}\n\n/**\n * Returns whether an object property is optional (not listed in `required`).\n *\n * @remarks\n * 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.\n *\n * @param parent - The parent JSON Schema object containing the property.\n * @param propertyName - The name of the property to check for optionality.\n * @returns `true` if the property is optional, otherwise `false`.\n */\nexport function isPropertyOptional<\n T extends Record<string, any> = Record<string, any>\n>(parent: JsonSchemaObject<T>, propertyName: string): boolean {\n const required = parent.required ?? [];\n\n return !required.includes(propertyName);\n}\n\n/**\n * Normalizes the JSON Schema `type` keyword to a string array.\n *\n * @remarks\n * This function ensures that the `type` keyword of a JSON Schema fragment is always represented as an array of strings, even if it was originally defined as a single string. This normalization simplifies type checking and processing of JSON Schemas by providing a consistent format for the `type` information.\n *\n * @param schema - The JSON Schema fragment to read types from.\n * @returns An array of JSON Schema primitive type names defined in the `type` keyword, or an empty array if no valid types are found.\n */\nexport function readSchemaTypes<T = unknown>(\n schema?: JsonSchema<T>\n): JsonSchemaPrimitiveType[] {\n if (Array.isArray(schema?.type)) {\n return schema.type.filter(isSetString) as JsonSchemaPrimitiveType[];\n }\n if (isSetString(schema?.type)) {\n return [schema.type as JsonSchemaPrimitiveType];\n }\n return [];\n}\n\n/**\n * Returns the primary non-null JSON Schema type name for a fragment.\n *\n * @param schema - The JSON Schema fragment to check.\n * @returns The primary non-null JSON Schema type name, or `undefined` if none is found.\n */\nexport function getPrimarySchemaType<T = unknown>(\n schema?: JsonSchema<T>\n): JsonSchemaPrimitiveType | undefined {\n if (!isSetObject(schema)) {\n return undefined;\n }\n\n return readSchemaTypes(schema).find(type => type !== \"null\");\n}\n"],"mappings":";;;;;;;;;;;;AAoCA,SAAgB,oBAGd,QAAuB,UAAgD;CACvE,IAAI,CAAC,UACH,OAAO;CAGT,MAAM,SAAwB,EAAE,GAAG,OAAO;CAC1C,KAAK,MAAM,OAAO,2BAA2B;EAC3C,MAAM,QAAQ,SAAS;EACvB,IAAI,UAAU,UAAa,UAAU,MACnC,OAAO,OACL;CAEN;CAEA,OAAO;AACT;;;;;;;;;;AAWA,SAAgB,iBAA8B,QAAiC;CAC7E,IAAI,CAACA,cAAY,MAAM,GACrB,OAAO;CAGT,IAAK,OAAiC,aAAa,MACjD,OAAO;CAKT,OAFc,gBAAgB,MAEnB,EAAE,SAAS,MAAM;AAC9B;;;;;;;;;;;AAYA,SAAgB,mBAEd,QAA6B,cAA+B;CAG5D,OAAO,EAFU,OAAO,YAAY,CAAC,GAEpB,SAAS,YAAY;AACxC;;;;;;;;;;AAWA,SAAgB,gBACd,QAC2B;CAC3B,IAAI,MAAM,QAAQ,QAAQ,IAAI,GAC5B,OAAO,OAAO,KAAK,OAAO,WAAW;CAEvC,IAAI,YAAY,QAAQ,IAAI,GAC1B,OAAO,CAAC,OAAO,IAA+B;CAEhD,OAAO,CAAC;AACV;;;;;;;AAQA,SAAgB,qBACd,QACqC;CACrC,IAAI,CAACA,cAAY,MAAM,GACrB;CAGF,OAAO,gBAAgB,MAAM,EAAE,MAAK,SAAQ,SAAS,MAAM;AAC7D"}
|
package/dist/type-checks.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-checks.mjs","names":[],"sources":["../src/type-checks.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isJsonSchemaObjectType } from \"@stryke/json\";\nimport {\n isFunction,\n isObject,\n isSetObject,\n isSetString\n} from \"@stryke/type-checks\";\nimport {\n InputObject as UntypedInputObject,\n Schema as UntypedSchema\n} from \"untyped\";\nimport { JSON_SCHEMA_DATA_TYPES } from \"./constants\";\nimport { ExtractedSchema, JsonSchema, JsonSchemaObject, Schema } from \"./types\";\n\ntype JsonSchemaDataType = (typeof JSON_SCHEMA_DATA_TYPES)[number];\n\nconst JSON_SCHEMA_DATA_TYPE_SET = new Set<JsonSchemaDataType>(\n JSON_SCHEMA_DATA_TYPES\n);\n\ntype SchemaKeywordValidator = (value: unknown) => boolean;\n\nconst isSetNumber = (value: unknown): value is number =>\n typeof value === \"number\" && Number.isFinite(value);\n\nconst isSetBoolean = (value: unknown): value is boolean =>\n typeof value === \"boolean\";\n\nconst isNonNegativeInteger = (value: unknown): value is number =>\n Number.isInteger(value) && (value as number) >= 0;\n\nconst isSchemaLikeValue = (value: unknown): boolean =>\n isSetObject(value) || isSetBoolean(value);\n\nconst isStringArray = (value: unknown): value is string[] =>\n Array.isArray(value) && value.every(item => isSetString(item));\n\nconst isRecordOfStringArrays = (\n value: unknown\n): value is Record<string, string[]> =>\n isSetObject(value) && Object.values(value).every(item => isStringArray(item));\n\nconst TYPE_KEYWORD_VALIDATORS: Record<\n JsonSchemaDataType,\n Record<string, SchemaKeywordValidator>\n> = {\n string: {\n minLength: isNonNegativeInteger,\n maxLength: isNonNegativeInteger,\n pattern: isSetString,\n format: isSetString,\n contentEncoding: isSetString,\n contentMediaType: isSetString\n },\n number: {\n minimum: isSetNumber,\n maximum: isSetNumber,\n exclusiveMinimum: isSetNumber,\n exclusiveMaximum: isSetNumber,\n multipleOf: isSetNumber\n },\n integer: {\n minimum: isSetNumber,\n maximum: isSetNumber,\n exclusiveMinimum: isSetNumber,\n exclusiveMaximum: isSetNumber,\n multipleOf: isSetNumber\n },\n boolean: {},\n array: {\n items: isSchemaLikeValue,\n prefixItems: Array.isArray,\n contains: isSchemaLikeValue,\n minItems: isNonNegativeInteger,\n maxItems: isNonNegativeInteger,\n uniqueItems: isSetBoolean,\n minContains: isNonNegativeInteger,\n maxContains: isNonNegativeInteger\n },\n object: {\n properties: isSetObject,\n patternProperties: isSetObject,\n additionalProperties: isSchemaLikeValue,\n required: isStringArray,\n minProperties: isNonNegativeInteger,\n maxProperties: isNonNegativeInteger,\n dependentRequired: isRecordOfStringArrays,\n dependentSchemas: isSetObject,\n propertyNames: isSchemaLikeValue,\n unevaluatedProperties: isSchemaLikeValue\n },\n null: {}\n};\n\n/**\n * Type guard for JSON Schema types.\n *\n * @remarks\n * This function checks if the input is a JSON Schema type, which is defined as having a `type` property or a `$ref` property. This is used to determine if a given input conforms to the structure of a JSON Schema definition that includes type information.\n *\n * @param input - The value to check.\n * @returns True if the input is a JSON Schema type, false otherwise.\n */\nexport function isJsonSchema<T = unknown>(\n input: unknown\n): input is JsonSchema<T> {\n if (!isSetObject(input)) {\n return false;\n }\n\n const schema = input as Record<string, unknown>;\n\n if (\"$ref\" in schema && isSetString(schema.$ref)) {\n return true;\n }\n\n if (!(\"type\" in schema)) {\n return false;\n }\n\n const schemaTypes: JsonSchemaDataType[] = [];\n\n if (isSetString(schema.type)) {\n if (!JSON_SCHEMA_DATA_TYPE_SET.has(schema.type as JsonSchemaDataType)) {\n return false;\n }\n\n schemaTypes.push(schema.type as JsonSchemaDataType);\n } else if (Array.isArray(schema.type) && schema.type.length > 0) {\n if (\n !schema.type.every(\n schemaType =>\n isSetString(schemaType) &&\n JSON_SCHEMA_DATA_TYPE_SET.has(schemaType as JsonSchemaDataType)\n ) ||\n new Set(schema.type).size !== schema.type.length\n ) {\n return false;\n }\n\n schemaTypes.push(...(schema.type as JsonSchemaDataType[]));\n } else {\n return false;\n }\n\n const typeKeywordValidators = schemaTypes.flatMap(schemaType =>\n Object.entries(TYPE_KEYWORD_VALIDATORS[schemaType])\n );\n\n for (const [keyword, validator] of typeKeywordValidators) {\n if (keyword in schema && !validator(schema[keyword])) {\n return false;\n }\n }\n\n if (\n \"minItems\" in schema &&\n \"maxItems\" in schema &&\n isNonNegativeInteger(schema.minItems) &&\n isNonNegativeInteger(schema.maxItems) &&\n schema.minItems > schema.maxItems\n ) {\n return false;\n }\n\n if (\n \"minLength\" in schema &&\n \"maxLength\" in schema &&\n isNonNegativeInteger(schema.minLength) &&\n isNonNegativeInteger(schema.maxLength) &&\n schema.minLength > schema.maxLength\n ) {\n return false;\n }\n\n if (\n \"minProperties\" in schema &&\n \"maxProperties\" in schema &&\n isNonNegativeInteger(schema.minProperties) &&\n isNonNegativeInteger(schema.maxProperties) &&\n schema.minProperties > schema.maxProperties\n ) {\n return false;\n }\n\n return true;\n}\n\n/**\n * Type guard for JSON Schema object forms.\n *\n * @remarks\n * This function checks if the input is a JSON Schema object type, which is defined as having a `type` property equal to \"object\" or having a `properties` object. This is used to determine if a given input conforms to the structure of a JSON Schema object definition.\n *\n * @param input - The value to check.\n * @returns True if the input is a JSON Schema object type, false otherwise.\n */\nexport function isJsonSchemaObject<\n T extends Record<string, any> = Record<string, any>\n>(input: unknown): input is JsonSchemaObject<T> {\n return (\n isJsonSchemaObjectType(input) &&\n (input.type === \"object\" || isObject(input.properties))\n );\n}\n\n/**\n * Type guard for JSON Schemas that only accept `null`.\n *\n * @remarks\n * This function checks if the input is a JSON Schema that exclusively accepts the `null` type. It verifies that the input is a valid JSON Schema and that its `type` property is set to \"null\". This is useful for identifying schemas that are specifically designed to allow only `null` values.\n *\n * @param input - The value to check.\n * @returns True if the input is a JSON Schema that only accepts `null`, false otherwise.\n */\nexport function isNullOnlyJsonSchema(\n input: unknown\n): input is JsonSchema<null> {\n return isJsonSchema(input) && input.type === \"null\";\n}\n\n/**\n * Type guard for untyped schema objects.\n *\n * @remarks\n * This function checks if the input is an untyped schema object, which is defined as having certain properties that are commonly found in untyped schema definitions. This includes properties such as `id`, `title`, `description`, `$schema`, `tsType`, `markdownType`, `type`, `required`, `tags`, `args`, `properties`, and `resolve`. The function verifies that these properties, if present, conform to the expected types (e.g., strings for certain properties, arrays for others). This type guard is used to determine if a given input can be treated as an untyped schema object within the context of the Powerlines schema system.\n *\n * @param input - The value to check.\n * @returns True if the input is an untyped schema object, false otherwise.\n */\nexport function isUntypedSchema(input: unknown): input is UntypedSchema {\n if (!isSetObject(input)) {\n return false;\n }\n\n const schema = input as Record<string, unknown>;\n if (\"id\" in schema && !isSetString(schema.id)) {\n return false;\n }\n if (\"title\" in schema && !isSetString(schema.title)) {\n return false;\n }\n if (\"description\" in schema && !isSetString(schema.description)) {\n return false;\n }\n if (\"$schema\" in schema && !isSetString(schema.$schema)) {\n return false;\n }\n if (\"tsType\" in schema && !isSetString(schema.tsType)) {\n return false;\n }\n if (\"markdownType\" in schema && !isSetString(schema.markdownType)) {\n return false;\n }\n if (\n \"type\" in schema &&\n !isSetString(schema.type) &&\n !Array.isArray(schema.type)\n ) {\n return false;\n }\n if (\"required\" in schema && !Array.isArray(schema.required)) {\n return false;\n }\n if (\"tags\" in schema && !Array.isArray(schema.tags)) {\n return false;\n }\n if (\"args\" in schema && !Array.isArray(schema.args)) {\n return false;\n }\n if (\"properties\" in schema && !isSetObject(schema.properties)) {\n return false;\n }\n if (\"resolve\" in schema && !isFunction(schema.resolve)) {\n return false;\n }\n\n return true;\n}\n\n/**\n * Type guard for untyped input objects.\n *\n * @remarks\n * This function checks if the input is an untyped input object, which is defined as having certain properties that are commonly found in untyped input definitions. This includes properties such as `$schema` and `$resolve`. The function verifies that these properties, if present, conform to the expected types (e.g., objects for `$schema`, functions for `$resolve`). This type guard is used to determine if a given input can be treated as an untyped input object within the context of the Powerlines schema system.\n *\n * @param input - The value to check.\n * @returns True if the input is an untyped input object, false otherwise.\n */\nexport function isUntypedInput(input: unknown): input is UntypedInputObject {\n if (!isSetObject(input)) {\n return false;\n }\n\n const inputObject = input as Record<string, unknown>;\n if (\"$schema\" in inputObject && !isUntypedSchema(inputObject.$schema)) {\n return false;\n }\n if (\"$resolve\" in inputObject && !isFunction(inputObject.$resolve)) {\n return false;\n }\n\n return true;\n}\n\n/**\n * Type guard for Powerlines Schema objects.\n *\n * @param input - The value to check.\n * @returns True if the input is a Powerlines Schema object, false otherwise.\n */\nexport function isSchema<T = unknown>(input: unknown): input is Schema<T> {\n return (\n isSetObject(input) &&\n \"schema\" in input &&\n isJsonSchema(input.schema) &&\n \"variant\" in input &&\n isSetString(input.variant) &&\n \"hash\" in input &&\n isSetString(input.hash)\n );\n}\n\nexport function isExtractedSchema<T = unknown>(\n input: unknown\n): input is ExtractedSchema<T> {\n return (\n isSchema<T>(input) &&\n \"source\" in input &&\n isSetObject(input.source) &&\n \"schema\" in input.source &&\n \"variant\" in input.source &&\n isSetString(input.source.variant)\n );\n}\n"],"mappings":";;;;;AAkCA,MAAM,4BAA4B,IAAI,IACpC,sBACF;AAIA,MAAM,eAA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,YAAY,MAAM,MAAM,KACxB,YAAY,MAAM,UAClB,aAAa,MAAM,UACnB,YAAY,MAAM,OAAO,OAAO;AAEpC"}
|
|
1
|
+
{"version":3,"file":"type-checks.mjs","names":[],"sources":["../src/type-checks.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isJsonSchemaObjectType } from \"@stryke/json\";\nimport {\n isFunction,\n isObject,\n isSetObject,\n isSetString\n} from \"@stryke/type-checks\";\nimport {\n InputObject as UntypedInputObject,\n Schema as UntypedSchema\n} from \"untyped\";\nimport { JSON_SCHEMA_DATA_TYPES } from \"./constants\";\nimport { ExtractedSchema, JsonSchema, JsonSchemaObject, Schema } from \"./types\";\n\ntype JsonSchemaDataType = (typeof JSON_SCHEMA_DATA_TYPES)[number];\n\nconst JSON_SCHEMA_DATA_TYPE_SET = new Set<JsonSchemaDataType>(\n JSON_SCHEMA_DATA_TYPES\n);\n\ntype SchemaKeywordValidator = (value: unknown) => boolean;\n\nconst isSetNumber = (value: unknown): value is number =>\n typeof value === \"number\" && Number.isFinite(value);\n\nconst isSetBoolean = (value: unknown): value is boolean =>\n typeof value === \"boolean\";\n\nconst isNonNegativeInteger = (value: unknown): value is number =>\n Number.isInteger(value) && (value as number) >= 0;\n\nconst isSchemaLikeValue = (value: unknown): boolean =>\n isSetObject(value) || isSetBoolean(value);\n\nconst isStringArray = (value: unknown): value is string[] =>\n Array.isArray(value) && value.every(item => isSetString(item));\n\nconst isRecordOfStringArrays = (\n value: unknown\n): value is Record<string, string[]> =>\n isSetObject(value) && Object.values(value).every(item => isStringArray(item));\n\nconst TYPE_KEYWORD_VALIDATORS: Record<\n JsonSchemaDataType,\n Record<string, SchemaKeywordValidator>\n> = {\n string: {\n minLength: isNonNegativeInteger,\n maxLength: isNonNegativeInteger,\n pattern: isSetString,\n format: isSetString,\n contentEncoding: isSetString,\n contentMediaType: isSetString\n },\n number: {\n minimum: isSetNumber,\n maximum: isSetNumber,\n exclusiveMinimum: isSetNumber,\n exclusiveMaximum: isSetNumber,\n multipleOf: isSetNumber\n },\n integer: {\n minimum: isSetNumber,\n maximum: isSetNumber,\n exclusiveMinimum: isSetNumber,\n exclusiveMaximum: isSetNumber,\n multipleOf: isSetNumber\n },\n boolean: {},\n array: {\n items: isSchemaLikeValue,\n prefixItems: Array.isArray,\n contains: isSchemaLikeValue,\n minItems: isNonNegativeInteger,\n maxItems: isNonNegativeInteger,\n uniqueItems: isSetBoolean,\n minContains: isNonNegativeInteger,\n maxContains: isNonNegativeInteger\n },\n object: {\n properties: isSetObject,\n patternProperties: isSetObject,\n additionalProperties: isSchemaLikeValue,\n required: isStringArray,\n minProperties: isNonNegativeInteger,\n maxProperties: isNonNegativeInteger,\n dependentRequired: isRecordOfStringArrays,\n dependentSchemas: isSetObject,\n propertyNames: isSchemaLikeValue,\n unevaluatedProperties: isSchemaLikeValue\n },\n null: {}\n};\n\n/**\n * Type guard for JSON Schema types.\n *\n * @remarks\n * This function checks if the input is a JSON Schema type, which is defined as having a `type` property or a `$ref` property. This is used to determine if a given input conforms to the structure of a JSON Schema definition that includes type information.\n *\n * @param input - The value to check.\n * @returns True if the input is a JSON Schema type, false otherwise.\n */\nexport function isJsonSchema<T = unknown>(\n input: unknown\n): input is JsonSchema<T> {\n if (!isSetObject(input)) {\n return false;\n }\n\n const schema = input as Record<string, unknown>;\n\n if (\"$ref\" in schema && isSetString(schema.$ref)) {\n return true;\n }\n\n if (!(\"type\" in schema)) {\n return false;\n }\n\n const schemaTypes: JsonSchemaDataType[] = [];\n\n if (isSetString(schema.type)) {\n if (!JSON_SCHEMA_DATA_TYPE_SET.has(schema.type as JsonSchemaDataType)) {\n return false;\n }\n\n schemaTypes.push(schema.type as JsonSchemaDataType);\n } else if (Array.isArray(schema.type) && schema.type.length > 0) {\n if (\n !schema.type.every(\n schemaType =>\n isSetString(schemaType) &&\n JSON_SCHEMA_DATA_TYPE_SET.has(schemaType as JsonSchemaDataType)\n ) ||\n new Set(schema.type).size !== schema.type.length\n ) {\n return false;\n }\n\n schemaTypes.push(...(schema.type as JsonSchemaDataType[]));\n } else {\n return false;\n }\n\n const typeKeywordValidators = schemaTypes.flatMap(schemaType =>\n Object.entries(TYPE_KEYWORD_VALIDATORS[schemaType])\n );\n\n for (const [keyword, validator] of typeKeywordValidators) {\n if (keyword in schema && !validator(schema[keyword])) {\n return false;\n }\n }\n\n if (\n \"minItems\" in schema &&\n \"maxItems\" in schema &&\n isNonNegativeInteger(schema.minItems) &&\n isNonNegativeInteger(schema.maxItems) &&\n schema.minItems > schema.maxItems\n ) {\n return false;\n }\n\n if (\n \"minLength\" in schema &&\n \"maxLength\" in schema &&\n isNonNegativeInteger(schema.minLength) &&\n isNonNegativeInteger(schema.maxLength) &&\n schema.minLength > schema.maxLength\n ) {\n return false;\n }\n\n if (\n \"minProperties\" in schema &&\n \"maxProperties\" in schema &&\n isNonNegativeInteger(schema.minProperties) &&\n isNonNegativeInteger(schema.maxProperties) &&\n schema.minProperties > schema.maxProperties\n ) {\n return false;\n }\n\n return true;\n}\n\n/**\n * Type guard for JSON Schema object forms.\n *\n * @remarks\n * This function checks if the input is a JSON Schema object type, which is defined as having a `type` property equal to \"object\" or having a `properties` object. This is used to determine if a given input conforms to the structure of a JSON Schema object definition.\n *\n * @param input - The value to check.\n * @returns True if the input is a JSON Schema object type, false otherwise.\n */\nexport function isJsonSchemaObject<\n T extends Record<string, any> = Record<string, any>\n>(input: unknown): input is JsonSchemaObject<T> {\n return (\n isJsonSchemaObjectType(input) &&\n (input.type === \"object\" || isObject(input.properties))\n );\n}\n\n/**\n * Type guard for JSON Schemas that only accept `null`.\n *\n * @remarks\n * This function checks if the input is a JSON Schema that exclusively accepts the `null` type. It verifies that the input is a valid JSON Schema and that its `type` property is set to \"null\". This is useful for identifying schemas that are specifically designed to allow only `null` values.\n *\n * @param input - The value to check.\n * @returns True if the input is a JSON Schema that only accepts `null`, false otherwise.\n */\nexport function isNullOnlyJsonSchema(\n input: unknown\n): input is JsonSchema<null> {\n return isJsonSchema<null>(input) && input.type === \"null\";\n}\n\n/**\n * Type guard for untyped schema objects.\n *\n * @remarks\n * This function checks if the input is an untyped schema object, which is defined as having certain properties that are commonly found in untyped schema definitions. This includes properties such as `id`, `title`, `description`, `$schema`, `tsType`, `markdownType`, `type`, `required`, `tags`, `args`, `properties`, and `resolve`. The function verifies that these properties, if present, conform to the expected types (e.g., strings for certain properties, arrays for others). This type guard is used to determine if a given input can be treated as an untyped schema object within the context of the Powerlines schema system.\n *\n * @param input - The value to check.\n * @returns True if the input is an untyped schema object, false otherwise.\n */\nexport function isUntypedSchema(input: unknown): input is UntypedSchema {\n if (!isSetObject(input)) {\n return false;\n }\n\n const schema = input as Record<string, unknown>;\n if (\"id\" in schema && !isSetString(schema.id)) {\n return false;\n }\n if (\"title\" in schema && !isSetString(schema.title)) {\n return false;\n }\n if (\"description\" in schema && !isSetString(schema.description)) {\n return false;\n }\n if (\"$schema\" in schema && !isSetString(schema.$schema)) {\n return false;\n }\n if (\"tsType\" in schema && !isSetString(schema.tsType)) {\n return false;\n }\n if (\"markdownType\" in schema && !isSetString(schema.markdownType)) {\n return false;\n }\n if (\n \"type\" in schema &&\n !isSetString(schema.type) &&\n !Array.isArray(schema.type)\n ) {\n return false;\n }\n if (\"required\" in schema && !Array.isArray(schema.required)) {\n return false;\n }\n if (\"tags\" in schema && !Array.isArray(schema.tags)) {\n return false;\n }\n if (\"args\" in schema && !Array.isArray(schema.args)) {\n return false;\n }\n if (\"properties\" in schema && !isSetObject(schema.properties)) {\n return false;\n }\n if (\"resolve\" in schema && !isFunction(schema.resolve)) {\n return false;\n }\n\n return true;\n}\n\n/**\n * Type guard for untyped input objects.\n *\n * @remarks\n * This function checks if the input is an untyped input object, which is defined as having certain properties that are commonly found in untyped input definitions. This includes properties such as `$schema` and `$resolve`. The function verifies that these properties, if present, conform to the expected types (e.g., objects for `$schema`, functions for `$resolve`). This type guard is used to determine if a given input can be treated as an untyped input object within the context of the Powerlines schema system.\n *\n * @param input - The value to check.\n * @returns True if the input is an untyped input object, false otherwise.\n */\nexport function isUntypedInput(input: unknown): input is UntypedInputObject {\n if (!isSetObject(input)) {\n return false;\n }\n\n const inputObject = input as Record<string, unknown>;\n if (\"$schema\" in inputObject && !isUntypedSchema(inputObject.$schema)) {\n return false;\n }\n if (\"$resolve\" in inputObject && !isFunction(inputObject.$resolve)) {\n return false;\n }\n\n return true;\n}\n\n/**\n * Type guard for Powerlines Schema objects.\n *\n * @param input - The value to check.\n * @returns True if the input is a Powerlines Schema object, false otherwise.\n */\nexport function isSchema<T = unknown>(input: unknown): input is Schema<T> {\n return (\n isSetObject(input) &&\n \"schema\" in input &&\n isJsonSchema(input.schema) &&\n \"variant\" in input &&\n isSetString(input.variant) &&\n \"hash\" in input &&\n isSetString(input.hash)\n );\n}\n\nexport function isExtractedSchema<T = unknown>(\n input: unknown\n): input is ExtractedSchema<T> {\n return (\n isSchema<T>(input) &&\n \"source\" in input &&\n isSetObject(input.source) &&\n \"schema\" in input.source &&\n \"variant\" in input.source &&\n isSetString(input.source.variant)\n );\n}\n"],"mappings":";;;;;AAkCA,MAAM,4BAA4B,IAAI,IACpC,sBACF;AAIA,MAAM,eAAe,UACnB,OAAO,UAAU,YAAY,OAAO,SAAS,KAAK;AAEpD,MAAM,gBAAgB,UACpB,OAAO,UAAU;AAEnB,MAAM,wBAAwB,UAC5B,OAAO,UAAU,KAAK,KAAM,SAAoB;AAElD,MAAM,qBAAqB,UACzB,YAAY,KAAK,KAAK,aAAa,KAAK;AAE1C,MAAM,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Known, ...Known[]] | Known[] | number | string | boolean | null;
|
|
58
|
-
type UncheckedPropertiesSchema<T> = { [K in keyof T]-?: (UncheckedJsonSchemaType<T[K], false> &
|
|
58
|
+
type UncheckedPropertiesSchema<T> = { [K in keyof T]-?: (UncheckedJsonSchemaType<T[K], false> & JsonSchemaNullable<T[K]>) | {
|
|
59
59
|
$ref: string;
|
|
60
60
|
} };
|
|
61
61
|
type UncheckedRequiredMembers<T> = { [K in keyof T]-?: undefined extends T[K] ? never : K }[keyof T];
|
|
62
|
-
type
|
|
62
|
+
type JsonSchemaNullable<T> = undefined extends T ? {
|
|
63
63
|
/** Indicates that the value may be null in addition to the declared type. */nullable: true; /** A constant value constrained to null when the schema is nullable. */
|
|
64
64
|
const?: null; /** Allowed values, including null when the schema is nullable. */
|
|
65
65
|
enum?: readonly (T | null)[]; /** The default value to use when none is provided. */
|
|
@@ -84,7 +84,7 @@ type UncheckedJsonSchemaType<T, IsPartial extends boolean> = ({
|
|
|
84
84
|
type: JsonType<"boolean", IsPartial>;
|
|
85
85
|
} : T extends readonly [any, ...any[]] ? {
|
|
86
86
|
type: JsonType<"array", IsPartial>; /** The tuple items in order. */
|
|
87
|
-
items: { readonly [K in keyof T]-?: UncheckedJsonSchemaType<T[K], false> &
|
|
87
|
+
items: { readonly [K in keyof T]-?: UncheckedJsonSchemaType<T[K], false> & JsonSchemaNullable<T[K]> } & {
|
|
88
88
|
length: T["length"];
|
|
89
89
|
}; /** The minimum number of items allowed in the tuple. */
|
|
90
90
|
minItems: T["length"];
|
|
@@ -178,7 +178,7 @@ interface SchemaMetadata {
|
|
|
178
178
|
*/
|
|
179
179
|
description?: string;
|
|
180
180
|
/**
|
|
181
|
-
* A URL
|
|
181
|
+
* A URL to external documentation for the schema, which can be used by documentation tools or other libraries that support this feature to provide additional information or resources related to the schema. The presence of this property does not affect the validation behavior of the schema itself, but it can provide additional context or information about the schema when used in conjunction with compatible tools.
|
|
182
182
|
*/
|
|
183
183
|
docs?: string;
|
|
184
184
|
/**
|
|
@@ -240,7 +240,7 @@ interface SchemaMetadata {
|
|
|
240
240
|
*/
|
|
241
241
|
contentSchema?: string;
|
|
242
242
|
}
|
|
243
|
-
type JsonSchemaProperty<T extends Record<string, any> = Record<string, any>, TName extends string = string> =
|
|
243
|
+
type JsonSchemaProperty<T extends Record<string, any> = Record<string, any>, TName extends keyof T & string = keyof T & string> = JsonSchema<T[TName]> & {
|
|
244
244
|
/** The property name within the parent object schema. */name: TName;
|
|
245
245
|
/**
|
|
246
246
|
* An indicator specifying if the field is nullable or not. If `true`, the field can accept `null` as a valid value. This property can be used by validation libraries or other tools that support this feature to provide additional validation rules for the data that the schema represents. The presence of this property does not affect the validation behavior of the schema itself, but it can provide additional context or information about the expected data when used in conjunction with compatible tools.
|
|
@@ -275,19 +275,19 @@ interface Schema<T = unknown> {
|
|
|
275
275
|
/** The normalized schema definition. */
|
|
276
276
|
schema: JsonSchema<T>;
|
|
277
277
|
}
|
|
278
|
-
interface BaseSchemaSource {
|
|
278
|
+
interface BaseSchemaSource<T = unknown> {
|
|
279
279
|
/** A stable content hash for the original source schema. */
|
|
280
280
|
hash: string;
|
|
281
281
|
/** The specific source format used for the schema input. */
|
|
282
282
|
variant: SchemaSourceVariant;
|
|
283
283
|
/** The original schema input captured before normalization. */
|
|
284
|
-
schema: SchemaSourceInput
|
|
284
|
+
schema: SchemaSourceInput<T>;
|
|
285
285
|
}
|
|
286
|
-
interface JsonSchemaSchemaSource<
|
|
286
|
+
interface JsonSchemaSchemaSource<T = unknown> extends BaseSchemaSource<T> {
|
|
287
287
|
/** Indicates the source input already uses JSON Schema syntax. */
|
|
288
288
|
variant: "json-schema";
|
|
289
289
|
/** The original JSON Schema document. */
|
|
290
|
-
schema: JsonSchema<
|
|
290
|
+
schema: JsonSchema<T>;
|
|
291
291
|
}
|
|
292
292
|
interface StandardSchemaSchemaSource extends BaseSchemaSource {
|
|
293
293
|
/** Indicates the source input follows the Standard Schema format. */
|
|
@@ -319,5 +319,5 @@ interface ExtractedSchema<T = unknown> extends Schema<T> {
|
|
|
319
319
|
source: SchemaSource;
|
|
320
320
|
}
|
|
321
321
|
//#endregion
|
|
322
|
-
export { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource };
|
|
322
|
+
export { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaNullable, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource };
|
|
323
323
|
//# sourceMappingURL=types.d.cts.map
|
package/dist/types.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.cts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;;KAyBY,kBAAA,GAAqB,WAAW;AAAA,KAChC,aAAA,GAAgB,QAAO;AADnC;;;AAAA,KAMY,uBAAA;AAAA,KASP,uBAAA,2GACmD,IAAA,KACpD,IAAI;AAAA,KAEH,mBAAA,OAA0B,CAAA,gBAAiB,CAAA,EAAG,CAAC,6BAClD,CAAA,sBAEE,CAAA;;;;KAMQ,cAAA,GAAiB,uBAAA,CAAwB,KAAA,UACnD,cAAA;AAAA,KAEG,sBAAA,MAA4B,OAAA,CAAQ,uBAAA,CAAwB,CAAA;AAAA,KAErD,QA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;EA1Y1D;EA4YR,OAAA;EA5Y6C;EA+Y7C,MAAA,EAAQ,oBAAA;AAAA;AAAA,UAGO,gBAAA,SAAyB,gBAAgB;EA9YxC;EAgZhB,OAAA;EA/YY;EAkZZ,MAAA,EAAQ,EAAA,CAAG,UAAA;AAAA;AAAA,UAGI,sBAAA,SAA+B,gBAAgB;EAlZxB;EAoZtC,OAAA;EAhZc;EAmZd,MAAA,EAAQ,IAAA;AAAA;AAAA,UAGO,mBAAA,SAA4B,gBAAA;EAlZjB;EAoZ1B,OAAA;EApZ2C;EAuZ3C,MAAA,EAAQ,kBAAA,GAAqB,aAAA;AAAA;AAAA,KAGnB,YAAA,GACR,sBAAA,GACA,0BAAA,GACA,gBAAA,GACA,mBAAA,GACA,sBAAA;AAAA,UAEa,eAAA,sBAAqC,MAAA,CAAO,CAAA;EA7ZpB;EA+ZvC,MAAA,EAAQ,YAAA;AAAA"}
|
package/dist/types.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { FormatName } from "ajv-formats/dist/formats.js";
|
|
2
1
|
import { Type } from "@powerlines/deepkit/vendor/type";
|
|
3
2
|
import { StandardJSONSchemaV1 } from "@standard-schema/spec";
|
|
4
3
|
import { TypeDefinition } from "@stryke/types/configuration";
|
|
4
|
+
import { FormatName } from "ajv-formats/dist/formats.js";
|
|
5
5
|
import { InputObject, Schema as Schema$1 } from "untyped";
|
|
6
6
|
import * as z3 from "zod/v3";
|
|
7
7
|
|
|
@@ -55,11 +55,11 @@ interface StringKeywords {
|
|
|
55
55
|
type Known = {
|
|
56
56
|
[key: string]: Known;
|
|
57
57
|
} | [Known, ...Known[]] | Known[] | number | string | boolean | null;
|
|
58
|
-
type UncheckedPropertiesSchema<T> = { [K in keyof T]-?: (UncheckedJsonSchemaType<T[K], false> &
|
|
58
|
+
type UncheckedPropertiesSchema<T> = { [K in keyof T]-?: (UncheckedJsonSchemaType<T[K], false> & JsonSchemaNullable<T[K]>) | {
|
|
59
59
|
$ref: string;
|
|
60
60
|
} };
|
|
61
61
|
type UncheckedRequiredMembers<T> = { [K in keyof T]-?: undefined extends T[K] ? never : K }[keyof T];
|
|
62
|
-
type
|
|
62
|
+
type JsonSchemaNullable<T> = undefined extends T ? {
|
|
63
63
|
/** Indicates that the value may be null in addition to the declared type. */nullable: true; /** A constant value constrained to null when the schema is nullable. */
|
|
64
64
|
const?: null; /** Allowed values, including null when the schema is nullable. */
|
|
65
65
|
enum?: readonly (T | null)[]; /** The default value to use when none is provided. */
|
|
@@ -84,7 +84,7 @@ type UncheckedJsonSchemaType<T, IsPartial extends boolean> = ({
|
|
|
84
84
|
type: JsonType<"boolean", IsPartial>;
|
|
85
85
|
} : T extends readonly [any, ...any[]] ? {
|
|
86
86
|
type: JsonType<"array", IsPartial>; /** The tuple items in order. */
|
|
87
|
-
items: { readonly [K in keyof T]-?: UncheckedJsonSchemaType<T[K], false> &
|
|
87
|
+
items: { readonly [K in keyof T]-?: UncheckedJsonSchemaType<T[K], false> & JsonSchemaNullable<T[K]> } & {
|
|
88
88
|
length: T["length"];
|
|
89
89
|
}; /** The minimum number of items allowed in the tuple. */
|
|
90
90
|
minItems: T["length"];
|
|
@@ -178,7 +178,7 @@ interface SchemaMetadata {
|
|
|
178
178
|
*/
|
|
179
179
|
description?: string;
|
|
180
180
|
/**
|
|
181
|
-
* A URL
|
|
181
|
+
* A URL to external documentation for the schema, which can be used by documentation tools or other libraries that support this feature to provide additional information or resources related to the schema. The presence of this property does not affect the validation behavior of the schema itself, but it can provide additional context or information about the schema when used in conjunction with compatible tools.
|
|
182
182
|
*/
|
|
183
183
|
docs?: string;
|
|
184
184
|
/**
|
|
@@ -240,7 +240,7 @@ interface SchemaMetadata {
|
|
|
240
240
|
*/
|
|
241
241
|
contentSchema?: string;
|
|
242
242
|
}
|
|
243
|
-
type JsonSchemaProperty<T extends Record<string, any> = Record<string, any>, TName extends string = string> =
|
|
243
|
+
type JsonSchemaProperty<T extends Record<string, any> = Record<string, any>, TName extends keyof T & string = keyof T & string> = JsonSchema<T[TName]> & {
|
|
244
244
|
/** The property name within the parent object schema. */name: TName;
|
|
245
245
|
/**
|
|
246
246
|
* An indicator specifying if the field is nullable or not. If `true`, the field can accept `null` as a valid value. This property can be used by validation libraries or other tools that support this feature to provide additional validation rules for the data that the schema represents. The presence of this property does not affect the validation behavior of the schema itself, but it can provide additional context or information about the expected data when used in conjunction with compatible tools.
|
|
@@ -275,19 +275,19 @@ interface Schema<T = unknown> {
|
|
|
275
275
|
/** The normalized schema definition. */
|
|
276
276
|
schema: JsonSchema<T>;
|
|
277
277
|
}
|
|
278
|
-
interface BaseSchemaSource {
|
|
278
|
+
interface BaseSchemaSource<T = unknown> {
|
|
279
279
|
/** A stable content hash for the original source schema. */
|
|
280
280
|
hash: string;
|
|
281
281
|
/** The specific source format used for the schema input. */
|
|
282
282
|
variant: SchemaSourceVariant;
|
|
283
283
|
/** The original schema input captured before normalization. */
|
|
284
|
-
schema: SchemaSourceInput
|
|
284
|
+
schema: SchemaSourceInput<T>;
|
|
285
285
|
}
|
|
286
|
-
interface JsonSchemaSchemaSource<
|
|
286
|
+
interface JsonSchemaSchemaSource<T = unknown> extends BaseSchemaSource<T> {
|
|
287
287
|
/** Indicates the source input already uses JSON Schema syntax. */
|
|
288
288
|
variant: "json-schema";
|
|
289
289
|
/** The original JSON Schema document. */
|
|
290
|
-
schema: JsonSchema<
|
|
290
|
+
schema: JsonSchema<T>;
|
|
291
291
|
}
|
|
292
292
|
interface StandardSchemaSchemaSource extends BaseSchemaSource {
|
|
293
293
|
/** Indicates the source input follows the Standard Schema format. */
|
|
@@ -319,5 +319,5 @@ interface ExtractedSchema<T = unknown> extends Schema<T> {
|
|
|
319
319
|
source: SchemaSource;
|
|
320
320
|
}
|
|
321
321
|
//#endregion
|
|
322
|
-
export { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource };
|
|
322
|
+
export { BaseSchemaSource, ExtractedSchema, JsonSchema, JsonSchemaLike, JsonSchemaNullable, JsonSchemaObject, JsonSchemaPrimitiveType, JsonSchemaProperty, JsonSchemaSchemaSource, JsonType, NumberFormat, NumberKeywords, ReflectionSchemaSource, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant, StandardSchemaSchemaSource, StringKeywords, TypeDefinitionReference, UntypedInputObject, UntypedSchema, UntypedSchemaSource, Zod3SchemaSource };
|
|
323
323
|
//# sourceMappingURL=types.d.mts.map
|
package/dist/types.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;;KAyBY,kBAAA,GAAqB,WAAW;AAAA,KAChC,aAAA,GAAgB,QAAO;AADnC;;;AAAA,KAMY,uBAAA;AAAA,KASP,uBAAA,2GACmD,IAAA,KACpD,IAAI;AAAA,KAEH,mBAAA,OAA0B,CAAA,gBAAiB,CAAA,EAAG,CAAC,6BAClD,CAAA,sBAEE,CAAA;;;;KAMQ,cAAA,GAAiB,uBAAA,CAAwB,KAAA,UACnD,cAAA;AAAA,KAEG,sBAAA,MAA4B,OAAA,CAAQ,uBAAA,CAAwB,CAAA;AAAA,KAErD,QAAA,gDAGR,SAAA,gBAAyB,CAAA,eAAgB,CAAA;AAAA,KAEjC,YAAA;AAAA,UAWK,cAAA;EA3CkB;EA6CjC,OAAA;EApC0B;EAuC1B,OAAA;EArCM;EAwCN,gBAAA;EA1CgD;EA6ChD,gBAAA;EA3CE;EA8CF,UAAA;EA9CM;EAiDN,MAAA,GAAS,YAAY;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EAoZtC,OAAA;EAhZc;EAmZd,MAAA,EAAQ,IAAA;AAAA;AAAA,UAGO,mBAAA,SAA4B,gBAAA;EAlZjB;EAoZ1B,OAAA;EApZ2C;EAuZ3C,MAAA,EAAQ,kBAAA,GAAqB,aAAA;AAAA;AAAA,KAGnB,YAAA,GACR,sBAAA,GACA,0BAAA,GACA,gBAAA,GACA,mBAAA,GACA,sBAAA;AAAA,UAEa,eAAA,sBAAqC,MAAA,CAAO,CAAA;EA7ZpB;EA+ZvC,MAAA,EAAQ,YAAA;AAAA"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
3
|
+
let ajv = require("ajv");
|
|
4
|
+
ajv = require_runtime.__toESM(ajv, 1);
|
|
5
|
+
let ajv_formats = require("ajv-formats");
|
|
6
|
+
ajv_formats = require_runtime.__toESM(ajv_formats, 1);
|
|
7
|
+
|
|
8
|
+
//#region src/validate.ts
|
|
9
|
+
/**
|
|
10
|
+
* Gets an Ajv validator instance for a given JSON Schema.
|
|
11
|
+
*
|
|
12
|
+
* @param schema - The JSON Schema to create a validator for.
|
|
13
|
+
* @returns An Ajv instance with the schema added.
|
|
14
|
+
*/
|
|
15
|
+
function getValidator(schema) {
|
|
16
|
+
const ajv$1 = new ajv.default({
|
|
17
|
+
schemas: [schema],
|
|
18
|
+
code: {
|
|
19
|
+
source: true,
|
|
20
|
+
esm: true
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
(0, ajv_formats.default)(ajv$1);
|
|
24
|
+
return ajv$1;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Gets a validation function for a given JSON Schema.
|
|
28
|
+
*
|
|
29
|
+
* @param schema - The JSON Schema to create a validation function for.
|
|
30
|
+
* @returns A function that validates data against the schema and returns a boolean indicating validity.
|
|
31
|
+
*/
|
|
32
|
+
function getValidatorFunction(schema) {
|
|
33
|
+
return getValidator(schema).compile(schema);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
//#endregion
|
|
37
|
+
exports.getValidator = getValidator;
|
|
38
|
+
exports.getValidatorFunction = getValidatorFunction;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { JsonSchema } from "./types.cjs";
|
|
2
|
+
import Ajv, { ValidateFunction } from "ajv";
|
|
3
|
+
|
|
4
|
+
//#region src/validate.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Gets an Ajv validator instance for a given JSON Schema.
|
|
7
|
+
*
|
|
8
|
+
* @param schema - The JSON Schema to create a validator for.
|
|
9
|
+
* @returns An Ajv instance with the schema added.
|
|
10
|
+
*/
|
|
11
|
+
declare function getValidator<T = unknown>(schema: JsonSchema<T>): Ajv;
|
|
12
|
+
/**
|
|
13
|
+
* Gets a validation function for a given JSON Schema.
|
|
14
|
+
*
|
|
15
|
+
* @param schema - The JSON Schema to create a validation function for.
|
|
16
|
+
* @returns A function that validates data against the schema and returns a boolean indicating validity.
|
|
17
|
+
*/
|
|
18
|
+
declare function getValidatorFunction<T = unknown>(schema: JsonSchema<T>): ValidateFunction<T>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { getValidator, getValidatorFunction };
|
|
21
|
+
//# sourceMappingURL=validate.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.d.cts","names":[],"sources":["../src/validate.ts"],"mappings":";;;;;;AA4BA;;;;iBAAgB,YAAA,aAAA,CAA0B,MAAA,EAAQ,UAAA,CAAW,CAAA,IAAK,GAAA;;;;;;;iBAiBlD,oBAAA,aAAA,CACd,MAAA,EAAQ,UAAA,CAAW,CAAA,IAClB,gBAAA,CAAiB,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { JsonSchema } from "./types.mjs";
|
|
2
|
+
import Ajv, { ValidateFunction } from "ajv";
|
|
3
|
+
|
|
4
|
+
//#region src/validate.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Gets an Ajv validator instance for a given JSON Schema.
|
|
7
|
+
*
|
|
8
|
+
* @param schema - The JSON Schema to create a validator for.
|
|
9
|
+
* @returns An Ajv instance with the schema added.
|
|
10
|
+
*/
|
|
11
|
+
declare function getValidator<T = unknown>(schema: JsonSchema<T>): Ajv;
|
|
12
|
+
/**
|
|
13
|
+
* Gets a validation function for a given JSON Schema.
|
|
14
|
+
*
|
|
15
|
+
* @param schema - The JSON Schema to create a validation function for.
|
|
16
|
+
* @returns A function that validates data against the schema and returns a boolean indicating validity.
|
|
17
|
+
*/
|
|
18
|
+
declare function getValidatorFunction<T = unknown>(schema: JsonSchema<T>): ValidateFunction<T>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { getValidator, getValidatorFunction };
|
|
21
|
+
//# sourceMappingURL=validate.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.d.mts","names":[],"sources":["../src/validate.ts"],"mappings":";;;;;;AA4BA;;;;iBAAgB,YAAA,aAAA,CAA0B,MAAA,EAAQ,UAAA,CAAW,CAAA,IAAK,GAAA;;;;;;;iBAiBlD,oBAAA,aAAA,CACd,MAAA,EAAQ,UAAA,CAAW,CAAA,IAClB,gBAAA,CAAiB,CAAA"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import Ajv from "ajv";
|
|
2
|
+
import addFormats from "ajv-formats";
|
|
3
|
+
|
|
4
|
+
//#region src/validate.ts
|
|
5
|
+
/**
|
|
6
|
+
* Gets an Ajv validator instance for a given JSON Schema.
|
|
7
|
+
*
|
|
8
|
+
* @param schema - The JSON Schema to create a validator for.
|
|
9
|
+
* @returns An Ajv instance with the schema added.
|
|
10
|
+
*/
|
|
11
|
+
function getValidator(schema) {
|
|
12
|
+
const ajv = new Ajv({
|
|
13
|
+
schemas: [schema],
|
|
14
|
+
code: {
|
|
15
|
+
source: true,
|
|
16
|
+
esm: true
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
addFormats(ajv);
|
|
20
|
+
return ajv;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Gets a validation function for a given JSON Schema.
|
|
24
|
+
*
|
|
25
|
+
* @param schema - The JSON Schema to create a validation function for.
|
|
26
|
+
* @returns A function that validates data against the schema and returns a boolean indicating validity.
|
|
27
|
+
*/
|
|
28
|
+
function getValidatorFunction(schema) {
|
|
29
|
+
return getValidator(schema).compile(schema);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
export { getValidator, getValidatorFunction };
|
|
34
|
+
//# sourceMappingURL=validate.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.mjs","names":[],"sources":["../src/validate.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport Ajv, { ValidateFunction } from \"ajv\";\nimport addFormats from \"ajv-formats\";\nimport { JsonSchema } from \"./types\";\n\n/**\n * Gets an Ajv validator instance for a given JSON Schema.\n *\n * @param schema - The JSON Schema to create a validator for.\n * @returns An Ajv instance with the schema added.\n */\nexport function getValidator<T = unknown>(schema: JsonSchema<T>): Ajv {\n const ajv = new Ajv({\n schemas: [schema],\n code: { source: true, esm: true }\n });\n\n addFormats(ajv);\n\n return ajv;\n}\n\n/**\n * Gets a validation function for a given JSON Schema.\n *\n * @param schema - The JSON Schema to create a validation function for.\n * @returns A function that validates data against the schema and returns a boolean indicating validity.\n */\nexport function getValidatorFunction<T = unknown>(\n schema: JsonSchema<T>\n): ValidateFunction<T> {\n const ajv = getValidator(schema);\n\n return ajv.compile<T>(schema);\n}\n"],"mappings":";;;;;;;;;;AA4BA,SAAgB,aAA0B,QAA4B;CACpE,MAAM,MAAM,IAAI,IAAI;EAClB,SAAS,CAAC,MAAM;EAChB,MAAM;GAAE,QAAQ;GAAM,KAAK;EAAK;CAClC,CAAC;CAED,WAAW,GAAG;CAEd,OAAO;AACT;;;;;;;AAQA,SAAgB,qBACd,QACqB;CAGrB,OAFY,aAAa,MAEhB,EAAE,QAAW,MAAM;AAC9B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powerlines/schema",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.41",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "A package containing a Powerlines plugin to assist in developing other Powerlines plugins.",
|
|
6
6
|
"keywords": [
|
|
@@ -91,6 +91,10 @@
|
|
|
91
91
|
"require": "./dist/type-checks.cjs"
|
|
92
92
|
},
|
|
93
93
|
"./types": { "import": "./dist/types.mjs", "require": "./dist/types.cjs" },
|
|
94
|
+
"./validate": {
|
|
95
|
+
"import": "./dist/validate.mjs",
|
|
96
|
+
"require": "./dist/validate.cjs"
|
|
97
|
+
},
|
|
94
98
|
"./package.json": "./package.json"
|
|
95
99
|
},
|
|
96
100
|
"main": "./dist/index.cjs",
|
|
@@ -121,5 +125,5 @@
|
|
|
121
125
|
"peerDependencies": { "zod": "^3.25.0 || ^4.0.0" },
|
|
122
126
|
"peerDependenciesMeta": { "zod": { "optional": true } },
|
|
123
127
|
"publishConfig": { "access": "public" },
|
|
124
|
-
"gitHead": "
|
|
128
|
+
"gitHead": "3b2fa8b99824b2931e215e3794ea4365b928034d"
|
|
125
129
|
}
|