@ontrails/config 1.0.0-beta.12
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/.turbo/turbo-build.log +1 -0
- package/.turbo/turbo-lint.log +3 -0
- package/.turbo/turbo-typecheck.log +1 -0
- package/CHANGELOG.md +19 -0
- package/dist/app-config.d.ts +65 -0
- package/dist/app-config.d.ts.map +1 -0
- package/dist/app-config.js +172 -0
- package/dist/app-config.js.map +1 -0
- package/dist/collect.d.ts +11 -0
- package/dist/collect.d.ts.map +1 -0
- package/dist/collect.js +81 -0
- package/dist/collect.js.map +1 -0
- package/dist/compose.d.ts +26 -0
- package/dist/compose.d.ts.map +1 -0
- package/dist/compose.js +19 -0
- package/dist/compose.js.map +1 -0
- package/dist/config-layer.d.ts +11 -0
- package/dist/config-layer.d.ts.map +1 -0
- package/dist/config-layer.js +6 -0
- package/dist/config-layer.js.map +1 -0
- package/dist/config-service.d.ts +3 -0
- package/dist/config-service.d.ts.map +1 -0
- package/dist/config-service.js +26 -0
- package/dist/config-service.js.map +1 -0
- package/dist/define-config.d.ts +61 -0
- package/dist/define-config.d.ts.map +1 -0
- package/dist/define-config.js +90 -0
- package/dist/define-config.js.map +1 -0
- package/dist/describe.d.ts +25 -0
- package/dist/describe.d.ts.map +1 -0
- package/dist/describe.js +147 -0
- package/dist/describe.js.map +1 -0
- package/dist/doctor.d.ts +27 -0
- package/dist/doctor.d.ts.map +1 -0
- package/dist/doctor.js +167 -0
- package/dist/doctor.js.map +1 -0
- package/dist/explain.d.ts +30 -0
- package/dist/explain.d.ts.map +1 -0
- package/dist/explain.js +114 -0
- package/dist/explain.js.map +1 -0
- package/dist/extensions.d.ts +38 -0
- package/dist/extensions.d.ts.map +1 -0
- package/dist/extensions.js +35 -0
- package/dist/extensions.js.map +1 -0
- package/dist/generate/env.d.ts +15 -0
- package/dist/generate/env.d.ts.map +1 -0
- package/dist/generate/env.js +65 -0
- package/dist/generate/env.js.map +1 -0
- package/dist/generate/example.d.ts +16 -0
- package/dist/generate/example.d.ts.map +1 -0
- package/dist/generate/example.js +136 -0
- package/dist/generate/example.js.map +1 -0
- package/dist/generate/helpers.d.ts +35 -0
- package/dist/generate/helpers.d.ts.map +1 -0
- package/dist/generate/helpers.js +116 -0
- package/dist/generate/helpers.js.map +1 -0
- package/dist/generate/index.d.ts +4 -0
- package/dist/generate/index.d.ts.map +1 -0
- package/dist/generate/index.js +4 -0
- package/dist/generate/index.js.map +1 -0
- package/dist/generate/json-schema.d.ts +18 -0
- package/dist/generate/json-schema.d.ts.map +1 -0
- package/dist/generate/json-schema.js +97 -0
- package/dist/generate/json-schema.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/merge.d.ts +16 -0
- package/dist/merge.d.ts.map +1 -0
- package/dist/merge.js +34 -0
- package/dist/merge.js.map +1 -0
- package/dist/ref.d.ts +24 -0
- package/dist/ref.d.ts.map +1 -0
- package/dist/ref.js +25 -0
- package/dist/ref.js.map +1 -0
- package/dist/registry.d.ts +24 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +12 -0
- package/dist/registry.js.map +1 -0
- package/dist/resolve.d.ts +21 -0
- package/dist/resolve.d.ts.map +1 -0
- package/dist/resolve.js +174 -0
- package/dist/resolve.js.map +1 -0
- package/dist/secret-heuristics.d.ts +10 -0
- package/dist/secret-heuristics.d.ts.map +1 -0
- package/dist/secret-heuristics.js +11 -0
- package/dist/secret-heuristics.js.map +1 -0
- package/dist/trails/config-check.d.ts +11 -0
- package/dist/trails/config-check.d.ts.map +1 -0
- package/dist/trails/config-check.js +53 -0
- package/dist/trails/config-check.js.map +1 -0
- package/dist/trails/config-describe.d.ts +12 -0
- package/dist/trails/config-describe.d.ts.map +1 -0
- package/dist/trails/config-describe.js +41 -0
- package/dist/trails/config-describe.js.map +1 -0
- package/dist/trails/config-explain.d.ts +8 -0
- package/dist/trails/config-explain.d.ts.map +1 -0
- package/dist/trails/config-explain.js +74 -0
- package/dist/trails/config-explain.js.map +1 -0
- package/dist/trails/config-init.d.ts +9 -0
- package/dist/trails/config-init.d.ts.map +1 -0
- package/dist/trails/config-init.js +78 -0
- package/dist/trails/config-init.js.map +1 -0
- package/dist/workspace.d.ts +9 -0
- package/dist/workspace.d.ts.map +1 -0
- package/dist/workspace.js +44 -0
- package/dist/workspace.js.map +1 -0
- package/dist/zod-utils.d.ts +14 -0
- package/dist/zod-utils.d.ts.map +1 -0
- package/dist/zod-utils.js +41 -0
- package/dist/zod-utils.js.map +1 -0
- package/package.json +20 -0
- package/src/__tests__/app-config.test.ts +329 -0
- package/src/__tests__/compose.test.ts +59 -0
- package/src/__tests__/config-check.test.ts +171 -0
- package/src/__tests__/config-describe.test.ts +154 -0
- package/src/__tests__/config-explain.test.ts +167 -0
- package/src/__tests__/config-init.test.ts +210 -0
- package/src/__tests__/config-layer.test.ts +53 -0
- package/src/__tests__/config-service.test.ts +87 -0
- package/src/__tests__/define-config.test.ts +263 -0
- package/src/__tests__/describe.test.ts +158 -0
- package/src/__tests__/doctor.test.ts +172 -0
- package/src/__tests__/explain.test.ts +139 -0
- package/src/__tests__/extensions.test.ts +134 -0
- package/src/__tests__/generate.test.ts +269 -0
- package/src/__tests__/ref.test.ts +35 -0
- package/src/__tests__/resolve.test.ts +246 -0
- package/src/__tests__/workspace.test.ts +64 -0
- package/src/app-config.ts +307 -0
- package/src/collect.ts +118 -0
- package/src/compose.ts +46 -0
- package/src/config-layer.ts +15 -0
- package/src/config-service.ts +32 -0
- package/src/define-config.ts +134 -0
- package/src/describe.ts +252 -0
- package/src/doctor.ts +219 -0
- package/src/explain.ts +176 -0
- package/src/extensions.ts +51 -0
- package/src/generate/env.ts +104 -0
- package/src/generate/example.ts +222 -0
- package/src/generate/helpers.ts +158 -0
- package/src/generate/index.ts +3 -0
- package/src/generate/json-schema.ts +137 -0
- package/src/index.ts +44 -0
- package/src/merge.ts +43 -0
- package/src/ref.ts +38 -0
- package/src/registry.ts +33 -0
- package/src/resolve.ts +279 -0
- package/src/secret-heuristics.ts +13 -0
- package/src/trails/config-check.ts +60 -0
- package/src/trails/config-describe.ts +44 -0
- package/src/trails/config-explain.ts +93 -0
- package/src/trails/config-init.ts +96 -0
- package/src/workspace.ts +51 -0
- package/src/zod-utils.ts +53 -0
- package/tsconfig.json +9 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
/** Metadata shape stored on Zod schemas via `.meta()`. */
|
|
3
|
+
export interface ConfigFieldMeta {
|
|
4
|
+
readonly env?: string;
|
|
5
|
+
readonly secret?: boolean;
|
|
6
|
+
readonly deprecated?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Bind a schema field to an environment variable.
|
|
10
|
+
*
|
|
11
|
+
* Must be called BEFORE `.default()`, `.optional()`, or other transforms
|
|
12
|
+
* so that the metadata lives on the inner type where `collectConfigMeta`
|
|
13
|
+
* can find it by unwrapping wrappers.
|
|
14
|
+
*/
|
|
15
|
+
export declare const env: <T extends z.ZodType>(schema: T, varName: string) => T;
|
|
16
|
+
/**
|
|
17
|
+
* Mark a schema field as sensitive. Redacted in survey, explain, and logs.
|
|
18
|
+
*
|
|
19
|
+
* Must be called BEFORE `.default()`, `.optional()`, or other transforms.
|
|
20
|
+
*/
|
|
21
|
+
export declare const secret: <T extends z.ZodType>(schema: T) => T;
|
|
22
|
+
/**
|
|
23
|
+
* Mark a schema field as deprecated with migration guidance.
|
|
24
|
+
*
|
|
25
|
+
* Stores **two** meta keys: `deprecated: true` and `deprecationMessage: string`.
|
|
26
|
+
* This indirection exists because Zod 4's `GlobalMeta` types `deprecated` as
|
|
27
|
+
* `boolean | undefined` — there is no way to attach a migration message to the
|
|
28
|
+
* standard key. We set `deprecated: true` so Zod-native tooling (schema
|
|
29
|
+
* serializers, OpenAPI generators) recognises the field as deprecated, and store
|
|
30
|
+
* the human-readable message under `deprecationMessage` for our own
|
|
31
|
+
* `collectConfigMeta` / survey / explain surfaces.
|
|
32
|
+
*
|
|
33
|
+
* Must be called BEFORE `.default()`, `.optional()`, or other transforms
|
|
34
|
+
* so that the metadata lives on the inner type where `collectConfigMeta`
|
|
35
|
+
* can find it by unwrapping wrappers.
|
|
36
|
+
*/
|
|
37
|
+
export declare const deprecated: <T extends z.ZodType>(schema: T, message: string) => T;
|
|
38
|
+
//# sourceMappingURL=extensions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extensions.d.ts","sourceRoot":"","sources":["../src/extensions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,0DAA0D;AAC1D,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED;;;;;;GAMG;AACH,eAAO,MAAM,GAAG,GAAI,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,QAAQ,CAAC,EAAE,SAAS,MAAM,KAAG,CAChB,CAAC;AAEvD;;;;GAIG;AACH,eAAO,MAAM,MAAM,GAAI,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,QAAQ,CAAC,KAAG,CACF,CAAC;AAEvD;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,UAAU,GAAI,CAAC,SAAS,CAAC,CAAC,OAAO,EAC5C,QAAQ,CAAC,EACT,SAAS,MAAM,KACd,CAKM,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bind a schema field to an environment variable.
|
|
3
|
+
*
|
|
4
|
+
* Must be called BEFORE `.default()`, `.optional()`, or other transforms
|
|
5
|
+
* so that the metadata lives on the inner type where `collectConfigMeta`
|
|
6
|
+
* can find it by unwrapping wrappers.
|
|
7
|
+
*/
|
|
8
|
+
export const env = (schema, varName) => schema.meta({ ...schema.meta(), env: varName });
|
|
9
|
+
/**
|
|
10
|
+
* Mark a schema field as sensitive. Redacted in survey, explain, and logs.
|
|
11
|
+
*
|
|
12
|
+
* Must be called BEFORE `.default()`, `.optional()`, or other transforms.
|
|
13
|
+
*/
|
|
14
|
+
export const secret = (schema) => schema.meta({ ...schema.meta(), secret: true });
|
|
15
|
+
/**
|
|
16
|
+
* Mark a schema field as deprecated with migration guidance.
|
|
17
|
+
*
|
|
18
|
+
* Stores **two** meta keys: `deprecated: true` and `deprecationMessage: string`.
|
|
19
|
+
* This indirection exists because Zod 4's `GlobalMeta` types `deprecated` as
|
|
20
|
+
* `boolean | undefined` — there is no way to attach a migration message to the
|
|
21
|
+
* standard key. We set `deprecated: true` so Zod-native tooling (schema
|
|
22
|
+
* serializers, OpenAPI generators) recognises the field as deprecated, and store
|
|
23
|
+
* the human-readable message under `deprecationMessage` for our own
|
|
24
|
+
* `collectConfigMeta` / survey / explain surfaces.
|
|
25
|
+
*
|
|
26
|
+
* Must be called BEFORE `.default()`, `.optional()`, or other transforms
|
|
27
|
+
* so that the metadata lives on the inner type where `collectConfigMeta`
|
|
28
|
+
* can find it by unwrapping wrappers.
|
|
29
|
+
*/
|
|
30
|
+
export const deprecated = (schema, message) => schema.meta({
|
|
31
|
+
...schema.meta(),
|
|
32
|
+
deprecated: true,
|
|
33
|
+
deprecationMessage: message,
|
|
34
|
+
});
|
|
35
|
+
//# sourceMappingURL=extensions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extensions.js","sourceRoot":"","sources":["../src/extensions.ts"],"names":[],"mappings":"AASA;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,GAAG,GAAG,CAAsB,MAAS,EAAE,OAAe,EAAK,EAAE,CACxE,MAAM,CAAC,IAAI,CAAC,EAAE,GAAG,MAAM,CAAC,IAAI,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,CAAM,CAAC;AAEvD;;;;GAIG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CAAsB,MAAS,EAAK,EAAE,CAC1D,MAAM,CAAC,IAAI,CAAC,EAAE,GAAG,MAAM,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAM,CAAC;AAEvD;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,MAAS,EACT,OAAe,EACZ,EAAE,CACL,MAAM,CAAC,IAAI,CAAC;IACV,GAAG,MAAM,CAAC,IAAI,EAAE;IAChB,UAAU,EAAE,IAAI;IAChB,kBAAkB,EAAE,OAAO;CAC5B,CAAM,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `.env.example` file generation from Zod schema `env()` bindings.
|
|
3
|
+
*
|
|
4
|
+
* Lists each env var with its type, default, and whether it is a secret.
|
|
5
|
+
* Returns an empty string when no env bindings are present.
|
|
6
|
+
*/
|
|
7
|
+
import type { z } from 'zod';
|
|
8
|
+
/**
|
|
9
|
+
* Generate a `.env.example` file from `env()` bindings in the schema.
|
|
10
|
+
*
|
|
11
|
+
* Lists each env var with its type, default, and whether it is a secret.
|
|
12
|
+
* Returns an empty string when no env bindings are present.
|
|
13
|
+
*/
|
|
14
|
+
export declare const generateEnvExample: (schema: z.ZodObject<Record<string, z.ZodType>>) => string;
|
|
15
|
+
//# sourceMappingURL=env.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../../src/generate/env.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAsF7B;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAC7B,QAAQ,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,KAC7C,MAGF,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { collectConfigMeta } from '../collect.js';
|
|
2
|
+
import { isLikelySecret } from '../secret-heuristics.js';
|
|
3
|
+
import { formatValue, getDefault, getDescription, resolveFieldByPath, unwrap, zodTypeName, zodTypeToJsonSchema, } from './helpers.js';
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Helpers
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
/** Map Zod type names to human-readable type labels. */
|
|
8
|
+
const typeLabel = (schema) => {
|
|
9
|
+
const typeName = zodTypeName(unwrap(schema));
|
|
10
|
+
return zodTypeToJsonSchema[typeName] ?? typeName;
|
|
11
|
+
};
|
|
12
|
+
/** Build the type annotation comment for an env entry. */
|
|
13
|
+
const envTypeAnnotation = (fieldSchema, meta) => {
|
|
14
|
+
const parts = [`type: ${typeLabel(fieldSchema)}`];
|
|
15
|
+
const info = getDefault(fieldSchema);
|
|
16
|
+
if (info.has) {
|
|
17
|
+
parts.push(`default: ${formatValue(info.value)}`);
|
|
18
|
+
}
|
|
19
|
+
if (meta.secret) {
|
|
20
|
+
parts.push('secret');
|
|
21
|
+
}
|
|
22
|
+
return `# ${parts.join(', ')}`;
|
|
23
|
+
};
|
|
24
|
+
/** Format a single env var entry. */
|
|
25
|
+
const envEntry = (envVar, fieldSchema, meta) => {
|
|
26
|
+
const effectiveMeta = !meta.secret && isLikelySecret(envVar) ? { ...meta, secret: true } : meta;
|
|
27
|
+
const lines = [];
|
|
28
|
+
const desc = getDescription(fieldSchema);
|
|
29
|
+
if (desc) {
|
|
30
|
+
lines.push(`# ${desc}`);
|
|
31
|
+
}
|
|
32
|
+
lines.push(envTypeAnnotation(fieldSchema, effectiveMeta));
|
|
33
|
+
lines.push(`${envVar}=`);
|
|
34
|
+
return lines;
|
|
35
|
+
};
|
|
36
|
+
/** Collect env entries from config metadata. */
|
|
37
|
+
const collectEnvEntries = (schema, meta) => {
|
|
38
|
+
const entries = [];
|
|
39
|
+
for (const [path, fieldMeta] of meta) {
|
|
40
|
+
if (!fieldMeta.env) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const fieldSchema = resolveFieldByPath(schema, path);
|
|
44
|
+
if (!fieldSchema) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
entries.push(...envEntry(fieldMeta.env, fieldSchema, fieldMeta));
|
|
48
|
+
entries.push('');
|
|
49
|
+
}
|
|
50
|
+
return entries;
|
|
51
|
+
};
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
// Public API
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
/**
|
|
56
|
+
* Generate a `.env.example` file from `env()` bindings in the schema.
|
|
57
|
+
*
|
|
58
|
+
* Lists each env var with its type, default, and whether it is a secret.
|
|
59
|
+
* Returns an empty string when no env bindings are present.
|
|
60
|
+
*/
|
|
61
|
+
export const generateEnvExample = (schema) => {
|
|
62
|
+
const entries = collectEnvEntries(schema, collectConfigMeta(schema));
|
|
63
|
+
return entries.length === 0 ? '' : entries.join('\n');
|
|
64
|
+
};
|
|
65
|
+
//# sourceMappingURL=env.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","sourceRoot":"","sources":["../../src/generate/env.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAGlD,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAEzD,OAAO,EACL,WAAW,EACX,UAAU,EACV,cAAc,EACd,kBAAkB,EAClB,MAAM,EACN,WAAW,EACX,mBAAmB,GACpB,MAAM,cAAc,CAAC;AAEtB,8EAA8E;AAC9E,UAAU;AACV,8EAA8E;AAE9E,wDAAwD;AACxD,MAAM,SAAS,GAAG,CAAC,MAAiB,EAAU,EAAE;IAC9C,MAAM,QAAQ,GAAG,WAAW,CAAC,MA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AG,CAAC,CAAC;IACzB,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEF,gDAAgD;AAChD,MAAM,iBAAiB,GAAG,CACxB,MAA8C,EAC9C,IAAkC,EACf,EAAE;IACrB,MAAM,OAAO,GAAa,EAAE,CAAC;IAC7B,KAAK,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,IAAI,EAAE,CAAC;QACrC,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC;YACnB,SAAS;QACX,CAAC;QACD,MAAM,WAAW,GAAG,kBAAkB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QACrD,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,SAAS;QACX,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,SAAS,CAAC,GAAG,EAAE,WAAW,EAAE,SAAS,CAAC,CAAC,CAAC;QACjE,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACnB,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC;AAEF,8EAA8E;AAC9E,aAAa;AACb,8EAA8E;AAE9E;;;;;GAKG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,MAA8C,EACtC,EAAE;IACV,MAAM,OAAO,GAAG,iBAAiB,CAAC,MAAM,EAAE,iBAAiB,CAAC,MAAM,CAAC,CAAC,CAAC;IACrE,OAAO,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACxD,CAAC,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config example file generation in multiple formats.
|
|
3
|
+
*
|
|
4
|
+
* Produces TOML, JSON, JSONC, and YAML example files from a Zod schema,
|
|
5
|
+
* with defaults shown and deprecated fields annotated.
|
|
6
|
+
*/
|
|
7
|
+
import type { z } from 'zod';
|
|
8
|
+
type ExampleFormat = 'json' | 'jsonc' | 'toml' | 'yaml';
|
|
9
|
+
/**
|
|
10
|
+
* Generate an example config file in the specified format.
|
|
11
|
+
*
|
|
12
|
+
* Includes descriptions as comments, defaults shown, deprecated fields annotated.
|
|
13
|
+
*/
|
|
14
|
+
export declare const generateExample: (schema: z.ZodObject<Record<string, z.ZodType>>, format: ExampleFormat) => string;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=example.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"example.d.ts","sourceRoot":"","sources":["../../src/generate/example.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAoM7B,KAAK,aAAa,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,CAAC;AAYxD;;;;GAIG;AACH,eAAO,MAAM,eAAe,GAC1B,QAAQ,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,EAC9C,QAAQ,aAAa,KACpB,MAAoC,CAAC"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { fieldComments, fieldValue, formatValue, getObjectShape, isObjectType, pushComments, unwrap, } from './helpers.js';
|
|
2
|
+
// ---------------------------------------------------------------------------
|
|
3
|
+
// TOML generation
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
/** Render a flat set of fields as TOML key = value lines. */
|
|
6
|
+
const renderTomlFields = (shape, lines) => {
|
|
7
|
+
for (const [key, fieldSchema] of Object.entries(shape)) {
|
|
8
|
+
if (isObjectType(fieldSchema)) {
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
pushComments(lines, fieldComments(fieldSchema, '#'), '');
|
|
12
|
+
lines.push(`${key} = ${formatValue(fieldValue(fieldSchema))}`);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
/** Render nested objects as TOML sections, recursing with dotted prefixes. */
|
|
16
|
+
const renderTomlSections = (shape, lines, prefix = '') => {
|
|
17
|
+
for (const [key, fieldSchema] of Object.entries(shape)) {
|
|
18
|
+
const nested = isObjectType(fieldSchema)
|
|
19
|
+
? getObjectShape(fieldSchema)
|
|
20
|
+
: undefined;
|
|
21
|
+
if (!nested) {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const sectionKey = prefix ? `${prefix}.${key}` : key;
|
|
25
|
+
if (lines.length > 0) {
|
|
26
|
+
lines.push('');
|
|
27
|
+
}
|
|
28
|
+
lines.push(`[${sectionKey}]`);
|
|
29
|
+
renderTomlFields(nested, lines);
|
|
30
|
+
// oxlint-disable-next-line max-statements -- recursive TOML section rendering
|
|
31
|
+
renderTomlSections(nested, lines, sectionKey);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const formatToml = (schema) => {
|
|
35
|
+
const shape = schema.shape;
|
|
36
|
+
const lines = [];
|
|
37
|
+
renderTomlFields(shape, lines);
|
|
38
|
+
renderTomlSections(shape, lines);
|
|
39
|
+
return `${lines.join('\n')}\n`;
|
|
40
|
+
};
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
// JSON / JSONC generation
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
/** Resolve a field to its nested object or its scalar value. */
|
|
45
|
+
const resolveJsonField = (fieldSchema, recurse) => {
|
|
46
|
+
if (!isObjectType(fieldSchema)) {
|
|
47
|
+
return fieldValue(fieldSchema);
|
|
48
|
+
}
|
|
49
|
+
const nested = getObjectShape(fieldSchema);
|
|
50
|
+
if (!nested) {
|
|
51
|
+
return fieldValue(fieldSchema);
|
|
52
|
+
}
|
|
53
|
+
const inner = unwrap(fieldSchema);
|
|
54
|
+
return recurse(inner);
|
|
55
|
+
};
|
|
56
|
+
/** Build a plain object with default/placeholder values for JSON output. */
|
|
57
|
+
const buildJsonObject = (schema) => {
|
|
58
|
+
const shape = schema.shape;
|
|
59
|
+
const obj = {};
|
|
60
|
+
for (const [key, fieldSchema] of Object.entries(shape)) {
|
|
61
|
+
obj[key] = resolveJsonField(fieldSchema, buildJsonObject);
|
|
62
|
+
}
|
|
63
|
+
return obj;
|
|
64
|
+
};
|
|
65
|
+
const formatJson = (schema) => `${JSON.stringify(buildJsonObject(schema), null, 2)}\n`;
|
|
66
|
+
/** Render a single JSONC entry (comments + key: value). */
|
|
67
|
+
const renderJsoncEntry = (key, fieldSchema, isLast, lines, indent) => {
|
|
68
|
+
pushComments(lines, fieldComments(fieldSchema, '//'), indent);
|
|
69
|
+
const comma = isLast ? '' : ',';
|
|
70
|
+
const nested = getObjectShape(fieldSchema);
|
|
71
|
+
if (nested) {
|
|
72
|
+
lines.push(`${indent}"${key}": {`);
|
|
73
|
+
renderJsoncFields(nested, lines, `${indent} `);
|
|
74
|
+
lines.push(`${indent}}${comma}`);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const val = formatValue(fieldValue(fieldSchema));
|
|
78
|
+
lines.push(`${indent}"${key}": ${val}${comma}`);
|
|
79
|
+
};
|
|
80
|
+
const renderJsoncFields = (shape, lines, indent) => {
|
|
81
|
+
const keys = Object.keys(shape);
|
|
82
|
+
for (let i = 0; i < keys.length; i += 1) {
|
|
83
|
+
const key = keys[i];
|
|
84
|
+
renderJsoncEntry(key, shape[key], i === keys.length - 1, lines, indent);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const formatJsonc = (schema) => {
|
|
88
|
+
const shape = schema.shape;
|
|
89
|
+
const lines = ['{'];
|
|
90
|
+
renderJsoncFields(shape, lines, ' ');
|
|
91
|
+
lines.push('}');
|
|
92
|
+
return `${lines.join('\n')}\n`;
|
|
93
|
+
};
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
// YAML generation
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
/** Render a single YAML scalar field. */
|
|
98
|
+
const renderYamlScalar = (key, fieldSchema, indent, lines) => {
|
|
99
|
+
pushComments(lines, fieldComments(fieldSchema, '#'), indent);
|
|
100
|
+
lines.push(`${indent}${key}: ${formatValue(fieldValue(fieldSchema))}`);
|
|
101
|
+
};
|
|
102
|
+
/** Render YAML fields at a given indent level. */
|
|
103
|
+
const renderYamlFields = (shape, indent, lines) => {
|
|
104
|
+
for (const [key, fieldSchema] of Object.entries(shape)) {
|
|
105
|
+
if (isObjectType(fieldSchema)) {
|
|
106
|
+
const nested = getObjectShape(fieldSchema);
|
|
107
|
+
if (!nested) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
lines.push(`${indent}${key}:`);
|
|
111
|
+
renderYamlFields(nested, `${indent} `, lines);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
renderYamlScalar(key, fieldSchema, indent, lines);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const formatYaml = (schema) => {
|
|
119
|
+
const shape = schema.shape;
|
|
120
|
+
const lines = [];
|
|
121
|
+
renderYamlFields(shape, '', lines);
|
|
122
|
+
return `${lines.join('\n')}\n`;
|
|
123
|
+
};
|
|
124
|
+
const formatters = {
|
|
125
|
+
json: formatJson,
|
|
126
|
+
jsonc: formatJsonc,
|
|
127
|
+
toml: formatToml,
|
|
128
|
+
yaml: formatYaml,
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Generate an example config file in the specified format.
|
|
132
|
+
*
|
|
133
|
+
* Includes descriptions as comments, defaults shown, deprecated fields annotated.
|
|
134
|
+
*/
|
|
135
|
+
export const generateExample = (schema, format) => formatters[format](schema);
|
|
136
|
+
//# sourceMappingURL=example.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"example.js","sourceRoot":"","sources":["../../src/generate/example.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,aAAa,EACb,UAAU,EACV,WAAW,EACX,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,MAAM,GACP,MAAM,cAAc,CAAC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,MAAM,MAAM,GAAG,cAAc,CAAC,WAAW,CAAC,CAAC;YAC3C,IAAI,CAAC,MAAM,EAAE,CAAC;gBACZ,SAAS;YACX,CAAC;YACD,KAAK,CAAC,IAAI,CAAC,GAAG,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC;YAC/B,gBAAgB,CAAC,MAAM,EAAE,GAAG,MAAM,IAAI,EAAE,KAAK,CAAC,CAAC;QACjD,CAAC;aAAM,CAAC;YACN,gBAAgB,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;QACpD,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,UAAU,GAAG,CAAC,MAA8C,EAAU,EAAE;IAC5E,MAAM,KAAK,GAAG,MAAM,CAAC,KAAkC,CAAC;IACxD,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,gBAAgB,CAAC,KAAK,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;IACnC,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;AACjC,CAAC,CAAC;AAQF,MAAM,UAAU,GAGZ;IACF,IAAI,EAAE,UAAU;IAChB,KAAK,EAAE,WAAW;IAClB,IAAI,EAAE,UAAU;IAChB,IAAI,EAAE,UAAU;CACjB,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,MAA8C,EAC9C,MAAqB,EACb,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
/** Unwrap `.default()`, `.optional()`, `.nullable()` to reach the inner type. */
|
|
3
|
+
export declare const unwrap: (schema: z.ZodType) => z.ZodType;
|
|
4
|
+
/** Read the Zod type discriminant from a schema's def. */
|
|
5
|
+
export declare const zodTypeName: (schema: z.ZodType) => string;
|
|
6
|
+
/** Walk the registry up through wrappers to find a metadata key. */
|
|
7
|
+
export declare const walkRegistryKey: (schema: z.ZodType, key: string) => unknown | undefined;
|
|
8
|
+
/** Get the description from the global registry for a schema or its inner type. */
|
|
9
|
+
export declare const getDescription: (schema: z.ZodType) => string | undefined;
|
|
10
|
+
/** Get the deprecation message from the config meta for a field. */
|
|
11
|
+
export declare const getDeprecation: (schema: z.ZodType) => string | undefined;
|
|
12
|
+
/** Get the default value from a schema if it has one. */
|
|
13
|
+
export declare const getDefault: (schema: z.ZodType) => {
|
|
14
|
+
has: false;
|
|
15
|
+
} | {
|
|
16
|
+
has: true;
|
|
17
|
+
value: unknown;
|
|
18
|
+
};
|
|
19
|
+
/** Check whether a schema (or its inner type) is a ZodObject. */
|
|
20
|
+
export declare const isObjectType: (schema: z.ZodType) => boolean;
|
|
21
|
+
/** Get the shape of a ZodObject, unwrapping wrappers first. */
|
|
22
|
+
export declare const getObjectShape: (schema: z.ZodType) => Record<string, z.ZodType> | undefined;
|
|
23
|
+
/** Resolve a dot-separated path to the field schema within an object. */
|
|
24
|
+
export declare const resolveFieldByPath: (schema: z.ZodObject<Record<string, z.ZodType>>, path: string) => z.ZodType | undefined;
|
|
25
|
+
/** Format a value as a quoted string or literal. */
|
|
26
|
+
export declare const formatValue: (value: unknown) => string;
|
|
27
|
+
/** Get the display value for a field (default or empty placeholder). */
|
|
28
|
+
export declare const fieldValue: (schema: z.ZodType) => unknown;
|
|
29
|
+
/** Collect comment lines for a field (description + deprecation). */
|
|
30
|
+
export declare const fieldComments: (schema: z.ZodType, prefix: string) => readonly string[];
|
|
31
|
+
/** Append comment lines to an output array. */
|
|
32
|
+
export declare const pushComments: (lines: string[], comments: readonly string[], indent: string) => void;
|
|
33
|
+
/** Map a Zod type name to a JSON Schema type. */
|
|
34
|
+
export declare const zodTypeToJsonSchema: Record<string, string>;
|
|
35
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources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pD,eAAO,MAAM,WAAW,GAAI,OAAO,OAAO,KAAG,MAK5C,CAAC;AAEF,wEAAwE;AACxE,eAAO,MAAM,UAAU,GAAI,QAAQ,CAAC,CAAC,OAAO,KAAG,OAG9C,CAAC;AAEF,qEAAqE;AACrE,eAAO,MAAM,aAAa,GACxB,QAAQ,CAAC,CAAC,OAAO,EACjB,QAAQ,MAAM,KACb,SAAS,MAAM,EAWjB,CAAC;AAEF,+CAA+C;AAC/C,eAAO,MAAM,YAAY,GACvB,OAAO,MAAM,EAAE,EACf,UAAU,SAAS,MAAM,EAAE,EAC3B,QAAQ,MAAM,KACb,IAIF,CAAC;AAEF,iDAAiD;AACjD,eAAO,MAAM,mBAAmB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAItD,CAAC"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { globalRegistry } from 'zod';
|
|
2
|
+
// ---------------------------------------------------------------------------
|
|
3
|
+
// Schema introspection helpers
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
/** Unwrap `.default()`, `.optional()`, `.nullable()` to reach the inner type. */
|
|
6
|
+
export const unwrap = (schema) => {
|
|
7
|
+
let current = schema;
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
9
|
+
while (true) {
|
|
10
|
+
const def = current.def;
|
|
11
|
+
const inner = def['innerType'];
|
|
12
|
+
if (!inner) {
|
|
13
|
+
return current;
|
|
14
|
+
}
|
|
15
|
+
current = inner;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
/** Read the Zod type discriminant from a schema's def. */
|
|
19
|
+
export const zodTypeName = (schema) => {
|
|
20
|
+
const def = schema.def;
|
|
21
|
+
return def['type'] ?? 'unknown';
|
|
22
|
+
};
|
|
23
|
+
/** Walk the registry up through wrappers to find a metadata key. */
|
|
24
|
+
export const walkRegistryKey = (schema, key) => {
|
|
25
|
+
let current = schema;
|
|
26
|
+
while (current) {
|
|
27
|
+
const meta = globalRegistry.get(current);
|
|
28
|
+
if (meta?.[key] !== undefined) {
|
|
29
|
+
return meta[key];
|
|
30
|
+
}
|
|
31
|
+
const def = current.def;
|
|
32
|
+
current = def['innerType'];
|
|
33
|
+
}
|
|
34
|
+
return undefined;
|
|
35
|
+
};
|
|
36
|
+
/** Get the description from the global registry for a schema or its inner type. */
|
|
37
|
+
export const getDescription = (schema) => walkRegistryKey(schema, 'description');
|
|
38
|
+
/** Get the deprecation message from the config meta for a field. */
|
|
39
|
+
export const getDeprecation = (schema) => walkRegistryKey(schema, 'deprecationMessage');
|
|
40
|
+
/** Get the default value from a schema if it has one. */
|
|
41
|
+
export const getDefault = (schema) => {
|
|
42
|
+
const def = schema.def;
|
|
43
|
+
if (def['type'] === 'default') {
|
|
44
|
+
return { has: true, value: def['defaultValue'] };
|
|
45
|
+
}
|
|
46
|
+
return { has: false };
|
|
47
|
+
};
|
|
48
|
+
/** Check whether a schema (or its inner type) is a ZodObject. */
|
|
49
|
+
export const isObjectType = (schema) => {
|
|
50
|
+
const inner = unwrap(schema);
|
|
51
|
+
const def = inner.def;
|
|
52
|
+
return def['type'] === 'object' && 'shape' in def;
|
|
53
|
+
};
|
|
54
|
+
/** Get the shape of a ZodObject, unwrapping wrappers first. */
|
|
55
|
+
export const getObjectShape = (schema) => {
|
|
56
|
+
const inner = unwrap(schema);
|
|
57
|
+
const def = inner.def;
|
|
58
|
+
if (def['type'] !== 'object' || !('shape' in def)) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
return inner.shape;
|
|
62
|
+
};
|
|
63
|
+
/** Resolve a dot-separated path to the field schema within an object. */
|
|
64
|
+
export const resolveFieldByPath = (schema, path) => {
|
|
65
|
+
const parts = path.split('.');
|
|
66
|
+
let current = schema;
|
|
67
|
+
for (const part of parts) {
|
|
68
|
+
const shape = getObjectShape(current);
|
|
69
|
+
if (!shape?.[part]) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
current = shape[part];
|
|
73
|
+
}
|
|
74
|
+
return current;
|
|
75
|
+
};
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
// Value formatting helpers
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
/** Format a value as a quoted string or literal. */
|
|
80
|
+
export const formatValue = (value) => {
|
|
81
|
+
if (typeof value === 'string') {
|
|
82
|
+
return JSON.stringify(value);
|
|
83
|
+
}
|
|
84
|
+
return String(value);
|
|
85
|
+
};
|
|
86
|
+
/** Get the display value for a field (default or empty placeholder). */
|
|
87
|
+
export const fieldValue = (schema) => {
|
|
88
|
+
const info = getDefault(schema);
|
|
89
|
+
return info.has ? info.value : '';
|
|
90
|
+
};
|
|
91
|
+
/** Collect comment lines for a field (description + deprecation). */
|
|
92
|
+
export const fieldComments = (schema, prefix) => {
|
|
93
|
+
const lines = [];
|
|
94
|
+
const desc = getDescription(schema);
|
|
95
|
+
if (desc) {
|
|
96
|
+
lines.push(`${prefix} ${desc}`);
|
|
97
|
+
}
|
|
98
|
+
const dep = getDeprecation(schema);
|
|
99
|
+
if (dep) {
|
|
100
|
+
lines.push(`${prefix} DEPRECATED: ${dep}`);
|
|
101
|
+
}
|
|
102
|
+
return lines;
|
|
103
|
+
};
|
|
104
|
+
/** Append comment lines to an output array. */
|
|
105
|
+
export const pushComments = (lines, comments, indent) => {
|
|
106
|
+
for (const c of comments) {
|
|
107
|
+
lines.push(`${indent}${c}`);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
/** Map a Zod type name to a JSON Schema type. */
|
|
111
|
+
export const zodTypeToJsonSchema = {
|
|
112
|
+
boolean: 'boolean',
|
|
113
|
+
number: 'number',
|
|
114
|
+
string: 'string',
|
|
115
|
+
};
|
|
116
|
+
//# sourceMappingURL=helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/generate/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,M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AC,GAAG,MAAM,IAAI,IAAI,EAAE,CAAC,CAAC;IAClC,CAAC;IACD,MAAM,GAAG,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;IACnC,IAAI,GAAG,EAAE,CAAC;QACR,KAAK,CAAC,IAAI,CAAC,GAAG,MAAM,gBAAgB,GAAG,EAAE,CAAC,CAAC;IAC7C,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEF,+CAA+C;AAC/C,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,KAAe,EACf,QAA2B,EAC3B,MAAc,EACR,EAAE;IACR,KAAK,MAAM,CAAC,IAAI,QAAQ,EAAE,CAAC;QACzB,KAAK,CAAC,IAAI,CAAC,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,CAAC;IAC9B,CAAC;AACH,CAAC,CAAC;AAEF,iDAAiD;AACjD,MAAM,CAAC,MAAM,mBAAmB,GAA2B;IACzD,OAAO,EAAE,SAAS;IAClB,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;CACjB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/generate/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/generate/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Schema generation from Zod object schemas.
|
|
3
|
+
*
|
|
4
|
+
* Produces JSON Schema Draft 2020-12 with descriptions, defaults,
|
|
5
|
+
* deprecated annotations, and constraints.
|
|
6
|
+
*/
|
|
7
|
+
import type { z } from 'zod';
|
|
8
|
+
/**
|
|
9
|
+
* Generate a JSON Schema from a Zod object schema.
|
|
10
|
+
*
|
|
11
|
+
* Includes descriptions, defaults, deprecated annotations, and constraints.
|
|
12
|
+
* Produces JSON Schema Draft 2020-12.
|
|
13
|
+
*/
|
|
14
|
+
export declare const generateJsonSchema: (schema: z.ZodObject<Record<string, z.ZodType>>, options?: {
|
|
15
|
+
readonly description?: string;
|
|
16
|
+
readonly title?: string;
|
|
17
|
+
}) => Record<string, unknown>;
|
|
18
|
+
//# sourceMappingURL=json-schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-schema.d.ts","sourceRoot":"","sources":["../../src/generate/json-schema.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAsG7B;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAC7B,QAAQ,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,EAC9C,UAAU;IAAE,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,KACnE,MAAM,CAAC,MAAM,EAAE,OAAO,CAmBxB,CAAC"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { getDefault, getDeprecation, getDescription, getObjectShape, isObjectType, unwrap, zodTypeName, zodTypeToJsonSchema, } from './helpers.js';
|
|
2
|
+
// ---------------------------------------------------------------------------
|
|
3
|
+
// Helpers
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
/** Extract the JSON Schema type or enum from a Zod schema. */
|
|
6
|
+
const jsonSchemaType = (inner) => {
|
|
7
|
+
const typeName = zodTypeName(inner);
|
|
8
|
+
if (typeName === 'enum') {
|
|
9
|
+
const def = inner.def;
|
|
10
|
+
const entries = def['entries'];
|
|
11
|
+
return entries ? { enum: Object.keys(entries) } : {};
|
|
12
|
+
}
|
|
13
|
+
const mapped = zodTypeToJsonSchema[typeName];
|
|
14
|
+
return mapped ? { type: mapped } : {};
|
|
15
|
+
};
|
|
16
|
+
/** Annotation extractors for JSON Schema properties. */
|
|
17
|
+
const annotationExtractors = [
|
|
18
|
+
(s) => {
|
|
19
|
+
const desc = getDescription(s);
|
|
20
|
+
return desc ? { description: desc } : {};
|
|
21
|
+
},
|
|
22
|
+
(s) => {
|
|
23
|
+
const info = getDefault(s);
|
|
24
|
+
return info.has ? { default: info.value } : {};
|
|
25
|
+
},
|
|
26
|
+
(s) => {
|
|
27
|
+
const dep = getDeprecation(s);
|
|
28
|
+
return dep ? { deprecated: true } : {};
|
|
29
|
+
},
|
|
30
|
+
];
|
|
31
|
+
/** Extract annotations (description, default, deprecated) for JSON Schema. */
|
|
32
|
+
const jsonSchemaAnnotations = (fieldSchema) => Object.assign({}, ...annotationExtractors.map((fn) => fn(fieldSchema)));
|
|
33
|
+
/** Check if a field is required (no default and not optional). */
|
|
34
|
+
const isRequired = (fieldSchema) => {
|
|
35
|
+
const def = fieldSchema.def;
|
|
36
|
+
return def['type'] !== 'default' && def['type'] !== 'optional';
|
|
37
|
+
};
|
|
38
|
+
/** Convert a single Zod field schema to a JSON Schema property. */
|
|
39
|
+
const fieldToJsonSchema = (fieldSchema) => {
|
|
40
|
+
if (isObjectType(fieldSchema)) {
|
|
41
|
+
const nestedShape = getObjectShape(fieldSchema);
|
|
42
|
+
if (nestedShape) {
|
|
43
|
+
// oxlint-disable-next-line no-use-before-define -- mutual recursion with buildSchemaProperties
|
|
44
|
+
const { properties, required } = buildSchemaProperties(nestedShape);
|
|
45
|
+
return {
|
|
46
|
+
properties,
|
|
47
|
+
type: 'object',
|
|
48
|
+
...(required.length > 0 ? { required } : {}),
|
|
49
|
+
...jsonSchemaAnnotations(fieldSchema),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
...jsonSchemaType(unwrap(fieldSchema)),
|
|
55
|
+
...jsonSchemaAnnotations(fieldSchema),
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
/** Build properties and required arrays from a schema shape. */
|
|
59
|
+
const buildSchemaProperties = (shape) => {
|
|
60
|
+
const properties = {};
|
|
61
|
+
const required = [];
|
|
62
|
+
for (const [key, fieldSchema] of Object.entries(shape)) {
|
|
63
|
+
properties[key] = fieldToJsonSchema(fieldSchema);
|
|
64
|
+
if (isRequired(fieldSchema)) {
|
|
65
|
+
required.push(key);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return { properties, required };
|
|
69
|
+
};
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
// Public API
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
/**
|
|
74
|
+
* Generate a JSON Schema from a Zod object schema.
|
|
75
|
+
*
|
|
76
|
+
* Includes descriptions, defaults, deprecated annotations, and constraints.
|
|
77
|
+
* Produces JSON Schema Draft 2020-12.
|
|
78
|
+
*/
|
|
79
|
+
export const generateJsonSchema = (schema, options) => {
|
|
80
|
+
const { properties, required } = buildSchemaProperties(schema.shape);
|
|
81
|
+
const result = {
|
|
82
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
|
83
|
+
properties,
|
|
84
|
+
type: 'object',
|
|
85
|
+
};
|
|
86
|
+
if (options?.title) {
|
|
87
|
+
result['title'] = options.title;
|
|
88
|
+
}
|
|
89
|
+
if (options?.description) {
|
|
90
|
+
result['description'] = options.description;
|
|
91
|
+
}
|
|
92
|
+
if (required.length > 0) {
|
|
93
|
+
result['required'] = required;
|
|
94
|
+
}
|
|
95
|
+
return result;
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=json-schema.js.map
|