@powerlines/schema 0.11.1 → 0.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -22
- package/dist/bundle.d.cts +2 -2
- package/dist/bundle.d.cts.map +1 -1
- package/dist/bundle.d.mts +2 -2
- package/dist/bundle.d.mts.map +1 -1
- package/dist/bundle.mjs.map +1 -1
- package/dist/codegen.cjs +27 -0
- package/dist/codegen.d.cts +15 -0
- package/dist/codegen.d.cts.map +1 -0
- package/dist/codegen.d.mts +15 -0
- package/dist/codegen.d.mts.map +1 -0
- package/dist/codegen.mjs +24 -0
- package/dist/codegen.mjs.map +1 -0
- package/dist/extract.cjs +201 -52
- package/dist/extract.d.cts +81 -16
- package/dist/extract.d.cts.map +1 -1
- package/dist/extract.d.mts +81 -16
- package/dist/extract.d.mts.map +1 -1
- package/dist/extract.mjs +199 -55
- package/dist/extract.mjs.map +1 -1
- package/dist/index.cjs +15 -3
- package/dist/index.d.cts +7 -5
- package/dist/index.d.mts +7 -5
- package/dist/index.mjs +6 -4
- package/dist/jtd.cjs +385 -0
- package/dist/jtd.d.cts +15 -0
- package/dist/jtd.d.cts.map +1 -0
- package/dist/jtd.d.mts +15 -0
- package/dist/jtd.d.mts.map +1 -0
- package/dist/jtd.mjs +384 -0
- package/dist/jtd.mjs.map +1 -0
- package/dist/reflection.cjs +321 -100
- package/dist/reflection.d.cts +16 -13
- package/dist/reflection.d.cts.map +1 -1
- package/dist/reflection.d.mts +16 -13
- package/dist/reflection.d.mts.map +1 -1
- package/dist/reflection.mjs +323 -101
- package/dist/reflection.mjs.map +1 -1
- package/dist/resolve.d.cts +5 -5
- package/dist/resolve.d.cts.map +1 -1
- package/dist/resolve.d.mts +5 -5
- package/dist/resolve.d.mts.map +1 -1
- package/dist/resolve.mjs.map +1 -1
- package/dist/type-checks.cjs +76 -0
- package/dist/type-checks.d.cts +43 -0
- package/dist/type-checks.d.cts.map +1 -0
- package/dist/type-checks.d.mts +43 -0
- package/dist/type-checks.d.mts.map +1 -0
- package/dist/type-checks.mjs +71 -0
- package/dist/type-checks.mjs.map +1 -0
- package/dist/types.d.cts +166 -24
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +166 -24
- package/dist/types.d.mts.map +1 -1
- package/package.json +25 -19
- package/dist/is-schema-definition.cjs +0 -18
- package/dist/is-schema-definition.d.cts +0 -13
- package/dist/is-schema-definition.d.cts.map +0 -1
- package/dist/is-schema-definition.d.mts +0 -13
- package/dist/is-schema-definition.d.mts.map +0 -1
- package/dist/is-schema-definition.mjs +0 -17
- package/dist/is-schema-definition.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ This package is part of the <b>🔌 Powerlines</b> monorepo. Powerlines is the "
|
|
|
32
32
|
<!-- prettier-ignore-start -->
|
|
33
33
|
<!-- markdownlint-disable -->
|
|
34
34
|
|
|
35
|
-
> [!IMPORTANT]
|
|
35
|
+
> [!IMPORTANT]
|
|
36
36
|
> This repository, and the apps, libraries, and tools contained within, is still in it's initial development phase. As a result, bugs and issues are expected with it's usage. When the main development phase completes, a proper release will be performed, the packages will be available through NPM (and other distributions), and this message will be removed. However, in the meantime, please feel free to report any issues you may come across.
|
|
37
37
|
|
|
38
38
|
<!-- markdownlint-restore -->
|
|
@@ -49,28 +49,42 @@ This package is part of the <b>🔌 Powerlines</b> monorepo. Powerlines is the "
|
|
|
49
49
|
|
|
50
50
|
<!-- END header -->
|
|
51
51
|
|
|
52
|
-
# Powerlines -
|
|
52
|
+
# Powerlines - Schema Package
|
|
53
53
|
|
|
54
|
-
A package
|
|
54
|
+
A package for extracting and resolving schemas from various input formats, including Zod schemas, JSON Schemas, JTD schemas, untyped schemas, and TypeScript type reflections. This package provides a unified interface for working with different schema formats and extracting relevant information for use in other parts of the Powerlines framework.
|
|
55
|
+
|
|
56
|
+
The input can be one of the following formats:
|
|
57
|
+
|
|
58
|
+
- A string that references a Typescript module's export (can be in the format `module#export` or just `module`, in which case it will attempt to resolve the default export)
|
|
59
|
+
- A [Zod v3 schema](https://github.com/colinhacks/zod)
|
|
60
|
+
- Any type that adheres to [the Standard JSON Schema specification](https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec)
|
|
61
|
+
- A [JSON Schema object](https://json-schema.org/)
|
|
62
|
+
- A [JTD schema object](https://ajv.js.org/json-type-definition.html)
|
|
63
|
+
- An [Untyped schema](https://github.com/unjs/untyped)
|
|
64
|
+
- A [TypeScript type reflection](https://deepkit.io/en/documentation/runtime-types/reflection)
|
|
65
|
+
|
|
66
|
+
By allowing for multiple input formats, this package provides flexibility and ease of use for developers working with different schema definitions. It abstracts away the complexities of handling various schema formats and provides a consistent interface for extracting and working with schemas across the Powerlines framework.
|
|
55
67
|
|
|
56
68
|
<!-- START doctoc -->
|
|
57
69
|
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
58
70
|
|
|
59
71
|
## Table of Contents
|
|
60
72
|
|
|
61
|
-
- [
|
|
62
|
-
- [
|
|
63
|
-
- [
|
|
64
|
-
- [
|
|
65
|
-
- [
|
|
66
|
-
|
|
67
|
-
- [
|
|
68
|
-
- [
|
|
69
|
-
- [
|
|
70
|
-
- [
|
|
71
|
-
- [
|
|
72
|
-
- [
|
|
73
|
-
- [
|
|
73
|
+
- [Powerlines - Schema Package](#powerlines---schema-package)
|
|
74
|
+
- [Table of Contents](#table-of-contents)
|
|
75
|
+
- [Installing](#installing)
|
|
76
|
+
- [Reduced Package Size](#reduced-package-size)
|
|
77
|
+
- [Development](#development)
|
|
78
|
+
- [Building](#building)
|
|
79
|
+
- [Running unit tests](#running-unit-tests)
|
|
80
|
+
- [Linting](#linting)
|
|
81
|
+
- [Storm Workspaces](#storm-workspaces)
|
|
82
|
+
- [Roadmap](#roadmap)
|
|
83
|
+
- [Support](#support)
|
|
84
|
+
- [License](#license)
|
|
85
|
+
- [Changelog](#changelog)
|
|
86
|
+
- [Contributing](#contributing)
|
|
87
|
+
- [Contributors](#contributors)
|
|
74
88
|
|
|
75
89
|
<!-- END doctoc -->
|
|
76
90
|
|
|
@@ -79,14 +93,14 @@ A package containing the Deepkit plugin for Powerlines, various helper utilities
|
|
|
79
93
|
Using [pnpm](http://pnpm.io):
|
|
80
94
|
|
|
81
95
|
```bash
|
|
82
|
-
pnpm add @powerlines/
|
|
96
|
+
pnpm add @powerlines/schema
|
|
83
97
|
```
|
|
84
98
|
|
|
85
99
|
<details>
|
|
86
100
|
<summary>Using npm</summary>
|
|
87
101
|
|
|
88
102
|
```bash
|
|
89
|
-
npm install @powerlines/
|
|
103
|
+
npm install @powerlines/schema
|
|
90
104
|
```
|
|
91
105
|
|
|
92
106
|
</details>
|
|
@@ -95,7 +109,7 @@ npm install @powerlines/plugin-deepkit
|
|
|
95
109
|
<summary>Using yarn</summary>
|
|
96
110
|
|
|
97
111
|
```bash
|
|
98
|
-
yarn add @powerlines/
|
|
112
|
+
yarn add @powerlines/schema
|
|
99
113
|
```
|
|
100
114
|
|
|
101
115
|
</details>
|
|
@@ -114,16 +128,16 @@ commands are available to assist in development.
|
|
|
114
128
|
|
|
115
129
|
### Building
|
|
116
130
|
|
|
117
|
-
Run `nx build
|
|
131
|
+
Run `nx build schema` to build the library.
|
|
118
132
|
|
|
119
133
|
### Running unit tests
|
|
120
134
|
|
|
121
|
-
Run `nx test
|
|
135
|
+
Run `nx test schema` to execute the unit tests via
|
|
122
136
|
[Jest](https://jestjs.io).
|
|
123
137
|
|
|
124
138
|
### Linting
|
|
125
139
|
|
|
126
|
-
Run `nx lint
|
|
140
|
+
Run `nx lint schema` to run
|
|
127
141
|
[ESLint](https://eslint.org/) on the package.
|
|
128
142
|
|
|
129
143
|
<!-- START footer -->
|
package/dist/bundle.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ResolveOptions, UnresolvedContext } from "@powerlines/core";
|
|
2
2
|
import { DeepPartial } from "@stryke/types/base";
|
|
3
3
|
import { BuildOptions, OutputFile } from "esbuild";
|
|
4
4
|
|
|
@@ -15,7 +15,7 @@ type BundleOptions = DeepPartial<BuildOptions> & {
|
|
|
15
15
|
* @param options - Optional overrides for the ESBuild configuration.
|
|
16
16
|
* @returns A promise that resolves to the bundled module.
|
|
17
17
|
*/
|
|
18
|
-
declare function bundle<TContext extends
|
|
18
|
+
declare function bundle<TContext extends UnresolvedContext>(context: TContext, file: string, options?: BundleOptions): Promise<OutputFile>;
|
|
19
19
|
//#endregion
|
|
20
20
|
export { BundleOptions, bundle };
|
|
21
21
|
//# sourceMappingURL=bundle.d.cts.map
|
package/dist/bundle.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bundle.d.cts","names":[],"sources":["../src/bundle.ts"],"mappings":";;;;;KAiCY,aAAA,GAAgB,WAAA,CAAY,YAAA;EACtC,IAAA;EACA,OAAA,GAAU,WAAA,CAAY,cAAA;AAAA;;;;;;;;;iBAWF,MAAA,kBAAwB,
|
|
1
|
+
{"version":3,"file":"bundle.d.cts","names":[],"sources":["../src/bundle.ts"],"mappings":";;;;;KAiCY,aAAA,GAAgB,WAAA,CAAY,YAAA;EACtC,IAAA;EACA,OAAA,GAAU,WAAA,CAAY,cAAA;AAAA;;;;;;;;;iBAWF,MAAA,kBAAwB,iBAAA,CAAA,CAC5C,OAAA,EAAS,QAAA,EACT,IAAA,UACA,OAAA,GAAS,aAAA,GACR,OAAA,CAAQ,UAAA"}
|
package/dist/bundle.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BuildOptions, OutputFile } from "esbuild";
|
|
2
|
-
import {
|
|
2
|
+
import { ResolveOptions, UnresolvedContext } from "@powerlines/core";
|
|
3
3
|
import { DeepPartial } from "@stryke/types/base";
|
|
4
4
|
|
|
5
5
|
//#region src/bundle.d.ts
|
|
@@ -15,7 +15,7 @@ type BundleOptions = DeepPartial<BuildOptions> & {
|
|
|
15
15
|
* @param options - Optional overrides for the ESBuild configuration.
|
|
16
16
|
* @returns A promise that resolves to the bundled module.
|
|
17
17
|
*/
|
|
18
|
-
declare function bundle<TContext extends
|
|
18
|
+
declare function bundle<TContext extends UnresolvedContext>(context: TContext, file: string, options?: BundleOptions): Promise<OutputFile>;
|
|
19
19
|
//#endregion
|
|
20
20
|
export { BundleOptions, bundle };
|
|
21
21
|
//# sourceMappingURL=bundle.d.mts.map
|
package/dist/bundle.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bundle.d.mts","names":[],"sources":["../src/bundle.ts"],"mappings":";;;;;KAiCY,aAAA,GAAgB,WAAA,CAAY,YAAA;EACtC,IAAA;EACA,OAAA,GAAU,WAAA,CAAY,cAAA;AAAA;;;;;;;;;iBAWF,MAAA,kBAAwB,
|
|
1
|
+
{"version":3,"file":"bundle.d.mts","names":[],"sources":["../src/bundle.ts"],"mappings":";;;;;KAiCY,aAAA,GAAgB,WAAA,CAAY,YAAA;EACtC,IAAA;EACA,OAAA,GAAU,WAAA,CAAY,cAAA;AAAA;;;;;;;;;iBAWF,MAAA,kBAAwB,iBAAA,CAAA,CAC5C,OAAA,EAAS,QAAA,EACT,IAAA,UACA,OAAA,GAAS,aAAA,GACR,OAAA,CAAQ,UAAA"}
|
package/dist/bundle.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bundle.mjs","names":[],"sources":["../src/bundle.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 {\n CreateUnpluginResolverOptions,\n
|
|
1
|
+
{"version":3,"file":"bundle.mjs","names":[],"sources":["../src/bundle.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 {\n CreateUnpluginResolverOptions,\n ResolveOptions,\n UnresolvedContext\n} from \"@powerlines/core\";\nimport { createUnpluginResolver } from \"@powerlines/core/lib/unplugin\";\nimport { resolveOptions } from \"@powerlines/unplugin/esbuild\";\nimport { omit } from \"@stryke/helpers/omit\";\nimport { findFileName } from \"@stryke/path/file-path-fns\";\nimport { DeepPartial } from \"@stryke/types/base\";\nimport defu from \"defu\";\nimport type { BuildOptions } from \"esbuild\";\nimport { build, OutputFile } from \"esbuild\";\nimport { createEsbuildPlugin } from \"unplugin\";\n\nexport type BundleOptions = DeepPartial<BuildOptions> & {\n name?: string;\n resolve?: DeepPartial<ResolveOptions>;\n};\n\n/**\n * Bundle a type definition to a module.\n *\n * @param context - The context object containing the environment paths.\n * @param file - The file path to bundle.\n * @param options - Optional overrides for the ESBuild configuration.\n * @returns A promise that resolves to the bundled module.\n */\nexport async function bundle<TContext extends UnresolvedContext>(\n context: TContext,\n file: string,\n options: BundleOptions = {}\n): Promise<OutputFile> {\n const path = await context.fs.resolve(file);\n if (!path || !context.fs.existsSync(path)) {\n throw new Error(\n `Module not found: \"${file}\". Please check the path and try again.`\n );\n }\n\n const result = await build(\n defu(\n {\n entryPoints: [path],\n write: false,\n sourcemap: false,\n splitting: false,\n treeShaking: true,\n bundle: true,\n packages: \"bundle\",\n platform: \"node\",\n logLevel: \"silent\",\n ...omit(options, [\"name\", \"resolve\"])\n },\n resolveOptions(context),\n {\n plugins: [\n createEsbuildPlugin(\n createUnpluginResolver(context, {\n name: options.name ?? `${findFileName(file)} Bundler`,\n prefix: false,\n overrides: defu(\n options.resolve ?? {},\n { skipNodeModulesBundle: false },\n context.config.resolve\n ) as CreateUnpluginResolverOptions[\"overrides\"],\n silenceHookLogging: true\n })\n )()\n ]\n }\n )\n );\n if (result.errors.length > 0) {\n throw new Error(\n `Failed to bundle ${file}: ${result.errors\n .map(error => error.text)\n .join(\", \")}`\n );\n }\n if (result.warnings.length > 0) {\n context.warn(\n `Warnings while bundling ${file}: ${result.warnings\n .map(warning => warning.text)\n .join(\", \")}`\n );\n }\n if (!result.outputFiles || result.outputFiles.filter(Boolean).length === 0) {\n throw new Error(\n `No output files generated for ${\n file\n }. Please check the configuration and try again.`\n );\n }\n\n return result.outputFiles.filter(Boolean)[0]!;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA8CA,eAAsB,OACpB,SACA,MACA,UAAyB,EAAE,EACN;CACrB,MAAM,OAAO,MAAM,QAAQ,GAAG,QAAQ,KAAK;AAC3C,KAAI,CAAC,QAAQ,CAAC,QAAQ,GAAG,WAAW,KAAK,CACvC,OAAM,IAAI,MACR,sBAAsB,KAAK,yCAC5B;CAGH,MAAM,SAAS,MAAM,MACnB,KACE;EACE,aAAa,CAAC,KAAK;EACnB,OAAO;EACP,WAAW;EACX,WAAW;EACX,aAAa;EACb,QAAQ;EACR,UAAU;EACV,UAAU;EACV,UAAU;EACV,GAAG,KAAK,SAAS,CAAC,QAAQ,UAAU,CAAC;EACtC,EACD,eAAe,QAAQ,EACvB,EACE,SAAS,CACP,oBACE,uBAAuB,SAAS;EAC9B,MAAM,QAAQ,QAAQ,GAAG,aAAa,KAAK,CAAC;EAC5C,QAAQ;EACR,WAAW,KACT,QAAQ,WAAW,EAAE,EACrB,EAAE,uBAAuB,OAAO,EAChC,QAAQ,OAAO,QAChB;EACD,oBAAoB;EACrB,CAAC,CACH,EAAE,CACJ,EACF,CACF,CACF;AACD,KAAI,OAAO,OAAO,SAAS,EACzB,OAAM,IAAI,MACR,oBAAoB,KAAK,IAAI,OAAO,OACjC,KAAI,UAAS,MAAM,KAAK,CACxB,KAAK,KAAK,GACd;AAEH,KAAI,OAAO,SAAS,SAAS,EAC3B,SAAQ,KACN,2BAA2B,KAAK,IAAI,OAAO,SACxC,KAAI,YAAW,QAAQ,KAAK,CAC5B,KAAK,KAAK,GACd;AAEH,KAAI,CAAC,OAAO,eAAe,OAAO,YAAY,OAAO,QAAQ,CAAC,WAAW,EACvE,OAAM,IAAI,MACR,iCACE,KACD,iDACF;AAGH,QAAO,OAAO,YAAY,OAAO,QAAQ,CAAC"}
|
package/dist/codegen.cjs
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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_dist_standalone_index_js = require("ajv/dist/standalone/index.js");
|
|
6
|
+
ajv_dist_standalone_index_js = require_runtime.__toESM(ajv_dist_standalone_index_js, 1);
|
|
7
|
+
|
|
8
|
+
//#region src/codegen.ts
|
|
9
|
+
/**
|
|
10
|
+
* Generates standalone validation code for the provided JSON schemas using the Ajv library. This function takes an array of JSON schemas and an optional set of references or functions, and returns a string containing the generated validation code. The generated code can be used to validate data against the provided schemas without requiring the Ajv library at runtime, making it suitable for use in environments where minimizing dependencies is important.
|
|
11
|
+
*
|
|
12
|
+
* @param schemas - An array of JSON schemas to generate validation code for. Each schema should be a valid JSON schema object that defines the structure and constraints of the data to be validated.
|
|
13
|
+
* @param refsOrFuncts - An optional parameter that can be either an object containing schema references or a function that returns such an object. This parameter allows you to provide additional schemas that may be referenced by the main schemas, or to define custom functions that can be used in the generated validation code. If not provided, the function will generate code based solely on the provided schemas.
|
|
14
|
+
* @returns A promise that resolves to a string containing the generated standalone validation code.
|
|
15
|
+
*/
|
|
16
|
+
async function generateCode(schemas, refsOrFuncts) {
|
|
17
|
+
return (0, ajv_dist_standalone_index_js.default)(new ajv.default({
|
|
18
|
+
schemas,
|
|
19
|
+
code: {
|
|
20
|
+
source: true,
|
|
21
|
+
esm: true
|
|
22
|
+
}
|
|
23
|
+
}), refsOrFuncts);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
exports.generateCode = generateCode;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Options } from "ajv";
|
|
2
|
+
import standaloneCode from "ajv/dist/standalone/index.js";
|
|
3
|
+
|
|
4
|
+
//#region src/codegen.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Generates standalone validation code for the provided JSON schemas using the Ajv library. This function takes an array of JSON schemas and an optional set of references or functions, and returns a string containing the generated validation code. The generated code can be used to validate data against the provided schemas without requiring the Ajv library at runtime, making it suitable for use in environments where minimizing dependencies is important.
|
|
7
|
+
*
|
|
8
|
+
* @param schemas - An array of JSON schemas to generate validation code for. Each schema should be a valid JSON schema object that defines the structure and constraints of the data to be validated.
|
|
9
|
+
* @param refsOrFuncts - An optional parameter that can be either an object containing schema references or a function that returns such an object. This parameter allows you to provide additional schemas that may be referenced by the main schemas, or to define custom functions that can be used in the generated validation code. If not provided, the function will generate code based solely on the provided schemas.
|
|
10
|
+
* @returns A promise that resolves to a string containing the generated standalone validation code.
|
|
11
|
+
*/
|
|
12
|
+
declare function generateCode(schemas: Options["schemas"], refsOrFuncts?: Parameters<typeof standaloneCode>[1]): Promise<string>;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { generateCode };
|
|
15
|
+
//# sourceMappingURL=codegen.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codegen.d.cts","names":[],"sources":["../src/codegen.ts"],"mappings":";;;;;;AA6BA;;;;;iBAAsB,YAAA,CACpB,OAAA,EAAS,OAAA,aACT,YAAA,GAAe,UAAA,QAAkB,cAAA,OAAkB,OAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Options } from "ajv";
|
|
2
|
+
import standaloneCode from "ajv/dist/standalone/index.js";
|
|
3
|
+
|
|
4
|
+
//#region src/codegen.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Generates standalone validation code for the provided JSON schemas using the Ajv library. This function takes an array of JSON schemas and an optional set of references or functions, and returns a string containing the generated validation code. The generated code can be used to validate data against the provided schemas without requiring the Ajv library at runtime, making it suitable for use in environments where minimizing dependencies is important.
|
|
7
|
+
*
|
|
8
|
+
* @param schemas - An array of JSON schemas to generate validation code for. Each schema should be a valid JSON schema object that defines the structure and constraints of the data to be validated.
|
|
9
|
+
* @param refsOrFuncts - An optional parameter that can be either an object containing schema references or a function that returns such an object. This parameter allows you to provide additional schemas that may be referenced by the main schemas, or to define custom functions that can be used in the generated validation code. If not provided, the function will generate code based solely on the provided schemas.
|
|
10
|
+
* @returns A promise that resolves to a string containing the generated standalone validation code.
|
|
11
|
+
*/
|
|
12
|
+
declare function generateCode(schemas: Options["schemas"], refsOrFuncts?: Parameters<typeof standaloneCode>[1]): Promise<string>;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { generateCode };
|
|
15
|
+
//# sourceMappingURL=codegen.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codegen.d.mts","names":[],"sources":["../src/codegen.ts"],"mappings":";;;;;;AA6BA;;;;;iBAAsB,YAAA,CACpB,OAAA,EAAS,OAAA,aACT,YAAA,GAAe,UAAA,QAAkB,cAAA,OAAkB,OAAA"}
|
package/dist/codegen.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import Ajv from "ajv";
|
|
2
|
+
import standaloneCode from "ajv/dist/standalone/index.js";
|
|
3
|
+
|
|
4
|
+
//#region src/codegen.ts
|
|
5
|
+
/**
|
|
6
|
+
* Generates standalone validation code for the provided JSON schemas using the Ajv library. This function takes an array of JSON schemas and an optional set of references or functions, and returns a string containing the generated validation code. The generated code can be used to validate data against the provided schemas without requiring the Ajv library at runtime, making it suitable for use in environments where minimizing dependencies is important.
|
|
7
|
+
*
|
|
8
|
+
* @param schemas - An array of JSON schemas to generate validation code for. Each schema should be a valid JSON schema object that defines the structure and constraints of the data to be validated.
|
|
9
|
+
* @param refsOrFuncts - An optional parameter that can be either an object containing schema references or a function that returns such an object. This parameter allows you to provide additional schemas that may be referenced by the main schemas, or to define custom functions that can be used in the generated validation code. If not provided, the function will generate code based solely on the provided schemas.
|
|
10
|
+
* @returns A promise that resolves to a string containing the generated standalone validation code.
|
|
11
|
+
*/
|
|
12
|
+
async function generateCode(schemas, refsOrFuncts) {
|
|
13
|
+
return standaloneCode(new Ajv({
|
|
14
|
+
schemas,
|
|
15
|
+
code: {
|
|
16
|
+
source: true,
|
|
17
|
+
esm: true
|
|
18
|
+
}
|
|
19
|
+
}), refsOrFuncts);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
export { generateCode };
|
|
24
|
+
//# sourceMappingURL=codegen.mjs.map
|
|
@@ -0,0 +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 type { Options } from \"ajv\";\nimport Ajv from \"ajv\";\nimport standaloneCode from \"ajv/dist/standalone\";\n\n/**\n * Generates standalone validation code for the provided JSON schemas using the Ajv library. This function takes an array of JSON schemas and an optional set of references or functions, and returns a string containing the generated validation code. The generated code can be used to validate data against the provided schemas without requiring the Ajv library at runtime, making it suitable for use in environments where minimizing dependencies is important.\n *\n * @param schemas - An array of JSON schemas to generate validation code for. Each schema should be a valid JSON schema object that defines the structure and constraints of the data to be validated.\n * @param refsOrFuncts - An optional parameter that can be either an object containing schema references or a function that returns such an object. This parameter allows you to provide additional schemas that may be referenced by the main schemas, or to define custom functions that can be used in the generated validation code. If not provided, the function will generate code based solely on the provided schemas.\n * @returns A promise that resolves to a string containing the generated standalone validation code.\n */\nexport async function generateCode(\n schemas: Options[\"schemas\"],\n refsOrFuncts?: Parameters<typeof standaloneCode>[1]\n) {\n return standaloneCode(\n new Ajv({\n schemas,\n code: { source: true, esm: true }\n }),\n refsOrFuncts\n );\n}\n"],"mappings":";;;;;;;;;;;AA6BA,eAAsB,aACpB,SACA,cACA;AACA,QAAO,eACL,IAAI,IAAI;EACN;EACA,MAAM;GAAE,QAAQ;GAAM,KAAK;GAAM;EAClC,CAAC,EACF,aACD"}
|
package/dist/extract.cjs
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
3
|
-
const
|
|
3
|
+
const require_jtd = require('./jtd.cjs');
|
|
4
4
|
const require_reflection = require('./reflection.cjs');
|
|
5
5
|
const require_resolve = require('./resolve.cjs');
|
|
6
|
+
const require_type_checks = require('./type-checks.cjs');
|
|
6
7
|
let defu = require("defu");
|
|
7
8
|
defu = require_runtime.__toESM(defu, 1);
|
|
8
9
|
let _powerlines_core = require("@powerlines/core");
|
|
9
10
|
let _powerlines_deepkit_esbuild_plugin = require("@powerlines/deepkit/esbuild-plugin");
|
|
10
11
|
let _powerlines_deepkit_vendor_type = require("@powerlines/deepkit/vendor/type");
|
|
12
|
+
let _stryke_hash = require("@stryke/hash");
|
|
11
13
|
let _stryke_json = require("@stryke/json");
|
|
12
14
|
let _stryke_type_checks = require("@stryke/type-checks");
|
|
13
15
|
let _stryke_type_checks_is_set_object = require("@stryke/type-checks/is-set-object");
|
|
@@ -15,6 +17,34 @@ let _stryke_zod = require("@stryke/zod");
|
|
|
15
17
|
|
|
16
18
|
//#region src/extract.ts
|
|
17
19
|
/**
|
|
20
|
+
* Creates a hash string for a given schema definition input. The function checks the type of the input and generates a hash based on its content. If the input is a Zod schema, it hashes the JSON representation of its internal definition. If the input is a Standard JSON Schema, it hashes the JSON representation of its internal standard schema. If the input is already a JSON Schema object, it hashes its JSON representation directly. If the input is a reflected Deepkit Type object, it hashes its JSON representation. The resulting hash string can be used for caching or comparison purposes.
|
|
21
|
+
*/
|
|
22
|
+
function extractHash(variant, input) {
|
|
23
|
+
if ((0, _stryke_type_checks.isSetString)(input)) return (0, _stryke_hash.murmurhash)({
|
|
24
|
+
variant,
|
|
25
|
+
input
|
|
26
|
+
});
|
|
27
|
+
else if ((0, _stryke_type_checks_is_set_object.isSetObject)(input)) {
|
|
28
|
+
if ((0, _stryke_zod.isZod3Type)(input)) return (0, _stryke_hash.murmurhash)({
|
|
29
|
+
variant,
|
|
30
|
+
input: input._def
|
|
31
|
+
});
|
|
32
|
+
else if ((0, _stryke_json.isStandardJsonSchema)(input)) return (0, _stryke_hash.murmurhash)({
|
|
33
|
+
variant,
|
|
34
|
+
input: input["~standard"]
|
|
35
|
+
});
|
|
36
|
+
else if ((0, _stryke_json.isJsonSchemaObjectType)(input)) return (0, _stryke_hash.murmurhash)({
|
|
37
|
+
variant,
|
|
38
|
+
input
|
|
39
|
+
});
|
|
40
|
+
else if ((0, _powerlines_deepkit_vendor_type.isType)(input)) return (0, _stryke_hash.murmurhash)({
|
|
41
|
+
variant,
|
|
42
|
+
input: (0, _powerlines_deepkit_vendor_type.stringifyType)(input)
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
throw new Error(`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.`);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
18
48
|
* Converts a reflected Deepkit {@link @powerlines/deepkit/vendor/type#Type} into a JSON Schema (draft-07) representation.
|
|
19
49
|
*
|
|
20
50
|
* @remarks
|
|
@@ -28,89 +58,208 @@ function extractReflection(reflection) {
|
|
|
28
58
|
return require_reflection.reflectionToJsonSchema(reflection);
|
|
29
59
|
}
|
|
30
60
|
/**
|
|
31
|
-
* Extracts a JSON
|
|
61
|
+
* Extracts a JSON Type Definition (RFC 8927) schema from a given schema definition, if possible.
|
|
32
62
|
*
|
|
33
63
|
* @remarks
|
|
34
|
-
* This function checks if the provided
|
|
64
|
+
* This function checks if the provided input is a Zod schema, a Standard JSON Schema, or already a JSON Schema object, extracts a JSON Schema fragment via the appropriate adapter (Zod and Standard Schema produce draft-07 fragments), and then converts that fragment into a valid JTD form suitable for AJV's JTD validator.
|
|
35
65
|
*
|
|
36
|
-
* @param schema - The schema definition to extract
|
|
37
|
-
* @returns The extracted
|
|
66
|
+
* @param schema - The schema definition to extract from. This can be a Zod schema, a Standard JSON Schema, or a JSON Schema object.
|
|
67
|
+
* @returns The extracted JTD schema if successful, otherwise undefined.
|
|
38
68
|
*/
|
|
39
69
|
function extractJsonSchema(schema) {
|
|
40
|
-
if ((0, _stryke_type_checks_is_set_object.isSetObject)(schema) && ((0, _stryke_zod.isZod3Type)(schema) || (0, _stryke_json.isStandardJsonSchema)(schema) || (0, _stryke_json.
|
|
70
|
+
if ((0, _stryke_type_checks_is_set_object.isSetObject)(schema) && ((0, _stryke_zod.isZod3Type)(schema) || (0, _stryke_json.isStandardJsonSchema)(schema) || (0, _stryke_json.isJsonSchemaObjectType)(schema) || require_type_checks.isUntypedInput(schema) || require_type_checks.isUntypedSchema(schema))) {
|
|
41
71
|
let jsonSchema;
|
|
42
|
-
if ((0, _stryke_zod.isZod3Type)(schema)) jsonSchema = (0, _stryke_zod.
|
|
43
|
-
else if ((0, _stryke_json.isStandardJsonSchema)(schema)) jsonSchema = schema["~standard"].jsonSchema.input({ target: "draft-
|
|
72
|
+
if ((0, _stryke_zod.isZod3Type)(schema)) jsonSchema = (0, _stryke_zod.extractJsonSchema)(schema);
|
|
73
|
+
else if ((0, _stryke_json.isStandardJsonSchema)(schema)) jsonSchema = schema["~standard"].jsonSchema.input({ target: "draft-2020-12" });
|
|
74
|
+
else if (require_type_checks.isUntypedInput(schema)) jsonSchema = schema.$schema;
|
|
44
75
|
else jsonSchema = schema;
|
|
45
|
-
|
|
76
|
+
const jtd = require_jtd.jsonSchemaToJtd(jsonSchema);
|
|
77
|
+
if (jtd) return jtd;
|
|
46
78
|
}
|
|
47
79
|
}
|
|
48
80
|
/**
|
|
49
|
-
* Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `
|
|
81
|
+
* Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
|
|
50
82
|
*
|
|
51
83
|
* @param input - The input object to extract the schema definition from.
|
|
52
|
-
* @returns A `
|
|
84
|
+
* @returns A `Schema` containing the extracted schema and its variant if successful.
|
|
53
85
|
* @throws An error if the input does not contain a valid schema definition.
|
|
54
86
|
*/
|
|
55
|
-
function
|
|
87
|
+
function extractResolvedVariant(input) {
|
|
56
88
|
if ((0, _stryke_type_checks_is_set_object.isSetObject)(input)) {
|
|
57
|
-
if ((0, _stryke_zod.isZod3Type)(input))
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
};
|
|
64
|
-
} else if ((0, _stryke_json.isStandardJsonSchema)(input)) {
|
|
65
|
-
const schema = extractJsonSchema(input);
|
|
66
|
-
if (schema) return {
|
|
67
|
-
schema,
|
|
68
|
-
variant: "standard-schema",
|
|
69
|
-
input
|
|
70
|
-
};
|
|
71
|
-
} else if ((0, _stryke_json.isJsonSchema7ObjectType)(input)) {
|
|
72
|
-
const schema = extractJsonSchema(input);
|
|
73
|
-
if (schema) return {
|
|
74
|
-
schema,
|
|
75
|
-
variant: "json-schema",
|
|
76
|
-
input
|
|
77
|
-
};
|
|
78
|
-
} else if ((0, _powerlines_deepkit_vendor_type.isType)(input)) {
|
|
79
|
-
const schema = extractReflection(input);
|
|
80
|
-
if (schema) return {
|
|
81
|
-
schema,
|
|
82
|
-
variant: "reflection",
|
|
83
|
-
input
|
|
84
|
-
};
|
|
85
|
-
}
|
|
89
|
+
if ((0, _stryke_zod.isZod3Type)(input)) return "zod3";
|
|
90
|
+
else if ((0, _stryke_json.isStandardJsonSchema)(input)) return "standard-schema";
|
|
91
|
+
else if (require_type_checks.isJTDSchema(input)) return "jtd-schema";
|
|
92
|
+
else if ((0, _stryke_json.isJsonSchemaObjectType)(input)) return "json-schema";
|
|
93
|
+
else if ((0, _powerlines_deepkit_vendor_type.isType)(input)) return "reflection";
|
|
94
|
+
else if (require_type_checks.isUntypedInput(input) || require_type_checks.isUntypedSchema(input)) return "untyped";
|
|
86
95
|
}
|
|
87
|
-
throw new Error(`Failed to
|
|
96
|
+
throw new Error(`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.`);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
|
|
100
|
+
*
|
|
101
|
+
* @param input - The input object to extract the schema definition from.
|
|
102
|
+
* @returns A `Schema` containing the extracted schema and its variant if successful.
|
|
103
|
+
* @throws An error if the input does not contain a valid schema definition.
|
|
104
|
+
*/
|
|
105
|
+
function extractVariant(input) {
|
|
106
|
+
if ((0, _stryke_type_checks.isSetString)(input) || (0, _powerlines_core.isTypeDefinition)(input)) return "type-definition";
|
|
107
|
+
return extractResolvedVariant(input);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Extracts a JSON Schema object from a given schema definition input. The input can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding JSON Schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns the extracted JSON Schema if successful; otherwise, it throws an error.
|
|
111
|
+
*
|
|
112
|
+
* @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.
|
|
113
|
+
* @param variant - The variant of the schema definition to extract.
|
|
114
|
+
* @returns The extracted JSON Schema if successful.
|
|
115
|
+
* @throws An error if the input does not contain a valid schema definition.
|
|
116
|
+
*/
|
|
117
|
+
async function extractSchemaSchema(input, variant) {
|
|
118
|
+
if (require_type_checks.isExtractedSchema(input)) return input.schema;
|
|
119
|
+
const resolvedVariant = variant ?? extractResolvedVariant(input);
|
|
120
|
+
let schema;
|
|
121
|
+
if (resolvedVariant === "zod3" || resolvedVariant === "json-schema" || resolvedVariant === "standard-schema" || resolvedVariant === "untyped") {
|
|
122
|
+
const jsonSchema = extractJsonSchema(input);
|
|
123
|
+
if (jsonSchema) schema = require_jtd.jsonSchemaToJtd(jsonSchema);
|
|
124
|
+
} else if (resolvedVariant === "reflection") schema = extractReflection(input);
|
|
125
|
+
else if (resolvedVariant === "jtd-schema") schema = input;
|
|
126
|
+
if (schema) return schema;
|
|
127
|
+
throw new Error(`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, a JTD schema, an untyped schema, or a reflected Deepkit Type object.`);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
|
|
131
|
+
*
|
|
132
|
+
* @param variant - The variant of the schema definition to extract.
|
|
133
|
+
* @param input - The input object to extract the schema definition from.
|
|
134
|
+
* @returns A `Schema` containing the extracted schema and its variant if successful.
|
|
135
|
+
* @throws An error if the input does not contain a valid schema definition.
|
|
136
|
+
*/
|
|
137
|
+
function extractSource(variant, input) {
|
|
138
|
+
if (variant === "zod3") return {
|
|
139
|
+
hash: extractHash(variant, input),
|
|
140
|
+
variant: "zod3",
|
|
141
|
+
schema: input
|
|
142
|
+
};
|
|
143
|
+
else if (variant === "untyped") return {
|
|
144
|
+
hash: extractHash(variant, input),
|
|
145
|
+
variant: "untyped",
|
|
146
|
+
schema: input
|
|
147
|
+
};
|
|
148
|
+
else if (variant === "standard-schema") return {
|
|
149
|
+
hash: extractHash(variant, input),
|
|
150
|
+
variant: "standard-schema",
|
|
151
|
+
schema: input
|
|
152
|
+
};
|
|
153
|
+
else if (variant === "json-schema") return {
|
|
154
|
+
hash: extractHash(variant, input),
|
|
155
|
+
variant: "json-schema",
|
|
156
|
+
schema: input
|
|
157
|
+
};
|
|
158
|
+
else if (variant === "reflection") return {
|
|
159
|
+
hash: extractHash(variant, input),
|
|
160
|
+
variant: "reflection",
|
|
161
|
+
schema: input
|
|
162
|
+
};
|
|
163
|
+
else if (variant === "jtd-schema") return {
|
|
164
|
+
hash: extractHash(variant, input),
|
|
165
|
+
variant: "jtd-schema",
|
|
166
|
+
schema: input
|
|
167
|
+
};
|
|
168
|
+
throw new Error(`Failed to extract source information from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object.`);
|
|
88
169
|
}
|
|
89
170
|
/**
|
|
90
171
|
* Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.
|
|
91
172
|
*
|
|
92
173
|
* @example
|
|
93
174
|
* ```ts
|
|
94
|
-
*
|
|
175
|
+
* // Resolve a schema definition from a file path
|
|
176
|
+
* const schema1 = await extractSchema(context, "./schemas.ts#MySchema");
|
|
177
|
+
* // Resolve a schema definition from a JSON Schema object
|
|
178
|
+
* const schema2 = await extractSchema(context, schemaObject);
|
|
179
|
+
* // Resolve a schema definition from a Zod schema
|
|
180
|
+
* const schema3 = await extractSchema(context, zodSchema);
|
|
181
|
+
* // Resolve a schema definition from a reflected Deepkit Type object
|
|
182
|
+
* const schema4 = await extractSchema(context, reflectionType);
|
|
95
183
|
* ```
|
|
96
184
|
*
|
|
97
185
|
* @param context - The plugin context used for resolving the schema definition input.
|
|
98
186
|
* @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.
|
|
99
187
|
* @param options - Optional overrides for the ESBuild configuration used during resolution.
|
|
100
|
-
* @returns A promise that resolves to a
|
|
188
|
+
* @returns A promise that resolves to a {@link ExtractedSchema} containing the extracted JSON Schema and its variant, or the bytecode if JSON Schema extraction is not possible.
|
|
189
|
+
*/
|
|
190
|
+
async function extractSchema(context, input, options = {}) {
|
|
191
|
+
if (require_type_checks.isExtractedSchema(input)) return input;
|
|
192
|
+
if (require_type_checks.isSchema(input)) return {
|
|
193
|
+
...input,
|
|
194
|
+
source: {
|
|
195
|
+
hash: extractHash("jtd-schema", input.schema),
|
|
196
|
+
variant: "jtd-schema",
|
|
197
|
+
schema: input.schema
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
let source;
|
|
201
|
+
const variant = extractVariant(input);
|
|
202
|
+
if (variant === "type-definition") {
|
|
203
|
+
const resolved = await require_resolve.resolve(context, input, (0, defu.default)(options, { plugins: [(0, _powerlines_deepkit_esbuild_plugin.esbuildPlugin)(context, {
|
|
204
|
+
reflection: "default",
|
|
205
|
+
level: "all"
|
|
206
|
+
})] }));
|
|
207
|
+
source = extractSource(extractResolvedVariant(resolved), resolved);
|
|
208
|
+
} else if ([
|
|
209
|
+
"json-schema",
|
|
210
|
+
"jtd-schema",
|
|
211
|
+
"standard-schema",
|
|
212
|
+
"zod3",
|
|
213
|
+
"untyped",
|
|
214
|
+
"reflection"
|
|
215
|
+
].includes(variant)) source = extractSource(variant, input);
|
|
216
|
+
else throw new Error(`Invalid schema definition input "${variant}". The variant must be one of "type-definition", "json-schema", "jtd-schema", "standard-schema", "zod3", "untyped", or "reflection".`);
|
|
217
|
+
return {
|
|
218
|
+
variant,
|
|
219
|
+
source,
|
|
220
|
+
schema: await extractSchemaSchema(source.schema, source.variant),
|
|
221
|
+
hash: extractHash(variant, input)
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* ```ts
|
|
229
|
+
* // Resolve a schema definition from a file path
|
|
230
|
+
* const schema1 = await extract(context, "./schemas.ts#MySchema");
|
|
231
|
+
* // Resolve a schema definition from a JSON Schema object
|
|
232
|
+
* const schema2 = await extract(context, schemaObject);
|
|
233
|
+
* // Resolve a schema definition from a Zod schema
|
|
234
|
+
* const schema3 = await extract(context, zodSchema);
|
|
235
|
+
* // Resolve a schema definition from a reflected Deepkit Type object
|
|
236
|
+
* const schema4 = await extract(context, reflectionType);
|
|
237
|
+
* ```
|
|
238
|
+
*
|
|
239
|
+
* @see https://github.com/colinhacks/zod
|
|
240
|
+
* @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec
|
|
241
|
+
* @see https://json-schema.org/
|
|
242
|
+
* @see https://ajv.js.org/json-type-definition.html
|
|
243
|
+
* @see https://deepkit.io/en/documentation/runtime-types/reflection
|
|
244
|
+
* @see https://github.com/unjs/untyped
|
|
245
|
+
* @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
|
|
246
|
+
*
|
|
247
|
+
* @param context - The {@link Context | context} used for resolving the {@link Schema | schema} definition input.
|
|
248
|
+
* @param input - The input object or string to extract the {@link Schema | schema} from. This can be {@link TypeDefinitionReference | a string that references a Typescript module}, a [Zod v3 schema](https://github.com/colinhacks/zod), any type that adheres to [the Standard JSON Schema specification](https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec), a [JSON Schema object](https://json-schema.org/), a [JTD schema object](https://ajv.js.org/json-type-definition.html), an [untyped schema](https://github.com/unjs/untyped), or a [TypeScript type reflection](https://deepkit.io/en/documentation/runtime-types/reflection).
|
|
249
|
+
* @param options - Optional overrides for the [ESBuild configuration](https://esbuild.github.io/api/#general-options) used during resolution.
|
|
250
|
+
* @returns A promise that resolves to a {@link Schema | schema} object parsed from the input.
|
|
101
251
|
*/
|
|
102
252
|
async function extract(context, input, options = {}) {
|
|
103
|
-
|
|
104
|
-
let inputObject = input;
|
|
105
|
-
if ((0, _stryke_type_checks.isSetString)(input) || (0, _powerlines_core.isTypeDefinition)(input)) inputObject = await require_resolve.resolve(context, input, (0, defu.default)(options, { plugins: [(0, _powerlines_deepkit_esbuild_plugin.esbuildPlugin)(context, {
|
|
106
|
-
reflection: "default",
|
|
107
|
-
level: "all"
|
|
108
|
-
})] }));
|
|
109
|
-
return extractSchema(inputObject);
|
|
253
|
+
return await extractSchema(context, input, options);
|
|
110
254
|
}
|
|
111
255
|
|
|
112
256
|
//#endregion
|
|
113
257
|
exports.extract = extract;
|
|
258
|
+
exports.extractHash = extractHash;
|
|
114
259
|
exports.extractJsonSchema = extractJsonSchema;
|
|
115
260
|
exports.extractReflection = extractReflection;
|
|
116
|
-
exports.
|
|
261
|
+
exports.extractResolvedVariant = extractResolvedVariant;
|
|
262
|
+
exports.extractSchema = extractSchema;
|
|
263
|
+
exports.extractSchemaSchema = extractSchemaSchema;
|
|
264
|
+
exports.extractSource = extractSource;
|
|
265
|
+
exports.extractVariant = extractVariant;
|