@fuzdev/fuz_util 0.52.0 → 0.52.1
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/zod.d.ts +76 -0
- package/dist/zod.d.ts.map +1 -0
- package/dist/zod.js +198 -0
- package/package.json +3 -3
- package/src/lib/zod.ts +218 -0
package/dist/zod.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod schema introspection utilities.
|
|
3
|
+
*
|
|
4
|
+
* Generic helpers for extracting metadata from Zod schemas.
|
|
5
|
+
* Designed for CLI argument parsing but applicable elsewhere.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import type { z } from 'zod';
|
|
10
|
+
/**
|
|
11
|
+
* Unwrap nested schema types (optional, default, nullable, etc).
|
|
12
|
+
*
|
|
13
|
+
* @param def - Zod type definition to unwrap.
|
|
14
|
+
* @returns Inner schema if wrapped, undefined otherwise.
|
|
15
|
+
*/
|
|
16
|
+
export declare const zod_to_subschema: (def: z.core.$ZodTypeDef) => z.ZodType | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Get the description from a schema's metadata, unwrapping if needed.
|
|
19
|
+
*
|
|
20
|
+
* @param schema - Zod schema to extract description from.
|
|
21
|
+
* @returns Description string or null if not found.
|
|
22
|
+
*/
|
|
23
|
+
export declare const zod_to_schema_description: (schema: z.ZodType) => string | null;
|
|
24
|
+
/**
|
|
25
|
+
* Get the default value from a schema, unwrapping if needed.
|
|
26
|
+
*
|
|
27
|
+
* @param schema - Zod schema to extract default from.
|
|
28
|
+
* @returns Default value or undefined.
|
|
29
|
+
*/
|
|
30
|
+
export declare const zod_to_schema_default: (schema: z.ZodType) => unknown;
|
|
31
|
+
/**
|
|
32
|
+
* Get aliases from a schema's metadata, unwrapping if needed.
|
|
33
|
+
*
|
|
34
|
+
* @param schema - Zod schema to extract aliases from.
|
|
35
|
+
* @returns Array of alias strings.
|
|
36
|
+
*/
|
|
37
|
+
export declare const zod_to_schema_aliases: (schema: z.ZodType) => Array<string>;
|
|
38
|
+
/**
|
|
39
|
+
* Get the type string for a schema, suitable for display.
|
|
40
|
+
*
|
|
41
|
+
* @param schema - Zod schema to get type string for.
|
|
42
|
+
* @returns Human-readable type string.
|
|
43
|
+
*/
|
|
44
|
+
export declare const zod_to_schema_type_string: (schema: z.ZodType) => string;
|
|
45
|
+
/**
|
|
46
|
+
* Format a value for display in help text.
|
|
47
|
+
*
|
|
48
|
+
* @param value - Value to format.
|
|
49
|
+
* @returns Formatted string representation.
|
|
50
|
+
*/
|
|
51
|
+
export declare const zod_format_value: (value: unknown) => string;
|
|
52
|
+
/**
|
|
53
|
+
* Property extracted from an object schema.
|
|
54
|
+
*/
|
|
55
|
+
export interface ZodSchemaProperty {
|
|
56
|
+
name: string;
|
|
57
|
+
type: string;
|
|
58
|
+
description: string;
|
|
59
|
+
default: unknown;
|
|
60
|
+
aliases: Array<string>;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Extract properties from a Zod object schema.
|
|
64
|
+
*
|
|
65
|
+
* @param schema - Zod object schema to extract from.
|
|
66
|
+
* @returns Array of property definitions.
|
|
67
|
+
*/
|
|
68
|
+
export declare const zod_to_schema_properties: (schema: z.ZodType) => Array<ZodSchemaProperty>;
|
|
69
|
+
/**
|
|
70
|
+
* Get all property names and their aliases from an object schema.
|
|
71
|
+
*
|
|
72
|
+
* @param schema - Zod object schema.
|
|
73
|
+
* @returns Set of all names and aliases.
|
|
74
|
+
*/
|
|
75
|
+
export declare const zod_to_schema_names_with_aliases: (schema: z.ZodType) => Set<string>;
|
|
76
|
+
//# sourceMappingURL=zod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/zod.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AAM3B;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,GAAI,KAAK,CAAC,CAAC,IAAI,CAAC,WAAW,KAAG,CAAC,CAAC,OAAO,GAAG,SAStE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,yBAAyB,GAAI,QAAQ,CAAC,CAAC,OAAO,KAAG,MAAM,GAAG,IAUtE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAAI,QAAQ,CAAC,CAAC,OAAO,KAAG,OAUzD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAAI,QAAQ,CAAC,CAAC,OAAO,KAAG,KAAK,CAAC,MAAM,CAUrE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,yBAAyB,GAAI,QAAQ,CAAC,CAAC,OAAO,KAAG,MA4C7D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,GAAI,OAAO,OAAO,KAAG,MAQjD,CAAC;AAMF;;GAEG;AACH,MAAM,WAAW,iBAAiB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;CACvB;AAED;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB,GAAI,QAAQ,CAAC,CAAC,OAAO,KAAG,KAAK,CAAC,iBAAiB,CAuBnF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,gCAAgC,GAAI,QAAQ,CAAC,CAAC,OAAO,KAAG,GAAG,CAAC,MAAM,CAW9E,CAAC"}
|
package/dist/zod.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod schema introspection utilities.
|
|
3
|
+
*
|
|
4
|
+
* Generic helpers for extracting metadata from Zod schemas.
|
|
5
|
+
* Designed for CLI argument parsing but applicable elsewhere.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
//
|
|
10
|
+
// Schema Introspection
|
|
11
|
+
//
|
|
12
|
+
/**
|
|
13
|
+
* Unwrap nested schema types (optional, default, nullable, etc).
|
|
14
|
+
*
|
|
15
|
+
* @param def - Zod type definition to unwrap.
|
|
16
|
+
* @returns Inner schema if wrapped, undefined otherwise.
|
|
17
|
+
*/
|
|
18
|
+
export const zod_to_subschema = (def) => {
|
|
19
|
+
if ('innerType' in def) {
|
|
20
|
+
return def.innerType;
|
|
21
|
+
}
|
|
22
|
+
else if ('in' in def) {
|
|
23
|
+
return def.in;
|
|
24
|
+
}
|
|
25
|
+
else if ('schema' in def) {
|
|
26
|
+
return def.schema;
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Get the description from a schema's metadata, unwrapping if needed.
|
|
32
|
+
*
|
|
33
|
+
* @param schema - Zod schema to extract description from.
|
|
34
|
+
* @returns Description string or null if not found.
|
|
35
|
+
*/
|
|
36
|
+
export const zod_to_schema_description = (schema) => {
|
|
37
|
+
const meta = schema.meta();
|
|
38
|
+
if (meta?.description) {
|
|
39
|
+
return meta.description;
|
|
40
|
+
}
|
|
41
|
+
const subschema = zod_to_subschema(schema.def);
|
|
42
|
+
if (subschema) {
|
|
43
|
+
return zod_to_schema_description(subschema);
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Get the default value from a schema, unwrapping if needed.
|
|
49
|
+
*
|
|
50
|
+
* @param schema - Zod schema to extract default from.
|
|
51
|
+
* @returns Default value or undefined.
|
|
52
|
+
*/
|
|
53
|
+
export const zod_to_schema_default = (schema) => {
|
|
54
|
+
const { def } = schema._zod;
|
|
55
|
+
if ('defaultValue' in def) {
|
|
56
|
+
return def.defaultValue;
|
|
57
|
+
}
|
|
58
|
+
const subschema = zod_to_subschema(def);
|
|
59
|
+
if (subschema) {
|
|
60
|
+
return zod_to_schema_default(subschema);
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Get aliases from a schema's metadata, unwrapping if needed.
|
|
66
|
+
*
|
|
67
|
+
* @param schema - Zod schema to extract aliases from.
|
|
68
|
+
* @returns Array of alias strings.
|
|
69
|
+
*/
|
|
70
|
+
export const zod_to_schema_aliases = (schema) => {
|
|
71
|
+
const meta = schema.meta();
|
|
72
|
+
if (meta?.aliases) {
|
|
73
|
+
return meta.aliases;
|
|
74
|
+
}
|
|
75
|
+
const subschema = zod_to_subschema(schema.def);
|
|
76
|
+
if (subschema) {
|
|
77
|
+
return zod_to_schema_aliases(subschema);
|
|
78
|
+
}
|
|
79
|
+
return [];
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Get the type string for a schema, suitable for display.
|
|
83
|
+
*
|
|
84
|
+
* @param schema - Zod schema to get type string for.
|
|
85
|
+
* @returns Human-readable type string.
|
|
86
|
+
*/
|
|
87
|
+
export const zod_to_schema_type_string = (schema) => {
|
|
88
|
+
const { def } = schema._zod;
|
|
89
|
+
switch (def.type) {
|
|
90
|
+
case 'string':
|
|
91
|
+
return 'string';
|
|
92
|
+
case 'number':
|
|
93
|
+
return 'number';
|
|
94
|
+
case 'int':
|
|
95
|
+
return 'int';
|
|
96
|
+
case 'boolean':
|
|
97
|
+
return 'boolean';
|
|
98
|
+
case 'bigint':
|
|
99
|
+
return 'bigint';
|
|
100
|
+
case 'null':
|
|
101
|
+
return 'null';
|
|
102
|
+
case 'undefined':
|
|
103
|
+
return 'undefined';
|
|
104
|
+
case 'any':
|
|
105
|
+
return 'any';
|
|
106
|
+
case 'unknown':
|
|
107
|
+
return 'unknown';
|
|
108
|
+
case 'array':
|
|
109
|
+
return 'Array<string>';
|
|
110
|
+
case 'enum':
|
|
111
|
+
return schema.options
|
|
112
|
+
.map((v) => `'${v}'`)
|
|
113
|
+
.join(' | ');
|
|
114
|
+
case 'literal':
|
|
115
|
+
return def.values
|
|
116
|
+
.map((v) => zod_format_value(v))
|
|
117
|
+
.join(' | ');
|
|
118
|
+
case 'nullable': {
|
|
119
|
+
const subschema = zod_to_subschema(def);
|
|
120
|
+
return subschema ? zod_to_schema_type_string(subschema) + ' | null' : 'nullable';
|
|
121
|
+
}
|
|
122
|
+
case 'optional': {
|
|
123
|
+
const subschema = zod_to_subschema(def);
|
|
124
|
+
return subschema ? zod_to_schema_type_string(subschema) + ' | undefined' : 'optional';
|
|
125
|
+
}
|
|
126
|
+
default: {
|
|
127
|
+
const subschema = zod_to_subschema(def);
|
|
128
|
+
return subschema ? zod_to_schema_type_string(subschema) : def.type;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* Format a value for display in help text.
|
|
134
|
+
*
|
|
135
|
+
* @param value - Value to format.
|
|
136
|
+
* @returns Formatted string representation.
|
|
137
|
+
*/
|
|
138
|
+
export const zod_format_value = (value) => {
|
|
139
|
+
if (value === undefined)
|
|
140
|
+
return '';
|
|
141
|
+
if (value === null)
|
|
142
|
+
return 'null';
|
|
143
|
+
if (typeof value === 'string')
|
|
144
|
+
return `'${value}'`;
|
|
145
|
+
if (Array.isArray(value))
|
|
146
|
+
return '[]';
|
|
147
|
+
if (typeof value === 'object')
|
|
148
|
+
return JSON.stringify(value);
|
|
149
|
+
if (typeof value === 'boolean' || typeof value === 'number')
|
|
150
|
+
return String(value);
|
|
151
|
+
return '';
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Extract properties from a Zod object schema.
|
|
155
|
+
*
|
|
156
|
+
* @param schema - Zod object schema to extract from.
|
|
157
|
+
* @returns Array of property definitions.
|
|
158
|
+
*/
|
|
159
|
+
export const zod_to_schema_properties = (schema) => {
|
|
160
|
+
const { def } = schema;
|
|
161
|
+
if (!('shape' in def)) {
|
|
162
|
+
return [];
|
|
163
|
+
}
|
|
164
|
+
const shape = def.shape;
|
|
165
|
+
const properties = [];
|
|
166
|
+
for (const name in shape) {
|
|
167
|
+
// Skip no- prefixed fields (used for boolean negation)
|
|
168
|
+
if ('no-' + name in shape)
|
|
169
|
+
continue;
|
|
170
|
+
const field = shape[name];
|
|
171
|
+
properties.push({
|
|
172
|
+
name,
|
|
173
|
+
type: zod_to_schema_type_string(field),
|
|
174
|
+
description: zod_to_schema_description(field) ?? '',
|
|
175
|
+
default: zod_to_schema_default(field),
|
|
176
|
+
aliases: zod_to_schema_aliases(field),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
return properties;
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* Get all property names and their aliases from an object schema.
|
|
183
|
+
*
|
|
184
|
+
* @param schema - Zod object schema.
|
|
185
|
+
* @returns Set of all names and aliases.
|
|
186
|
+
*/
|
|
187
|
+
export const zod_to_schema_names_with_aliases = (schema) => {
|
|
188
|
+
const names = new Set();
|
|
189
|
+
for (const prop of zod_to_schema_properties(schema)) {
|
|
190
|
+
if (prop.name !== '_') {
|
|
191
|
+
names.add(prop.name);
|
|
192
|
+
for (const alias of prop.aliases) {
|
|
193
|
+
names.add(alias);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return names;
|
|
198
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fuzdev/fuz_util",
|
|
3
|
-
"version": "0.52.
|
|
3
|
+
"version": "0.52.1",
|
|
4
4
|
"description": "utility belt for JS",
|
|
5
5
|
"glyph": "🦕",
|
|
6
6
|
"logo": "logo.svg",
|
|
@@ -70,9 +70,9 @@
|
|
|
70
70
|
"devDependencies": {
|
|
71
71
|
"@changesets/changelog-git": "^0.2.1",
|
|
72
72
|
"@fuzdev/fuz_code": "^0.45.1",
|
|
73
|
-
"@fuzdev/fuz_css": "^0.
|
|
73
|
+
"@fuzdev/fuz_css": "^0.53.0",
|
|
74
74
|
"@fuzdev/fuz_ui": "^0.184.0",
|
|
75
|
-
"@fuzdev/gro": "^0.
|
|
75
|
+
"@fuzdev/gro": "^0.195.0",
|
|
76
76
|
"@jridgewell/trace-mapping": "^0.3.31",
|
|
77
77
|
"@ryanatkn/eslint-config": "^0.9.0",
|
|
78
78
|
"@sveltejs/adapter-static": "^3.0.10",
|
package/src/lib/zod.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod schema introspection utilities.
|
|
3
|
+
*
|
|
4
|
+
* Generic helpers for extracting metadata from Zod schemas.
|
|
5
|
+
* Designed for CLI argument parsing but applicable elsewhere.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type {z} from 'zod';
|
|
11
|
+
|
|
12
|
+
//
|
|
13
|
+
// Schema Introspection
|
|
14
|
+
//
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Unwrap nested schema types (optional, default, nullable, etc).
|
|
18
|
+
*
|
|
19
|
+
* @param def - Zod type definition to unwrap.
|
|
20
|
+
* @returns Inner schema if wrapped, undefined otherwise.
|
|
21
|
+
*/
|
|
22
|
+
export const zod_to_subschema = (def: z.core.$ZodTypeDef): z.ZodType | undefined => {
|
|
23
|
+
if ('innerType' in def) {
|
|
24
|
+
return def.innerType as z.ZodType;
|
|
25
|
+
} else if ('in' in def) {
|
|
26
|
+
return def.in as z.ZodType;
|
|
27
|
+
} else if ('schema' in def) {
|
|
28
|
+
return def.schema as z.ZodType;
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Get the description from a schema's metadata, unwrapping if needed.
|
|
35
|
+
*
|
|
36
|
+
* @param schema - Zod schema to extract description from.
|
|
37
|
+
* @returns Description string or null if not found.
|
|
38
|
+
*/
|
|
39
|
+
export const zod_to_schema_description = (schema: z.ZodType): string | null => {
|
|
40
|
+
const meta = schema.meta();
|
|
41
|
+
if (meta?.description) {
|
|
42
|
+
return meta.description;
|
|
43
|
+
}
|
|
44
|
+
const subschema = zod_to_subschema(schema.def);
|
|
45
|
+
if (subschema) {
|
|
46
|
+
return zod_to_schema_description(subschema);
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Get the default value from a schema, unwrapping if needed.
|
|
53
|
+
*
|
|
54
|
+
* @param schema - Zod schema to extract default from.
|
|
55
|
+
* @returns Default value or undefined.
|
|
56
|
+
*/
|
|
57
|
+
export const zod_to_schema_default = (schema: z.ZodType): unknown => {
|
|
58
|
+
const {def} = schema._zod;
|
|
59
|
+
if ('defaultValue' in def) {
|
|
60
|
+
return def.defaultValue;
|
|
61
|
+
}
|
|
62
|
+
const subschema = zod_to_subschema(def);
|
|
63
|
+
if (subschema) {
|
|
64
|
+
return zod_to_schema_default(subschema);
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get aliases from a schema's metadata, unwrapping if needed.
|
|
71
|
+
*
|
|
72
|
+
* @param schema - Zod schema to extract aliases from.
|
|
73
|
+
* @returns Array of alias strings.
|
|
74
|
+
*/
|
|
75
|
+
export const zod_to_schema_aliases = (schema: z.ZodType): Array<string> => {
|
|
76
|
+
const meta = schema.meta();
|
|
77
|
+
if (meta?.aliases) {
|
|
78
|
+
return meta.aliases as Array<string>;
|
|
79
|
+
}
|
|
80
|
+
const subschema = zod_to_subschema(schema.def);
|
|
81
|
+
if (subschema) {
|
|
82
|
+
return zod_to_schema_aliases(subschema);
|
|
83
|
+
}
|
|
84
|
+
return [];
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get the type string for a schema, suitable for display.
|
|
89
|
+
*
|
|
90
|
+
* @param schema - Zod schema to get type string for.
|
|
91
|
+
* @returns Human-readable type string.
|
|
92
|
+
*/
|
|
93
|
+
export const zod_to_schema_type_string = (schema: z.ZodType): string => {
|
|
94
|
+
const {def} = schema._zod;
|
|
95
|
+
switch (def.type) {
|
|
96
|
+
case 'string':
|
|
97
|
+
return 'string';
|
|
98
|
+
case 'number':
|
|
99
|
+
return 'number';
|
|
100
|
+
case 'int':
|
|
101
|
+
return 'int';
|
|
102
|
+
case 'boolean':
|
|
103
|
+
return 'boolean';
|
|
104
|
+
case 'bigint':
|
|
105
|
+
return 'bigint';
|
|
106
|
+
case 'null':
|
|
107
|
+
return 'null';
|
|
108
|
+
case 'undefined':
|
|
109
|
+
return 'undefined';
|
|
110
|
+
case 'any':
|
|
111
|
+
return 'any';
|
|
112
|
+
case 'unknown':
|
|
113
|
+
return 'unknown';
|
|
114
|
+
case 'array':
|
|
115
|
+
return 'Array<string>';
|
|
116
|
+
case 'enum':
|
|
117
|
+
return (schema as unknown as {options: Array<string>}).options
|
|
118
|
+
.map((v) => `'${v}'`)
|
|
119
|
+
.join(' | ');
|
|
120
|
+
case 'literal':
|
|
121
|
+
return (def as unknown as {values: Array<unknown>}).values
|
|
122
|
+
.map((v) => zod_format_value(v))
|
|
123
|
+
.join(' | ');
|
|
124
|
+
case 'nullable': {
|
|
125
|
+
const subschema = zod_to_subschema(def);
|
|
126
|
+
return subschema ? zod_to_schema_type_string(subschema) + ' | null' : 'nullable';
|
|
127
|
+
}
|
|
128
|
+
case 'optional': {
|
|
129
|
+
const subschema = zod_to_subschema(def);
|
|
130
|
+
return subschema ? zod_to_schema_type_string(subschema) + ' | undefined' : 'optional';
|
|
131
|
+
}
|
|
132
|
+
default: {
|
|
133
|
+
const subschema = zod_to_subschema(def);
|
|
134
|
+
return subschema ? zod_to_schema_type_string(subschema) : def.type;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Format a value for display in help text.
|
|
141
|
+
*
|
|
142
|
+
* @param value - Value to format.
|
|
143
|
+
* @returns Formatted string representation.
|
|
144
|
+
*/
|
|
145
|
+
export const zod_format_value = (value: unknown): string => {
|
|
146
|
+
if (value === undefined) return '';
|
|
147
|
+
if (value === null) return 'null';
|
|
148
|
+
if (typeof value === 'string') return `'${value}'`;
|
|
149
|
+
if (Array.isArray(value)) return '[]';
|
|
150
|
+
if (typeof value === 'object') return JSON.stringify(value);
|
|
151
|
+
if (typeof value === 'boolean' || typeof value === 'number') return String(value);
|
|
152
|
+
return '';
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
//
|
|
156
|
+
// Object Schema Helpers
|
|
157
|
+
//
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Property extracted from an object schema.
|
|
161
|
+
*/
|
|
162
|
+
export interface ZodSchemaProperty {
|
|
163
|
+
name: string;
|
|
164
|
+
type: string;
|
|
165
|
+
description: string;
|
|
166
|
+
default: unknown;
|
|
167
|
+
aliases: Array<string>;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Extract properties from a Zod object schema.
|
|
172
|
+
*
|
|
173
|
+
* @param schema - Zod object schema to extract from.
|
|
174
|
+
* @returns Array of property definitions.
|
|
175
|
+
*/
|
|
176
|
+
export const zod_to_schema_properties = (schema: z.ZodType): Array<ZodSchemaProperty> => {
|
|
177
|
+
const {def} = schema;
|
|
178
|
+
|
|
179
|
+
if (!('shape' in def)) {
|
|
180
|
+
return [];
|
|
181
|
+
}
|
|
182
|
+
const shape = (def as z.core.$ZodObjectDef).shape;
|
|
183
|
+
|
|
184
|
+
const properties: Array<ZodSchemaProperty> = [];
|
|
185
|
+
for (const name in shape) {
|
|
186
|
+
// Skip no- prefixed fields (used for boolean negation)
|
|
187
|
+
if ('no-' + name in shape) continue;
|
|
188
|
+
|
|
189
|
+
const field = shape[name] as z.ZodType;
|
|
190
|
+
properties.push({
|
|
191
|
+
name,
|
|
192
|
+
type: zod_to_schema_type_string(field),
|
|
193
|
+
description: zod_to_schema_description(field) ?? '',
|
|
194
|
+
default: zod_to_schema_default(field),
|
|
195
|
+
aliases: zod_to_schema_aliases(field),
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
return properties;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Get all property names and their aliases from an object schema.
|
|
203
|
+
*
|
|
204
|
+
* @param schema - Zod object schema.
|
|
205
|
+
* @returns Set of all names and aliases.
|
|
206
|
+
*/
|
|
207
|
+
export const zod_to_schema_names_with_aliases = (schema: z.ZodType): Set<string> => {
|
|
208
|
+
const names: Set<string> = new Set();
|
|
209
|
+
for (const prop of zod_to_schema_properties(schema)) {
|
|
210
|
+
if (prop.name !== '_') {
|
|
211
|
+
names.add(prop.name);
|
|
212
|
+
for (const alias of prop.aliases) {
|
|
213
|
+
names.add(alias);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return names;
|
|
218
|
+
};
|