@powerlines/schema 0.11.81 → 0.11.82
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/_virtual/_rolldown/runtime.cjs +29 -0
- package/dist/bundle.cjs +49 -0
- package/dist/bundle.d.cts +21 -0
- package/dist/bundle.d.cts.map +1 -0
- package/dist/bundle.d.mts +21 -0
- package/dist/bundle.d.mts.map +1 -0
- package/dist/bundle.mjs +48 -0
- package/dist/bundle.mjs.map +1 -0
- package/dist/codegen.cjs +312 -0
- package/dist/codegen.d.cts +67 -0
- package/dist/codegen.d.cts.map +1 -0
- package/dist/codegen.d.mts +67 -0
- package/dist/codegen.d.mts.map +1 -0
- package/dist/codegen.mjs +306 -0
- package/dist/codegen.mjs.map +1 -0
- package/dist/constants.cjs +61 -0
- package/dist/constants.d.cts +17 -0
- package/dist/constants.d.cts.map +1 -0
- package/dist/constants.d.mts +17 -0
- package/dist/constants.d.mts.map +1 -0
- package/dist/constants.mjs +57 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/extract.cjs +482 -0
- package/dist/extract.d.cts +125 -0
- package/dist/extract.d.cts.map +1 -0
- package/dist/extract.d.mts +125 -0
- package/dist/extract.d.mts.map +1 -0
- package/dist/extract.mjs +472 -0
- package/dist/extract.mjs.map +1 -0
- package/dist/helpers.cjs +241 -0
- package/dist/helpers.d.cts +121 -0
- package/dist/helpers.d.cts.map +1 -0
- package/dist/helpers.d.mts +121 -0
- package/dist/helpers.d.mts.map +1 -0
- package/dist/helpers.mjs +234 -0
- package/dist/helpers.mjs.map +1 -0
- package/dist/index.cjs +95 -2434
- package/dist/index.d.cts +11 -1892
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +11 -1892
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +12 -2321
- package/dist/metadata.cjs +53 -0
- package/dist/metadata.d.cts +31 -0
- package/dist/metadata.d.cts.map +1 -0
- package/dist/metadata.d.mts +31 -0
- package/dist/metadata.d.mts.map +1 -0
- package/dist/metadata.mjs +52 -0
- package/dist/metadata.mjs.map +1 -0
- package/dist/persistence.cjs +74 -0
- package/dist/persistence.d.cts +47 -0
- package/dist/persistence.d.cts.map +1 -0
- package/dist/persistence.d.mts +47 -0
- package/dist/persistence.d.mts.map +1 -0
- package/dist/persistence.mjs +71 -0
- package/dist/persistence.mjs.map +1 -0
- package/dist/reflection.cjs +426 -0
- package/dist/reflection.d.cts +11 -0
- package/dist/reflection.d.cts.map +1 -0
- package/dist/reflection.d.mts +11 -0
- package/dist/reflection.d.mts.map +1 -0
- package/dist/reflection.mjs +425 -0
- package/dist/reflection.mjs.map +1 -0
- package/dist/resolve.cjs +105 -0
- package/dist/resolve.d.cts +36 -0
- package/dist/resolve.d.cts.map +1 -0
- package/dist/resolve.d.mts +36 -0
- package/dist/resolve.d.mts.map +1 -0
- package/dist/resolve.mjs +102 -0
- package/dist/resolve.mjs.map +1 -0
- package/dist/type-checks.cjs +633 -0
- package/dist/type-checks.d.cts +316 -0
- package/dist/type-checks.d.cts.map +1 -0
- package/dist/type-checks.d.mts +316 -0
- package/dist/type-checks.d.mts.map +1 -0
- package/dist/type-checks.mjs +594 -0
- package/dist/type-checks.mjs.map +1 -0
- package/dist/types.d.cts +1152 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +1152 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/validate.cjs +27 -0
- package/dist/validate.mjs +25 -0
- package/dist/validate.mjs.map +1 -0
- package/package.json +6 -6
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
exports.__toESM = __toESM;
|
package/dist/bundle.cjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
2
|
+
let _powerlines_core_lib_unplugin = require("@powerlines/core/lib/unplugin");
|
|
3
|
+
let _powerlines_unplugin_esbuild = require("@powerlines/unplugin/esbuild");
|
|
4
|
+
let _stryke_helpers_omit = require("@stryke/helpers/omit");
|
|
5
|
+
let _stryke_path_file_path_fns = require("@stryke/path/file-path-fns");
|
|
6
|
+
let defu = require("defu");
|
|
7
|
+
defu = require_runtime.__toESM(defu, 1);
|
|
8
|
+
let esbuild = require("esbuild");
|
|
9
|
+
let unplugin = require("unplugin");
|
|
10
|
+
|
|
11
|
+
//#region src/bundle.ts
|
|
12
|
+
/**
|
|
13
|
+
* Bundle a type definition to a module.
|
|
14
|
+
*
|
|
15
|
+
* @param context - The context object containing the environment paths.
|
|
16
|
+
* @param file - The file path to bundle.
|
|
17
|
+
* @param options - Optional overrides for the ESBuild configuration.
|
|
18
|
+
* @returns A promise that resolves to the bundled module.
|
|
19
|
+
*/
|
|
20
|
+
async function bundle(context, file, options = {}) {
|
|
21
|
+
const path = await context.fs.resolve(file);
|
|
22
|
+
if (!path || !context.fs.existsSync(path)) throw new Error(`Module not found: "${file}". Please check the path and try again.`);
|
|
23
|
+
const result = await (0, esbuild.build)((0, defu.default)({
|
|
24
|
+
platform: "node",
|
|
25
|
+
...(0, _stryke_helpers_omit.omit)(options, ["name", "resolve"]),
|
|
26
|
+
logLevel: "silent",
|
|
27
|
+
entryPoints: [path],
|
|
28
|
+
write: false,
|
|
29
|
+
sourcemap: false,
|
|
30
|
+
splitting: false,
|
|
31
|
+
treeShaking: false,
|
|
32
|
+
bundle: true,
|
|
33
|
+
packages: "bundle",
|
|
34
|
+
keepNames: true,
|
|
35
|
+
metafile: false
|
|
36
|
+
}, (0, _powerlines_unplugin_esbuild.resolveOptions)(context), { plugins: [(0, unplugin.createEsbuildPlugin)((0, _powerlines_core_lib_unplugin.createUnpluginResolver)(context, {
|
|
37
|
+
name: options.name ?? `${(0, _stryke_path_file_path_fns.findFileName)(file)} Bundler`,
|
|
38
|
+
prefix: false,
|
|
39
|
+
overrides: (0, defu.default)(options.resolve ?? {}, { skipNodeModulesBundle: false }),
|
|
40
|
+
silenceHookLogging: true
|
|
41
|
+
}))()] }));
|
|
42
|
+
if (result.errors.length > 0) throw new Error(`Failed to bundle ${file}: ${result.errors.map((error) => error.text).join(", ")}`);
|
|
43
|
+
if (result.warnings.length > 0) context.warn(`Warnings while bundling ${file}: ${result.warnings.map((warning) => warning.text).join(", ")}`);
|
|
44
|
+
if (!result.outputFiles || result.outputFiles.filter(Boolean).length === 0) throw new Error(`No output files generated for ${file}. Please check the configuration and try again.`);
|
|
45
|
+
return result.outputFiles.filter(Boolean)[0];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
exports.bundle = bundle;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ResolveOptions, UnresolvedContext } from "@powerlines/core";
|
|
2
|
+
import { DeepPartial } from "@stryke/types/base";
|
|
3
|
+
import { BuildOptions, OutputFile } from "esbuild";
|
|
4
|
+
|
|
5
|
+
//#region src/bundle.d.ts
|
|
6
|
+
type BundleOptions = DeepPartial<BuildOptions> & {
|
|
7
|
+
name?: string;
|
|
8
|
+
resolve?: DeepPartial<ResolveOptions>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Bundle a type definition to a module.
|
|
12
|
+
*
|
|
13
|
+
* @param context - The context object containing the environment paths.
|
|
14
|
+
* @param file - The file path to bundle.
|
|
15
|
+
* @param options - Optional overrides for the ESBuild configuration.
|
|
16
|
+
* @returns A promise that resolves to the bundled module.
|
|
17
|
+
*/
|
|
18
|
+
declare function bundle<TContext extends UnresolvedContext>(context: TContext, file: string, options?: BundleOptions): Promise<OutputFile>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { BundleOptions, bundle };
|
|
21
|
+
//# sourceMappingURL=bundle.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bundle.d.cts","names":[],"sources":["../src/bundle.ts"],"mappings":";;;;;KA6BY,aAAA,GAAgB,WAAA,CAAY,YAAA;EACtC,IAAA;EACA,OAAA,GAAU,WAAA,CAAY,cAAA;AAAA;;;;;;;;;iBAWF,MAAA,kBAAwB,iBAAA,EAC5C,OAAA,EAAS,QAAA,EACT,IAAA,UACA,OAAA,GAAS,aAAA,GACR,OAAA,CAAQ,UAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { BuildOptions, OutputFile } from "esbuild";
|
|
2
|
+
import { ResolveOptions, UnresolvedContext } from "@powerlines/core";
|
|
3
|
+
import { DeepPartial } from "@stryke/types/base";
|
|
4
|
+
|
|
5
|
+
//#region src/bundle.d.ts
|
|
6
|
+
type BundleOptions = DeepPartial<BuildOptions> & {
|
|
7
|
+
name?: string;
|
|
8
|
+
resolve?: DeepPartial<ResolveOptions>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Bundle a type definition to a module.
|
|
12
|
+
*
|
|
13
|
+
* @param context - The context object containing the environment paths.
|
|
14
|
+
* @param file - The file path to bundle.
|
|
15
|
+
* @param options - Optional overrides for the ESBuild configuration.
|
|
16
|
+
* @returns A promise that resolves to the bundled module.
|
|
17
|
+
*/
|
|
18
|
+
declare function bundle<TContext extends UnresolvedContext>(context: TContext, file: string, options?: BundleOptions): Promise<OutputFile>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { BundleOptions, bundle };
|
|
21
|
+
//# sourceMappingURL=bundle.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bundle.d.mts","names":[],"sources":["../src/bundle.ts"],"mappings":";;;;;KA6BY,aAAA,GAAgB,WAAA,CAAY,YAAA;EACtC,IAAA;EACA,OAAA,GAAU,WAAA,CAAY,cAAA;AAAA;;;;;;;;;iBAWF,MAAA,kBAAwB,iBAAA,EAC5C,OAAA,EAAS,QAAA,EACT,IAAA,UACA,OAAA,GAAS,aAAA,GACR,OAAA,CAAQ,UAAA"}
|
package/dist/bundle.mjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createUnpluginResolver } from "@powerlines/core/lib/unplugin";
|
|
2
|
+
import { resolveOptions } from "@powerlines/unplugin/esbuild";
|
|
3
|
+
import { omit } from "@stryke/helpers/omit";
|
|
4
|
+
import { findFileName } from "@stryke/path/file-path-fns";
|
|
5
|
+
import defu from "defu";
|
|
6
|
+
import { build } from "esbuild";
|
|
7
|
+
import { createEsbuildPlugin } from "unplugin";
|
|
8
|
+
|
|
9
|
+
//#region src/bundle.ts
|
|
10
|
+
/**
|
|
11
|
+
* Bundle a type definition to a module.
|
|
12
|
+
*
|
|
13
|
+
* @param context - The context object containing the environment paths.
|
|
14
|
+
* @param file - The file path to bundle.
|
|
15
|
+
* @param options - Optional overrides for the ESBuild configuration.
|
|
16
|
+
* @returns A promise that resolves to the bundled module.
|
|
17
|
+
*/
|
|
18
|
+
async function bundle(context, file, options = {}) {
|
|
19
|
+
const path = await context.fs.resolve(file);
|
|
20
|
+
if (!path || !context.fs.existsSync(path)) throw new Error(`Module not found: "${file}". Please check the path and try again.`);
|
|
21
|
+
const result = await build(defu({
|
|
22
|
+
platform: "node",
|
|
23
|
+
...omit(options, ["name", "resolve"]),
|
|
24
|
+
logLevel: "silent",
|
|
25
|
+
entryPoints: [path],
|
|
26
|
+
write: false,
|
|
27
|
+
sourcemap: false,
|
|
28
|
+
splitting: false,
|
|
29
|
+
treeShaking: false,
|
|
30
|
+
bundle: true,
|
|
31
|
+
packages: "bundle",
|
|
32
|
+
keepNames: true,
|
|
33
|
+
metafile: false
|
|
34
|
+
}, resolveOptions(context), { plugins: [createEsbuildPlugin(createUnpluginResolver(context, {
|
|
35
|
+
name: options.name ?? `${findFileName(file)} Bundler`,
|
|
36
|
+
prefix: false,
|
|
37
|
+
overrides: defu(options.resolve ?? {}, { skipNodeModulesBundle: false }),
|
|
38
|
+
silenceHookLogging: true
|
|
39
|
+
}))()] }));
|
|
40
|
+
if (result.errors.length > 0) throw new Error(`Failed to bundle ${file}: ${result.errors.map((error) => error.text).join(", ")}`);
|
|
41
|
+
if (result.warnings.length > 0) context.warn(`Warnings while bundling ${file}: ${result.warnings.map((warning) => warning.text).join(", ")}`);
|
|
42
|
+
if (!result.outputFiles || result.outputFiles.filter(Boolean).length === 0) throw new Error(`No output files generated for ${file}. Please check the configuration and try again.`);
|
|
43
|
+
return result.outputFiles.filter(Boolean)[0];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
//#endregion
|
|
47
|
+
export { bundle };
|
|
48
|
+
//# sourceMappingURL=bundle.mjs.map
|
|
@@ -0,0 +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 { ResolveOptions, UnresolvedContext } 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 platform: \"node\",\n ...omit(options, [\"name\", \"resolve\"]),\n logLevel: \"silent\",\n entryPoints: [path],\n write: false,\n sourcemap: false,\n splitting: false,\n treeShaking: false,\n bundle: true,\n packages: \"bundle\",\n keepNames: true,\n metafile: false\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(options.resolve ?? {}, {\n skipNodeModulesBundle: false\n }),\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":";;;;;;;;;;;;;;;;;AA0CA,eAAsB,OACpB,SACA,MACA,UAAyB,CAAC,GACL;CACrB,MAAM,OAAO,MAAM,QAAQ,GAAG,QAAQ,IAAI;CAC1C,IAAI,CAAC,QAAQ,CAAC,QAAQ,GAAG,WAAW,IAAI,GACtC,MAAM,IAAI,MACR,sBAAsB,KAAK,wCAC7B;CAGF,MAAM,SAAS,MAAM,MACnB,KACE;EACE,UAAU;EACV,GAAG,KAAK,SAAS,CAAC,QAAQ,SAAS,CAAC;EACpC,UAAU;EACV,aAAa,CAAC,IAAI;EAClB,OAAO;EACP,WAAW;EACX,WAAW;EACX,aAAa;EACb,QAAQ;EACR,UAAU;EACV,WAAW;EACX,UAAU;CACZ,GACA,eAAe,OAAO,GACtB,EACE,SAAS,CACP,oBACE,uBAAuB,SAAS;EAC9B,MAAM,QAAQ,QAAQ,GAAG,aAAa,IAAI,EAAE;EAC5C,QAAQ;EACR,WAAW,KAAK,QAAQ,WAAW,CAAC,GAAG,EACrC,uBAAuB,MACzB,CAAC;EACD,oBAAoB;CACtB,CAAC,CACH,EAAE,CACJ,EACF,CACF,CACF;CACA,IAAI,OAAO,OAAO,SAAS,GACzB,MAAM,IAAI,MACR,oBAAoB,KAAK,IAAI,OAAO,OACjC,KAAI,UAAS,MAAM,IAAI,EACvB,KAAK,IAAI,GACd;CAEF,IAAI,OAAO,SAAS,SAAS,GAC3B,QAAQ,KACN,2BAA2B,KAAK,IAAI,OAAO,SACxC,KAAI,YAAW,QAAQ,IAAI,EAC3B,KAAK,IAAI,GACd;CAEF,IAAI,CAAC,OAAO,eAAe,OAAO,YAAY,OAAO,OAAO,EAAE,WAAW,GACvE,MAAM,IAAI,MACR,iCACE,KACD,gDACH;CAGF,OAAO,OAAO,YAAY,OAAO,OAAO,EAAE;AAC5C"}
|
package/dist/codegen.cjs
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
2
|
+
const require_metadata = require('./metadata.cjs');
|
|
3
|
+
const require_type_checks = require('./type-checks.cjs');
|
|
4
|
+
const require_helpers = require('./helpers.cjs');
|
|
5
|
+
const require_validate = require('./validate.cjs');
|
|
6
|
+
let _stryke_convert_to_bool = require("@stryke/convert/to-bool");
|
|
7
|
+
let _stryke_type_checks = require("@stryke/type-checks");
|
|
8
|
+
let _stryke_type_checks_is_boolean = require("@stryke/type-checks/is-boolean");
|
|
9
|
+
let _stryke_type_checks_is_null = require("@stryke/type-checks/is-null");
|
|
10
|
+
let _stryke_type_checks_is_number = require("@stryke/type-checks/is-number");
|
|
11
|
+
let _stryke_type_checks_is_set_string = require("@stryke/type-checks/is-set-string");
|
|
12
|
+
let _stryke_type_checks_is_undefined = require("@stryke/type-checks/is-undefined");
|
|
13
|
+
let ajv_dist_standalone_index_js = require("ajv/dist/standalone/index.js");
|
|
14
|
+
ajv_dist_standalone_index_js = require_runtime.__toESM(ajv_dist_standalone_index_js, 1);
|
|
15
|
+
|
|
16
|
+
//#region src/codegen.ts
|
|
17
|
+
/**
|
|
18
|
+
* Stringifies a value for generated TypeScript code.
|
|
19
|
+
*/
|
|
20
|
+
function stringifyValue(value, type) {
|
|
21
|
+
return (0, _stryke_type_checks_is_undefined.isUndefined)(value) ? "undefined" : (0, _stryke_type_checks_is_null.isNull)(value) ? "null" : type === "boolean" || (0, _stryke_type_checks_is_boolean.isBoolean)(value) ? String((0, _stryke_convert_to_bool.toBool)(value)) : type === "number" || (0, _stryke_type_checks_is_number.isNumber)(value) ? Number.parseFloat(String(value)).toLocaleString(void 0, { maximumFractionDigits: 20 }) : type === "integer" ? Number.parseInt(String(value)).toLocaleString() : type === "string" || type === "object" || type === "array" ? JSON.stringify(value) : String(value);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Stringifies a JSON Schema fragment into a TypeScript-like type string.
|
|
25
|
+
*/
|
|
26
|
+
function stringifyType(schema) {
|
|
27
|
+
if (!schema) return "unknown";
|
|
28
|
+
if (typeof schema === "boolean") return schema ? "unknown" : "never";
|
|
29
|
+
if (require_type_checks.isJsonSchemaObject(schema) && (0, _stryke_type_checks_is_set_string.isSetString)(schema.name)) return schema.name;
|
|
30
|
+
const objectSchema = schema;
|
|
31
|
+
if ((0, _stryke_type_checks_is_set_string.isSetString)(objectSchema.$ref)) return /^#\/(?:definitions|\$defs)\/(.+)$/.exec(objectSchema.$ref)?.[1] ?? objectSchema.$ref;
|
|
32
|
+
const primaryType = require_metadata.getPrimarySchemaType(schema);
|
|
33
|
+
if (primaryType) {
|
|
34
|
+
if (primaryType === "integer" || primaryType === "number") return "number";
|
|
35
|
+
return primaryType;
|
|
36
|
+
}
|
|
37
|
+
if (objectSchema.type === "array" && Array.isArray(objectSchema.enum)) return objectSchema.enum.map((value) => JSON.stringify(value)).join(" | ");
|
|
38
|
+
if (objectSchema.const !== void 0) return JSON.stringify(objectSchema.const);
|
|
39
|
+
if (objectSchema.type === "array" || objectSchema.items) return `${stringifyType(Array.isArray(objectSchema.items) ? objectSchema.items[0] : objectSchema.items)}[]`;
|
|
40
|
+
if (objectSchema.type === "object" || objectSchema.properties || objectSchema.additionalProperties) {
|
|
41
|
+
if (require_type_checks.isJsonSchema(objectSchema.additionalProperties)) return `{ [key: string]: ${stringifyType(objectSchema.additionalProperties)} }`;
|
|
42
|
+
if (require_type_checks.isJsonSchemaObject(objectSchema)) {
|
|
43
|
+
const required = objectSchema.required ?? [];
|
|
44
|
+
return `{ ${require_helpers.getPropertiesList(objectSchema).map((property) => {
|
|
45
|
+
const suffix = !required.includes(property.name) || require_helpers.isSchemaNullable(property) ? `${!required.includes(property.name) ? "?" : ""}${require_helpers.isSchemaNullable(property) ? " | null" : ""}` : "";
|
|
46
|
+
return `${property.name}${suffix}: ${stringifyType(property)}`;
|
|
47
|
+
}).join(";\n")} }`;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (objectSchema.oneOf || objectSchema.anyOf) return (objectSchema.oneOf ?? objectSchema.anyOf ?? []).map((branch) => stringifyType(branch)).join(" | ");
|
|
51
|
+
if (objectSchema.allOf) return "object";
|
|
52
|
+
return "unknown";
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns a string type representation of a value based on its type and an optional JSON Schema primitive type hint.
|
|
56
|
+
*
|
|
57
|
+
* @param value - The value whose type is to be represented as a string.
|
|
58
|
+
* @returns A string representation of the value's type, which may be influenced by the provided JSON Schema primitive type hint. The function handles various JavaScript types and formats them accordingly, including special handling for `undefined`, `null`, booleans, numbers (with formatting), strings, objects, and arrays. If a specific type hint is provided, it will take precedence in determining the string representation of the value.
|
|
59
|
+
*/
|
|
60
|
+
function getJsonSchemaType(value) {
|
|
61
|
+
return (0, _stryke_type_checks_is_null.isNull)(value) ? "null" : (0, _stryke_type_checks_is_boolean.isBoolean)(value) ? "boolean" : (0, _stryke_type_checks.isInteger)(value) ? "integer" : (0, _stryke_type_checks_is_number.isNumber)(value) ? "number" : (0, _stryke_type_checks.isString)(value) ? "string" : (0, _stryke_type_checks.isObject)(value) ? "object" : Array.isArray(value) ? "array" : void 0;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Resolves a local JSON Schema `$ref` (e.g. `#/$defs/Name`) to the referenced definition name.
|
|
65
|
+
*/
|
|
66
|
+
function resolveLocalRefName(ref) {
|
|
67
|
+
return /^#\/(?:definitions|\$defs)\/(.+)$/.exec(ref)?.[1];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Converts an arbitrary definition name into a safe JavaScript identifier suffix.
|
|
71
|
+
*/
|
|
72
|
+
function toParserIdentifier(name) {
|
|
73
|
+
const cleaned = name.replace(/[^\w$]/gu, "_");
|
|
74
|
+
return `parse_${/^\d/u.test(cleaned) ? `_${cleaned}` : cleaned}`;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Returns the list of JSON Schema `type` keyword values declared on a fragment,
|
|
78
|
+
* preserving `object` and `array` (which {@link readSchemaTypes} intentionally drops).
|
|
79
|
+
*/
|
|
80
|
+
function readDeclaredTypes(schema) {
|
|
81
|
+
const type = schema.type;
|
|
82
|
+
if (Array.isArray(type)) return [...type];
|
|
83
|
+
return type ? [type] : [];
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Generates a JavaScript expression that builds a path string for a child element.
|
|
87
|
+
*/
|
|
88
|
+
function childPath(pathExpr, segment) {
|
|
89
|
+
return `${pathExpr} + ${JSON.stringify(segment)}`;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Generates standalone parser code for a JSON Schema.
|
|
93
|
+
*
|
|
94
|
+
* @remarks
|
|
95
|
+
* The generated `parse` function reads an arbitrary input value and converts it
|
|
96
|
+
* into the shape described by the schema. It walks the schema recursively to:
|
|
97
|
+
*
|
|
98
|
+
* - resolve local `$ref` pointers (`#/$defs/*` and `#/definitions/*`) into
|
|
99
|
+
* dedicated parser functions so recursive schemas are supported,
|
|
100
|
+
* - apply `default` values for object properties (and root/array values) that
|
|
101
|
+
* are missing from the input,
|
|
102
|
+
* - coerce primitive values to the declared type (for example `"42"` to `42`
|
|
103
|
+
* for an `integer` schema, or `1` to `true` for a `boolean` schema),
|
|
104
|
+
* - validate `const`, `enum`, `oneOf`/`anyOf` and `allOf` constraints, and
|
|
105
|
+
* - collect detailed, path-aware errors and throw a `ParserError` when the
|
|
106
|
+
* input cannot be converted into a valid value.
|
|
107
|
+
*
|
|
108
|
+
* @param schema - The JSON Schema to generate parser code for.
|
|
109
|
+
* @returns The generated standalone parser code as a string.
|
|
110
|
+
*/
|
|
111
|
+
function generateParserCode(schema) {
|
|
112
|
+
const rootSchema = typeof schema === "boolean" ? schema : schema;
|
|
113
|
+
const definitions = typeof rootSchema === "boolean" ? {} : {
|
|
114
|
+
...rootSchema.definitions,
|
|
115
|
+
...rootSchema.$defs
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Generates a JavaScript expression that parses `valueExpr` against `fragment`.
|
|
119
|
+
* The expression has access to an in-scope mutable `errors` array.
|
|
120
|
+
*/
|
|
121
|
+
function generateExpression(fragment, valueExpr, pathExpr) {
|
|
122
|
+
if (typeof fragment === "boolean") return fragment ? valueExpr : `((value, path) => { errors.push({ path, message: "No value is allowed at this location" }); return value; })(${valueExpr}, ${pathExpr})`;
|
|
123
|
+
const view = fragment;
|
|
124
|
+
if ((0, _stryke_type_checks_is_set_string.isSetString)(view.$ref)) {
|
|
125
|
+
const refName = resolveLocalRefName(view.$ref);
|
|
126
|
+
if (refName && refName in definitions) return `${toParserIdentifier(refName)}(${valueExpr}, ${pathExpr}, errors)`;
|
|
127
|
+
return valueExpr;
|
|
128
|
+
}
|
|
129
|
+
return `((value, path) => {\n${generateBody(view)}\n})(${valueExpr}, ${pathExpr})`;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Generates the body statements (including a terminating `return`) for the
|
|
133
|
+
* arrow function produced by {@link generateExpression}.
|
|
134
|
+
*/
|
|
135
|
+
function generateBody(view) {
|
|
136
|
+
const lines = [];
|
|
137
|
+
const nullable = require_helpers.isSchemaNullable(view);
|
|
138
|
+
if (view.default !== void 0) lines.push(`if (value === undefined) { return ${JSON.stringify(view.default)}; }`);
|
|
139
|
+
else lines.push(`if (value === undefined) { errors.push({ path, message: "A value is required" }); return value; }`);
|
|
140
|
+
if (nullable) lines.push(`if (value === null) { return null; }`);
|
|
141
|
+
if (view.const !== void 0) {
|
|
142
|
+
const constValue = JSON.stringify(view.const);
|
|
143
|
+
lines.push(`if (JSON.stringify(value) !== ${JSON.stringify(constValue)}) { errors.push({ path, message: "Expected the constant value " + ${JSON.stringify(constValue)} }); }`, `return ${constValue};`);
|
|
144
|
+
return lines.join("\n");
|
|
145
|
+
}
|
|
146
|
+
if (Array.isArray(view.enum)) {
|
|
147
|
+
const enumValues = JSON.stringify(view.enum);
|
|
148
|
+
lines.push(`if (!${enumValues}.some(allowed => JSON.stringify(allowed) === JSON.stringify(value))) { errors.push({ path, message: "Expected one of " + ${JSON.stringify(enumValues)} }); }`, `return value;`);
|
|
149
|
+
return lines.join("\n");
|
|
150
|
+
}
|
|
151
|
+
if (Array.isArray(view.oneOf) || Array.isArray(view.anyOf)) {
|
|
152
|
+
const branchFns = (view.oneOf ?? view.anyOf ?? []).map((branch) => `(value, path, errors) => (${generateExpression(branch, "value", "path")})`).join(",\n");
|
|
153
|
+
lines.push(`const branches = [\n${branchFns}\n];`, `for (const branch of branches) {`, ` const branchErrors = [];`, ` const branchResult = branch(value, path, branchErrors);`, ` if (branchErrors.length === 0) { return branchResult; }`, `}`, `errors.push({ path, message: "Value does not match any of the allowed schemas" });`, `return value;`);
|
|
154
|
+
return lines.join("\n");
|
|
155
|
+
}
|
|
156
|
+
if (Array.isArray(view.allOf)) {
|
|
157
|
+
const { allOf, ...rest } = view;
|
|
158
|
+
const merged = require_helpers.merge(rest, ...allOf);
|
|
159
|
+
lines.push(`return ${generateExpression(merged, "value", "path")};`);
|
|
160
|
+
return lines.join("\n");
|
|
161
|
+
}
|
|
162
|
+
const declaredTypes = readDeclaredTypes(view);
|
|
163
|
+
switch (require_metadata.getPrimarySchemaType(view) ?? declaredTypes.find((type) => type !== "null") ?? (view.properties ? "object" : view.items ? "array" : void 0)) {
|
|
164
|
+
case "object":
|
|
165
|
+
lines.push(generateObjectBody(view));
|
|
166
|
+
break;
|
|
167
|
+
case "array":
|
|
168
|
+
lines.push(generateArrayBody(view));
|
|
169
|
+
break;
|
|
170
|
+
case "string":
|
|
171
|
+
lines.push(`if (typeof value === "string") { return value; }`, `if (typeof value === "number" || typeof value === "boolean") { return String(value); }`, `errors.push({ path, message: "Expected a string value" });`, `return value;`);
|
|
172
|
+
break;
|
|
173
|
+
case "integer":
|
|
174
|
+
lines.push(`if (typeof value === "number" && Number.isInteger(value)) { return value; }`, `if (typeof value === "string" && value.trim() !== "" && Number.isInteger(Number(value))) { return Number(value); }`, `if (typeof value === "boolean") { return value ? 1 : 0; }`, `errors.push({ path, message: "Expected an integer value" });`, `return value;`);
|
|
175
|
+
break;
|
|
176
|
+
case "number":
|
|
177
|
+
lines.push(`if (typeof value === "number") { return value; }`, `if (typeof value === "string" && value.trim() !== "" && !Number.isNaN(Number(value))) { return Number(value); }`, `if (typeof value === "boolean") { return value ? 1 : 0; }`, `errors.push({ path, message: "Expected a number value" });`, `return value;`);
|
|
178
|
+
break;
|
|
179
|
+
case "boolean":
|
|
180
|
+
lines.push(`if (typeof value === "boolean") { return value; }`, `if (value === "true" || value === 1) { return true; }`, `if (value === "false" || value === 0) { return false; }`, `errors.push({ path, message: "Expected a boolean value" });`, `return value;`);
|
|
181
|
+
break;
|
|
182
|
+
case "null":
|
|
183
|
+
lines.push(`if (value === null) { return null; }`, `errors.push({ path, message: "Expected a null value" });`, `return value;`);
|
|
184
|
+
break;
|
|
185
|
+
case void 0:
|
|
186
|
+
default:
|
|
187
|
+
lines.push(`return value;`);
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
return lines.join("\n");
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Generates the parsing statements for an `object` schema, applying property
|
|
194
|
+
* defaults and recursing into each declared property.
|
|
195
|
+
*/
|
|
196
|
+
function generateObjectBody(view) {
|
|
197
|
+
const type = stringifyType(view);
|
|
198
|
+
const lines = [`if (typeof value !== "object" || value === null || Array.isArray(value)) { errors.push({ path, message: "Expected an object value" }); return value; }`, `const result = {}${type ? ` as ${type}` : ""};`];
|
|
199
|
+
const properties = require_type_checks.isJsonSchemaObject(view) ? require_helpers.getPropertiesList(view) : [];
|
|
200
|
+
const propertyNames = /* @__PURE__ */ new Set();
|
|
201
|
+
for (const property of properties) {
|
|
202
|
+
const name = property.name;
|
|
203
|
+
propertyNames.add(name);
|
|
204
|
+
const accessor = `value[${JSON.stringify(name)}]`;
|
|
205
|
+
const propertyPath = childPath("path", `.${name}`);
|
|
206
|
+
const propertyExpression = generateExpression(property, accessor, propertyPath);
|
|
207
|
+
const missingBranch = property.default !== void 0 ? `result[${JSON.stringify(name)}] = ${JSON.stringify(property.default)};` : property.required ? `errors.push({ path: ${propertyPath}, message: "Required property is missing" });` : ``;
|
|
208
|
+
lines.push(`if (${accessor} !== undefined) {`, ` result[${JSON.stringify(name)}] = ${propertyExpression};`, `}${missingBranch ? ` else { ${missingBranch} }` : ``}`);
|
|
209
|
+
}
|
|
210
|
+
const additional = view.additionalProperties;
|
|
211
|
+
if (require_type_checks.isJsonSchema(additional)) {
|
|
212
|
+
const additionalExpression = generateExpression(additional, `value[key]`, `path + "." + key`);
|
|
213
|
+
lines.push(`for (const key of Object.keys(value)) {`, ` if (${JSON.stringify([...propertyNames])}.includes(key)) { continue; }`, ` result[key] = ${additionalExpression};`, `}`);
|
|
214
|
+
} else if (additional !== false) lines.push(`for (const key of Object.keys(value)) {`, ` if (${JSON.stringify([...propertyNames])}.includes(key)) { continue; }`, ` result[key] = value[key];`, `}`);
|
|
215
|
+
lines.push(`return result;`);
|
|
216
|
+
return lines.join("\n");
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Generates the parsing statements for an `array` schema, recursing into each
|
|
220
|
+
* item (supporting both list and tuple `items`/`prefixItems` forms).
|
|
221
|
+
*/
|
|
222
|
+
function generateArrayBody(view) {
|
|
223
|
+
const lines = [`if (!Array.isArray(value)) { errors.push({ path, message: "Expected an array value" }); return value; }`];
|
|
224
|
+
const tupleItems = view.prefixItems ?? (Array.isArray(view.items) ? view.items : void 0);
|
|
225
|
+
if (tupleItems) {
|
|
226
|
+
const listItems = !Array.isArray(view.items) ? view.items : void 0;
|
|
227
|
+
const itemExpressions = tupleItems.map((item, index) => `index === ${index} ? (${generateExpression(item, "item", childPath("path", `[${index}]`))})`);
|
|
228
|
+
const fallbackExpression = listItems ? generateExpression(listItems, "item", `path + "[" + index + "]"`) : "item";
|
|
229
|
+
lines.push(`return value.map((item, index) => ${itemExpressions.join(" : ")}${itemExpressions.length > 0 ? " : " : ""}${fallbackExpression});`);
|
|
230
|
+
return lines.join("\n");
|
|
231
|
+
}
|
|
232
|
+
const itemExpression = generateExpression(view.items ?? true, "item", `path + "[" + index + "]"`);
|
|
233
|
+
lines.push(`return value.map((item, index) => ${itemExpression});`);
|
|
234
|
+
return lines.join("\n");
|
|
235
|
+
}
|
|
236
|
+
return `/**
|
|
237
|
+
* Error thrown when an input value cannot be parsed into the type described by the JSON Schema.
|
|
238
|
+
*/
|
|
239
|
+
export class ParserError extends Error {
|
|
240
|
+
public override name = "ParserError";
|
|
241
|
+
|
|
242
|
+
public errors: { path: string; message: string }[];
|
|
243
|
+
|
|
244
|
+
public constructor(errors: { path: string; message: string }[]) {
|
|
245
|
+
super(
|
|
246
|
+
"Failed to parse the provided value against the JSON Schema:\\n" +
|
|
247
|
+
errors.map(error => " - " + error.path + ": " + error.message).join("\\n")
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
this.errors = errors;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
${Object.entries(definitions).map(([name, definition]) => `function ${toParserIdentifier(name)}(value, path, errors) {\n return ${generateExpression(definition, "value", "path")};\n}`).join("\n\n")}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Parses an input value into the type described by the JSON Schema.
|
|
258
|
+
*
|
|
259
|
+
* @remarks
|
|
260
|
+
* The parser applies default values for missing properties, coerces primitive values to the declared type, and throws a {@link ParserError} (containing a detailed list of validation errors) when the value cannot be converted into a valid result.
|
|
261
|
+
*
|
|
262
|
+
* @param value - The input value to parse.
|
|
263
|
+
* @returns The parsed value conforming to the schema.
|
|
264
|
+
*/
|
|
265
|
+
export function parse(value: unknown)${schema.name ? `: ${stringifyType(schema)}` : ""} {
|
|
266
|
+
const errors: { path: string; message: string }[] = [];
|
|
267
|
+
|
|
268
|
+
const result = ${generateExpression(schema, "value", "\"$\"")};
|
|
269
|
+
|
|
270
|
+
if (errors.length > 0) {
|
|
271
|
+
throw new ParserError(errors);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return result;
|
|
275
|
+
}`;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Generates standalone JSON Schema validation code using Ajv.
|
|
279
|
+
*
|
|
280
|
+
* @remarks
|
|
281
|
+
* The generated code includes a validation function that can be used to validate data against the provided JSON Schema at runtime. The validation function will throw an error if the data does not conform to the schema, providing detailed information about the validation errors.
|
|
282
|
+
*
|
|
283
|
+
* @param schema - The JSON Schema to generate validation code for.
|
|
284
|
+
* @param refsOrFuncts - Optional additional references or functions for Ajv's standalone code generation.
|
|
285
|
+
* @returns The generated standalone validation code as a string.
|
|
286
|
+
*/
|
|
287
|
+
function generateValidationCode(schema, refsOrFuncts) {
|
|
288
|
+
return (0, ajv_dist_standalone_index_js.default)(require_validate.getValidator(schema), refsOrFuncts);
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Generates standalone JavaScript code for validating and parsing data according to a JSON Schema.
|
|
292
|
+
*
|
|
293
|
+
* @remarks
|
|
294
|
+
* The generated code includes:
|
|
295
|
+
* - Validation code generated by Ajv for the provided JSON Schema, which can be used to validate data against the schema at runtime. The validation function will throw an error if the data does not conform to the schema, providing detailed information about the validation errors.
|
|
296
|
+
* - Parsing code generated for the provided JSON Schema, which can be used to parse and validate data against the schema at runtime. The parsing function will apply default values specified in the schema if they are not present in the input data, throw an error if the input data does not conform to the schema (providing detailed information about the validation errors), and return the parsed data if it is valid according to the schema.
|
|
297
|
+
*
|
|
298
|
+
* @param schema - The JSON Schema to generate code for.
|
|
299
|
+
* @param refsOrFuncts - Optional additional references or functions for Ajv's standalone code generation.
|
|
300
|
+
* @returns The generated standalone validation and parsing code as a string.
|
|
301
|
+
*/
|
|
302
|
+
function generateCode(schema, refsOrFuncts) {
|
|
303
|
+
return `${generateValidationCode(schema, refsOrFuncts)}\n\n${generateParserCode(schema)}`;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
//#endregion
|
|
307
|
+
exports.generateCode = generateCode;
|
|
308
|
+
exports.generateParserCode = generateParserCode;
|
|
309
|
+
exports.generateValidationCode = generateValidationCode;
|
|
310
|
+
exports.getJsonSchemaType = getJsonSchemaType;
|
|
311
|
+
exports.stringifyType = stringifyType;
|
|
312
|
+
exports.stringifyValue = stringifyValue;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { JsonSchema, JsonSchemaType } from "./types.cjs";
|
|
2
|
+
import standaloneCode from "ajv/dist/standalone/index.js";
|
|
3
|
+
|
|
4
|
+
//#region src/codegen.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Stringifies a value for generated TypeScript code.
|
|
7
|
+
*/
|
|
8
|
+
declare function stringifyValue(value?: unknown, type?: JsonSchemaType | string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Stringifies a JSON Schema fragment into a TypeScript-like type string.
|
|
11
|
+
*/
|
|
12
|
+
declare function stringifyType(schema?: JsonSchema): string;
|
|
13
|
+
/**
|
|
14
|
+
* Returns a string type representation of a value based on its type and an optional JSON Schema primitive type hint.
|
|
15
|
+
*
|
|
16
|
+
* @param value - The value whose type is to be represented as a string.
|
|
17
|
+
* @returns A string representation of the value's type, which may be influenced by the provided JSON Schema primitive type hint. The function handles various JavaScript types and formats them accordingly, including special handling for `undefined`, `null`, booleans, numbers (with formatting), strings, objects, and arrays. If a specific type hint is provided, it will take precedence in determining the string representation of the value.
|
|
18
|
+
*/
|
|
19
|
+
declare function getJsonSchemaType(value?: unknown): JsonSchemaType | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Generates standalone parser code for a JSON Schema.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* The generated `parse` function reads an arbitrary input value and converts it
|
|
25
|
+
* into the shape described by the schema. It walks the schema recursively to:
|
|
26
|
+
*
|
|
27
|
+
* - resolve local `$ref` pointers (`#/$defs/*` and `#/definitions/*`) into
|
|
28
|
+
* dedicated parser functions so recursive schemas are supported,
|
|
29
|
+
* - apply `default` values for object properties (and root/array values) that
|
|
30
|
+
* are missing from the input,
|
|
31
|
+
* - coerce primitive values to the declared type (for example `"42"` to `42`
|
|
32
|
+
* for an `integer` schema, or `1` to `true` for a `boolean` schema),
|
|
33
|
+
* - validate `const`, `enum`, `oneOf`/`anyOf` and `allOf` constraints, and
|
|
34
|
+
* - collect detailed, path-aware errors and throw a `ParserError` when the
|
|
35
|
+
* input cannot be converted into a valid value.
|
|
36
|
+
*
|
|
37
|
+
* @param schema - The JSON Schema to generate parser code for.
|
|
38
|
+
* @returns The generated standalone parser code as a string.
|
|
39
|
+
*/
|
|
40
|
+
declare function generateParserCode(schema: JsonSchema): string;
|
|
41
|
+
/**
|
|
42
|
+
* Generates standalone JSON Schema validation code using Ajv.
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* The generated code includes a validation function that can be used to validate data against the provided JSON Schema at runtime. The validation function will throw an error if the data does not conform to the schema, providing detailed information about the validation errors.
|
|
46
|
+
*
|
|
47
|
+
* @param schema - The JSON Schema to generate validation code for.
|
|
48
|
+
* @param refsOrFuncts - Optional additional references or functions for Ajv's standalone code generation.
|
|
49
|
+
* @returns The generated standalone validation code as a string.
|
|
50
|
+
*/
|
|
51
|
+
declare function generateValidationCode(schema: JsonSchema, refsOrFuncts?: Parameters<typeof standaloneCode>[1]): string;
|
|
52
|
+
/**
|
|
53
|
+
* Generates standalone JavaScript code for validating and parsing data according to a JSON Schema.
|
|
54
|
+
*
|
|
55
|
+
* @remarks
|
|
56
|
+
* The generated code includes:
|
|
57
|
+
* - Validation code generated by Ajv for the provided JSON Schema, which can be used to validate data against the schema at runtime. The validation function will throw an error if the data does not conform to the schema, providing detailed information about the validation errors.
|
|
58
|
+
* - Parsing code generated for the provided JSON Schema, which can be used to parse and validate data against the schema at runtime. The parsing function will apply default values specified in the schema if they are not present in the input data, throw an error if the input data does not conform to the schema (providing detailed information about the validation errors), and return the parsed data if it is valid according to the schema.
|
|
59
|
+
*
|
|
60
|
+
* @param schema - The JSON Schema to generate code for.
|
|
61
|
+
* @param refsOrFuncts - Optional additional references or functions for Ajv's standalone code generation.
|
|
62
|
+
* @returns The generated standalone validation and parsing code as a string.
|
|
63
|
+
*/
|
|
64
|
+
declare function generateCode(schema: JsonSchema, refsOrFuncts?: Parameters<typeof standaloneCode>[1]): string;
|
|
65
|
+
//#endregion
|
|
66
|
+
export { generateCode, generateParserCode, generateValidationCode, getJsonSchemaType, stringifyType, stringifyValue };
|
|
67
|
+
//# sourceMappingURL=codegen.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codegen.d.cts","names":[],"sources":["../src/codegen.ts"],"mappings":";;;;;;AAqDA;iBAAgB,cAAA,CACd,KAAA,YACA,IAAA,GAAO,cAAc;;;;iBAsBP,aAAA,CAAc,MAAmB,GAAV,UAAU;;;AAtBjB;AAsBhC;;;iBA8FgB,iBAAA,CAAkB,KAAA,aAAkB,cAAc;AA9FjB;AA8FjD;;;;AAAkE;AA0ElE;;;;AAAqD;AA+VrD;;;;;;;;;AAvgBiD,iBAwKjC,kBAAA,CAAmB,MAAkB,EAAV,UAAU;;;;;AAiWA;AAiBrD;;;;;iBAnBgB,sBAAA,CACd,MAAA,EAAQ,UAAA,EACR,YAAA,GAAe,UAAA,QAAkB,cAAA;;;;;;;;;AAmBkB;;;;iBAFrC,YAAA,CACd,MAAA,EAAQ,UAAA,EACR,YAAA,GAAe,UAAA,QAAkB,cAAA"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { JsonSchema, JsonSchemaType } from "./types.mjs";
|
|
2
|
+
import standaloneCode from "ajv/dist/standalone/index.js";
|
|
3
|
+
|
|
4
|
+
//#region src/codegen.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Stringifies a value for generated TypeScript code.
|
|
7
|
+
*/
|
|
8
|
+
declare function stringifyValue(value?: unknown, type?: JsonSchemaType | string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Stringifies a JSON Schema fragment into a TypeScript-like type string.
|
|
11
|
+
*/
|
|
12
|
+
declare function stringifyType(schema?: JsonSchema): string;
|
|
13
|
+
/**
|
|
14
|
+
* Returns a string type representation of a value based on its type and an optional JSON Schema primitive type hint.
|
|
15
|
+
*
|
|
16
|
+
* @param value - The value whose type is to be represented as a string.
|
|
17
|
+
* @returns A string representation of the value's type, which may be influenced by the provided JSON Schema primitive type hint. The function handles various JavaScript types and formats them accordingly, including special handling for `undefined`, `null`, booleans, numbers (with formatting), strings, objects, and arrays. If a specific type hint is provided, it will take precedence in determining the string representation of the value.
|
|
18
|
+
*/
|
|
19
|
+
declare function getJsonSchemaType(value?: unknown): JsonSchemaType | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Generates standalone parser code for a JSON Schema.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* The generated `parse` function reads an arbitrary input value and converts it
|
|
25
|
+
* into the shape described by the schema. It walks the schema recursively to:
|
|
26
|
+
*
|
|
27
|
+
* - resolve local `$ref` pointers (`#/$defs/*` and `#/definitions/*`) into
|
|
28
|
+
* dedicated parser functions so recursive schemas are supported,
|
|
29
|
+
* - apply `default` values for object properties (and root/array values) that
|
|
30
|
+
* are missing from the input,
|
|
31
|
+
* - coerce primitive values to the declared type (for example `"42"` to `42`
|
|
32
|
+
* for an `integer` schema, or `1` to `true` for a `boolean` schema),
|
|
33
|
+
* - validate `const`, `enum`, `oneOf`/`anyOf` and `allOf` constraints, and
|
|
34
|
+
* - collect detailed, path-aware errors and throw a `ParserError` when the
|
|
35
|
+
* input cannot be converted into a valid value.
|
|
36
|
+
*
|
|
37
|
+
* @param schema - The JSON Schema to generate parser code for.
|
|
38
|
+
* @returns The generated standalone parser code as a string.
|
|
39
|
+
*/
|
|
40
|
+
declare function generateParserCode(schema: JsonSchema): string;
|
|
41
|
+
/**
|
|
42
|
+
* Generates standalone JSON Schema validation code using Ajv.
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* The generated code includes a validation function that can be used to validate data against the provided JSON Schema at runtime. The validation function will throw an error if the data does not conform to the schema, providing detailed information about the validation errors.
|
|
46
|
+
*
|
|
47
|
+
* @param schema - The JSON Schema to generate validation code for.
|
|
48
|
+
* @param refsOrFuncts - Optional additional references or functions for Ajv's standalone code generation.
|
|
49
|
+
* @returns The generated standalone validation code as a string.
|
|
50
|
+
*/
|
|
51
|
+
declare function generateValidationCode(schema: JsonSchema, refsOrFuncts?: Parameters<typeof standaloneCode>[1]): string;
|
|
52
|
+
/**
|
|
53
|
+
* Generates standalone JavaScript code for validating and parsing data according to a JSON Schema.
|
|
54
|
+
*
|
|
55
|
+
* @remarks
|
|
56
|
+
* The generated code includes:
|
|
57
|
+
* - Validation code generated by Ajv for the provided JSON Schema, which can be used to validate data against the schema at runtime. The validation function will throw an error if the data does not conform to the schema, providing detailed information about the validation errors.
|
|
58
|
+
* - Parsing code generated for the provided JSON Schema, which can be used to parse and validate data against the schema at runtime. The parsing function will apply default values specified in the schema if they are not present in the input data, throw an error if the input data does not conform to the schema (providing detailed information about the validation errors), and return the parsed data if it is valid according to the schema.
|
|
59
|
+
*
|
|
60
|
+
* @param schema - The JSON Schema to generate code for.
|
|
61
|
+
* @param refsOrFuncts - Optional additional references or functions for Ajv's standalone code generation.
|
|
62
|
+
* @returns The generated standalone validation and parsing code as a string.
|
|
63
|
+
*/
|
|
64
|
+
declare function generateCode(schema: JsonSchema, refsOrFuncts?: Parameters<typeof standaloneCode>[1]): string;
|
|
65
|
+
//#endregion
|
|
66
|
+
export { generateCode, generateParserCode, generateValidationCode, getJsonSchemaType, stringifyType, stringifyValue };
|
|
67
|
+
//# sourceMappingURL=codegen.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codegen.d.mts","names":[],"sources":["../src/codegen.ts"],"mappings":";;;;;;AAqDA;iBAAgB,cAAA,CACd,KAAA,YACA,IAAA,GAAO,cAAc;;;;iBAsBP,aAAA,CAAc,MAAmB,GAAV,UAAU;;;AAtBjB;AAsBhC;;;iBA8FgB,iBAAA,CAAkB,KAAA,aAAkB,cAAc;AA9FjB;AA8FjD;;;;AAAkE;AA0ElE;;;;AAAqD;AA+VrD;;;;;;;;;AAvgBiD,iBAwKjC,kBAAA,CAAmB,MAAkB,EAAV,UAAU;;;;;AAiWA;AAiBrD;;;;;iBAnBgB,sBAAA,CACd,MAAA,EAAQ,UAAA,EACR,YAAA,GAAe,UAAA,QAAkB,cAAA;;;;;;;;;AAmBkB;;;;iBAFrC,YAAA,CACd,MAAA,EAAQ,UAAA,EACR,YAAA,GAAe,UAAA,QAAkB,cAAA"}
|