@politty/zod 0.1.2 → 0.2.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/README.md +31 -0
- package/dist/{arg-registry-Be4GJADw.d.ts → arg-registry-BzQ-y-e0.d.ts} +2 -2
- package/dist/augment.d.ts +1 -1
- package/dist/augment.js +1 -1
- package/dist/cli-main-B4FX3h4J.js +1 -0
- package/dist/cli-main-CjYk6tAX.js +2 -0
- package/dist/cli-run-DN55A0JZ.js +1 -0
- package/dist/cli.js +1 -15
- package/dist/command-Mbdt0bmN.js +1 -0
- package/dist/compile-cache-VuUbFWIo.js +1 -0
- package/dist/compile-cache.js +1 -3
- package/dist/completion-D8DMrPqg.js +116 -0
- package/dist/completion-DUMcvXkT.js +1 -0
- package/dist/completion.d.ts +87 -7
- package/dist/completion.js +1 -4
- package/dist/docs.d.ts +1 -1
- package/dist/docs.js +84 -3063
- package/dist/dynamic-CMtee4tD.js +1 -0
- package/dist/dynamic-CrnlveHj.js +6 -0
- package/dist/field-meta-COGya7xp.js +1 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -27
- package/dist/log-collector-DICKib75.js +1 -0
- package/dist/logger-CJsyJ8sb.js +1 -0
- package/dist/prompt-CqmGq1_N.js +1 -0
- package/dist/prompt-clack.d.ts +1 -1
- package/dist/prompt-clack.js +1 -32
- package/dist/prompt-inquirer.d.ts +1 -1
- package/dist/prompt-inquirer.js +1 -47
- package/dist/prompt.d.ts +1 -1
- package/dist/prompt.js +1 -4
- package/dist/register-C1WbbeYH.js +1 -0
- package/dist/runner-BIdcntiz.js +29 -0
- package/dist/runner-Chaf_5lK.js +1 -0
- package/dist/schema-BiUP_KyV.js +1 -0
- package/dist/schema-extractor-DU0Vuhbd.js +1 -0
- package/dist/skill.d.ts +1 -1
- package/dist/skill.js +2 -1837
- package/dist/subcommand-router-D8GTMXYL.js +1 -0
- package/dist/{index-B4NylQOW.d.ts → with-completion-command-CmVdHFHX.d.ts} +3 -74
- package/dist/with-completion-command-DDohm8U6.js +3 -0
- package/package.json +3 -3
- package/dist/cli-main-CAa5KwCo.js +0 -314
- package/dist/cli-main-DIRMduj1.js +0 -3
- package/dist/cli-run-B--tZW_I.js +0 -8
- package/dist/command-k-4yAz4J.js +0 -42
- package/dist/compile-cache-BC65o7MH.js +0 -103
- package/dist/completion-Ot2qpNNw.js +0 -5616
- package/dist/field-meta-CO38jOvl.js +0 -148
- package/dist/log-collector-CoUkLVJB.js +0 -114
- package/dist/logger-i_bb-Jhc.js +0 -133
- package/dist/prompt-BjIZThsH.js +0 -169
- package/dist/register-DdcsbNwM.js +0 -440
- package/dist/runner-Bz3arOqh.js +0 -3051
- package/dist/runner-Cs-Mz9JN.js +0 -3
- package/dist/schema-extractor-CVNDs07l.js +0 -250
- package/dist/src-D4QGGgW1.js +0 -6
- package/dist/src-DhQVU2hG.js +0 -12
- package/dist/subcommand-router-Cskpofdk.js +0 -134
|
@@ -1,440 +0,0 @@
|
|
|
1
|
-
import { n as resolveFieldMeta, o as registerValidatorAdapter } from "./field-meta-CO38jOvl.js";
|
|
2
|
-
|
|
3
|
-
//#region ../core/src/core/arg-registry.ts
|
|
4
|
-
/**
|
|
5
|
-
* Custom registry for politty argument metadata, keyed by schema identity.
|
|
6
|
-
* This avoids polluting Zod's GlobalMeta, and — unlike `z.registry()` — is
|
|
7
|
-
* validator-neutral: any schema object (zod, valibot, internal descriptors)
|
|
8
|
-
* can carry arg metadata without politty importing the schema library.
|
|
9
|
-
*/
|
|
10
|
-
const argRegistry = /* @__PURE__ */ new WeakMap();
|
|
11
|
-
function arg(schema, meta) {
|
|
12
|
-
if (meta) argRegistry.set(schema, meta);
|
|
13
|
-
return schema;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Get metadata for a schema from the registry
|
|
17
|
-
*
|
|
18
|
-
* @param schema - The schema object used as the registry key
|
|
19
|
-
* @returns The metadata if registered, undefined otherwise
|
|
20
|
-
*/
|
|
21
|
-
function getArgMeta$1(schema) {
|
|
22
|
-
return argRegistry.get(schema);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
//#endregion
|
|
26
|
-
//#region src/adapter.ts
|
|
27
|
-
/**
|
|
28
|
-
* Zod validator adapter: implements politty's neutral `ValidatorAdapter`
|
|
29
|
-
* contract against zod v4's schema representation.
|
|
30
|
-
*
|
|
31
|
-
* All introspection here is structural (`_def` / `.def` access, method
|
|
32
|
-
* calls on the schema object) — this module never imports zod at runtime,
|
|
33
|
-
* so it stays out of the user's startup path until a zod schema actually
|
|
34
|
-
* flows through it.
|
|
35
|
-
*/
|
|
36
|
-
/**
|
|
37
|
-
* Get ArgMeta from both the custom registry and Zod's _def
|
|
38
|
-
* Priority: custom registry > _def.argMeta
|
|
39
|
-
*/
|
|
40
|
-
function getArgMeta(schema) {
|
|
41
|
-
const fromRegistry = getArgMeta$1(schema);
|
|
42
|
-
if (fromRegistry) return fromRegistry;
|
|
43
|
-
if (typeof schema.meta === "function") {
|
|
44
|
-
const meta = schema.meta();
|
|
45
|
-
if (meta && typeof meta === "object") return meta;
|
|
46
|
-
}
|
|
47
|
-
const def = schema._def;
|
|
48
|
-
if (def?.argMeta) return def.argMeta;
|
|
49
|
-
if (def?.meta) return def.meta;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Get the type name from a zod schema (v4 compatible)
|
|
53
|
-
*/
|
|
54
|
-
function getTypeName(schema) {
|
|
55
|
-
const s = schema;
|
|
56
|
-
return s.def?.type ?? s._def?.type ?? s.type;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Detect unknown keys handling mode from a Zod object schema
|
|
60
|
-
*
|
|
61
|
-
* In Zod v4:
|
|
62
|
-
* - Default (strip): _def.catchall is undefined
|
|
63
|
-
* - strict: _def.catchall is ZodNever (type = "never")
|
|
64
|
-
* - passthrough: _def.catchall is ZodUnknown (type = "unknown")
|
|
65
|
-
*/
|
|
66
|
-
function getUnknownKeysMode(schema) {
|
|
67
|
-
const s = unwrapSchema(schema);
|
|
68
|
-
const catchall = (s.def ?? s._def)?.catchall;
|
|
69
|
-
if (!catchall) return "strip";
|
|
70
|
-
const catchallType = getTypeName(catchall);
|
|
71
|
-
if (catchallType === "never") return "strict";
|
|
72
|
-
if (catchallType === "unknown" || catchallType === "any") return "passthrough";
|
|
73
|
-
return "strip";
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Get the inner schema, unwrapping optional, nullable, default, etc.
|
|
77
|
-
*/
|
|
78
|
-
function unwrapSchema(schema) {
|
|
79
|
-
const typeName = getTypeName(schema);
|
|
80
|
-
const s = schema;
|
|
81
|
-
const def = s.def ?? s._def;
|
|
82
|
-
if (typeName === "optional" || typeName === "nullable" || typeName === "default") {
|
|
83
|
-
const innerSchema = def?.innerType;
|
|
84
|
-
if (innerSchema) return unwrapSchema(innerSchema);
|
|
85
|
-
}
|
|
86
|
-
if (typeName === "pipe") {
|
|
87
|
-
const innerSchema = def?.in ?? def?.schema;
|
|
88
|
-
if (innerSchema) return unwrapSchema(innerSchema);
|
|
89
|
-
}
|
|
90
|
-
return schema;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Detect the base type of a schema
|
|
94
|
-
*/
|
|
95
|
-
function detectType(schema) {
|
|
96
|
-
switch (getTypeName(unwrapSchema(schema))) {
|
|
97
|
-
case "string":
|
|
98
|
-
case "enum": return "string";
|
|
99
|
-
case "number":
|
|
100
|
-
case "int": return "number";
|
|
101
|
-
case "boolean": return "boolean";
|
|
102
|
-
case "array": return "array";
|
|
103
|
-
default: return "unknown";
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Extract enum values from a schema if it's an enum type
|
|
108
|
-
*
|
|
109
|
-
* @param schema - The Zod schema to extract enum values from
|
|
110
|
-
* @returns Array of enum values if schema is an enum, undefined otherwise
|
|
111
|
-
*/
|
|
112
|
-
function extractEnumValues(schema) {
|
|
113
|
-
const innerSchema = unwrapSchema(schema);
|
|
114
|
-
const typeName = getTypeName(innerSchema);
|
|
115
|
-
const s = innerSchema;
|
|
116
|
-
const def = s.def ?? s._def;
|
|
117
|
-
if (typeName === "enum") {
|
|
118
|
-
const entries = def?.entries;
|
|
119
|
-
if (entries && typeof entries === "object") return Object.values(entries);
|
|
120
|
-
const values = def?.values;
|
|
121
|
-
if (Array.isArray(values)) return values;
|
|
122
|
-
const options = s.options;
|
|
123
|
-
if (Array.isArray(options)) return options;
|
|
124
|
-
}
|
|
125
|
-
if (typeName === "array") {
|
|
126
|
-
const element = def?.element;
|
|
127
|
-
if (element) return extractEnumValues(element);
|
|
128
|
-
}
|
|
129
|
-
if (typeName === "union") {
|
|
130
|
-
const options = def?.options;
|
|
131
|
-
if (Array.isArray(options)) {
|
|
132
|
-
const literalValues = [];
|
|
133
|
-
for (const option of options) if (getTypeName(option) === "literal") {
|
|
134
|
-
const optionDef = option.def ?? option._def;
|
|
135
|
-
const value = optionDef?.value;
|
|
136
|
-
const values = optionDef?.values;
|
|
137
|
-
const literalValue = value ?? values?.[0];
|
|
138
|
-
if (typeof literalValue === "string") literalValues.push(literalValue);
|
|
139
|
-
}
|
|
140
|
-
if (literalValues.length === options.length && literalValues.length > 0) return literalValues;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Check if a schema is required (not optional or has default)
|
|
146
|
-
*
|
|
147
|
-
* Note: We only check isOptional(), not isNullable(), because CLI arguments
|
|
148
|
-
* are either present (string value) or absent (undefined), never null.
|
|
149
|
-
* Also, some coerce types incorrectly report isNullable()=true.
|
|
150
|
-
*/
|
|
151
|
-
function isRequired(schema) {
|
|
152
|
-
return !schema.isOptional();
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Extract default value from schema if present
|
|
156
|
-
*/
|
|
157
|
-
function extractDefaultValue(schema) {
|
|
158
|
-
const typeName = getTypeName(schema);
|
|
159
|
-
const s = schema;
|
|
160
|
-
const def = s.def ?? s._def;
|
|
161
|
-
if (typeName === "default") {
|
|
162
|
-
const defaultValue = def?.defaultValue;
|
|
163
|
-
if (typeof defaultValue === "function") return defaultValue();
|
|
164
|
-
return defaultValue;
|
|
165
|
-
}
|
|
166
|
-
if (typeName === "optional" || typeName === "nullable") {
|
|
167
|
-
const innerSchema = def?.innerType;
|
|
168
|
-
if (innerSchema) return extractDefaultValue(innerSchema);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Extract description from schema
|
|
173
|
-
*/
|
|
174
|
-
function extractDescription(schema) {
|
|
175
|
-
if (schema.description) return schema.description;
|
|
176
|
-
const typeName = getTypeName(schema);
|
|
177
|
-
const s = schema;
|
|
178
|
-
const def = s.def ?? s._def;
|
|
179
|
-
if (typeName === "optional" || typeName === "nullable" || typeName === "default") {
|
|
180
|
-
const innerSchema = def?.innerType;
|
|
181
|
-
if (innerSchema) return extractDescription(innerSchema);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Resolve field metadata from a zod field schema. The zod-specific
|
|
186
|
-
* introspection happens here; the shared alias/negation policy lives in
|
|
187
|
-
* `resolveFieldMeta` (adapter/field-meta.ts).
|
|
188
|
-
*/
|
|
189
|
-
function resolveZodFieldMeta(name, schema) {
|
|
190
|
-
const argMeta = getArgMeta(schema) ?? getArgMeta(unwrapSchema(schema));
|
|
191
|
-
return resolveFieldMeta(name, {
|
|
192
|
-
argMeta,
|
|
193
|
-
description: extractDescription(schema),
|
|
194
|
-
required: isRequired(schema),
|
|
195
|
-
defaultValue: extractDefaultValue(schema),
|
|
196
|
-
type: detectType(schema),
|
|
197
|
-
enumValues: extractEnumValues(schema),
|
|
198
|
-
schema
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Get shape from a ZodObject
|
|
203
|
-
*/
|
|
204
|
-
function getObjectShape(schema) {
|
|
205
|
-
const s = schema;
|
|
206
|
-
return (s.def ?? s._def)?.shape ?? s.shape ?? {};
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Extract fields from a ZodObject
|
|
210
|
-
*/
|
|
211
|
-
function extractFromObject(schema) {
|
|
212
|
-
const shape = getObjectShape(schema);
|
|
213
|
-
return Object.entries(shape).map(([name, fieldSchema]) => resolveZodFieldMeta(name, fieldSchema));
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Extract fields from a discriminated union
|
|
217
|
-
*/
|
|
218
|
-
function extractFromDiscriminatedUnion(schema) {
|
|
219
|
-
const s = schema;
|
|
220
|
-
const def = s.def ?? s._def;
|
|
221
|
-
const discriminator = def?.discriminator ?? "";
|
|
222
|
-
const options = def?.options ?? [];
|
|
223
|
-
const allFieldsMap = /* @__PURE__ */ new Map();
|
|
224
|
-
const variants = [];
|
|
225
|
-
for (const option of options) {
|
|
226
|
-
const shape = getObjectShape(option);
|
|
227
|
-
const variantFields = [];
|
|
228
|
-
let discriminatorValue = "";
|
|
229
|
-
const discriminatorSchema = shape[discriminator];
|
|
230
|
-
if (discriminatorSchema) {
|
|
231
|
-
const typeName = getTypeName(discriminatorSchema);
|
|
232
|
-
if (typeName === "literal") {
|
|
233
|
-
const litDef = discriminatorSchema.def ?? discriminatorSchema._def;
|
|
234
|
-
const value = litDef?.value;
|
|
235
|
-
const values = litDef?.values;
|
|
236
|
-
discriminatorValue = String(value ?? values?.[0] ?? "");
|
|
237
|
-
} else if (typeName === "enum") {
|
|
238
|
-
const enumValues = extractEnumValues(discriminatorSchema);
|
|
239
|
-
if (enumValues && enumValues.length === 1) discriminatorValue = enumValues[0];
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
for (const [name, fieldSchema] of Object.entries(shape)) {
|
|
243
|
-
const fieldMeta = resolveZodFieldMeta(name, fieldSchema);
|
|
244
|
-
variantFields.push(fieldMeta);
|
|
245
|
-
if (!allFieldsMap.has(name)) allFieldsMap.set(name, fieldMeta);
|
|
246
|
-
}
|
|
247
|
-
const variantDescription = extractDescription(option);
|
|
248
|
-
variants.push({
|
|
249
|
-
discriminatorValue,
|
|
250
|
-
fields: variantFields,
|
|
251
|
-
...variantDescription ? { description: variantDescription } : {}
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
const description = extractDescription(schema);
|
|
255
|
-
return {
|
|
256
|
-
fields: Array.from(allFieldsMap.values()),
|
|
257
|
-
schema,
|
|
258
|
-
schemaType: "discriminatedUnion",
|
|
259
|
-
unknownKeysMode: getUnknownKeysMode(schema),
|
|
260
|
-
discriminator,
|
|
261
|
-
variants,
|
|
262
|
-
...description ? { description } : {}
|
|
263
|
-
};
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* Extract fields from a union-like schema (union or xor)
|
|
267
|
-
*/
|
|
268
|
-
function extractFromUnionLike(schema, schemaType) {
|
|
269
|
-
const s = schema;
|
|
270
|
-
const options = (s.def ?? s._def)?.options ?? [];
|
|
271
|
-
const allFieldsMap = /* @__PURE__ */ new Map();
|
|
272
|
-
const unionOptions = [];
|
|
273
|
-
for (const option of options) {
|
|
274
|
-
const extracted = extractZodFields(option);
|
|
275
|
-
unionOptions.push(extracted);
|
|
276
|
-
for (const field of extracted.fields) if (!allFieldsMap.has(field.name)) allFieldsMap.set(field.name, field);
|
|
277
|
-
}
|
|
278
|
-
const description = extractDescription(schema);
|
|
279
|
-
return {
|
|
280
|
-
fields: Array.from(allFieldsMap.values()),
|
|
281
|
-
schema,
|
|
282
|
-
schemaType,
|
|
283
|
-
unknownKeysMode: getUnknownKeysMode(schema),
|
|
284
|
-
unionOptions,
|
|
285
|
-
...description ? { description } : {}
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Extract fields from an intersection
|
|
290
|
-
*/
|
|
291
|
-
function extractFromIntersection(schema) {
|
|
292
|
-
const s = schema;
|
|
293
|
-
const def = s.def ?? s._def;
|
|
294
|
-
const left = def?.left;
|
|
295
|
-
const right = def?.right;
|
|
296
|
-
const allFieldsMap = /* @__PURE__ */ new Map();
|
|
297
|
-
const extractSubFields = (subSchema) => {
|
|
298
|
-
if (!subSchema) return;
|
|
299
|
-
const extracted = extractZodFields(subSchema);
|
|
300
|
-
for (const field of extracted.fields) if (!allFieldsMap.has(field.name)) allFieldsMap.set(field.name, field);
|
|
301
|
-
};
|
|
302
|
-
extractSubFields(left);
|
|
303
|
-
extractSubFields(right);
|
|
304
|
-
const description = extractDescription(schema);
|
|
305
|
-
return {
|
|
306
|
-
fields: Array.from(allFieldsMap.values()),
|
|
307
|
-
schema,
|
|
308
|
-
schemaType: "intersection",
|
|
309
|
-
unknownKeysMode: getUnknownKeysMode(schema),
|
|
310
|
-
...description ? { description } : {}
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* Cache for extractZodFields results to avoid redundant schema extraction
|
|
315
|
-
*/
|
|
316
|
-
const extractFieldsCache = /* @__PURE__ */ new WeakMap();
|
|
317
|
-
/**
|
|
318
|
-
* Extract all fields from a zod args schema
|
|
319
|
-
* (ZodObject, ZodDiscriminatedUnion, etc.)
|
|
320
|
-
*/
|
|
321
|
-
function extractZodFields(schema) {
|
|
322
|
-
const cached = extractFieldsCache.get(schema);
|
|
323
|
-
if (cached) return cached;
|
|
324
|
-
const zodSchema = schema;
|
|
325
|
-
let result;
|
|
326
|
-
const typeName = getTypeName(zodSchema);
|
|
327
|
-
const s = zodSchema;
|
|
328
|
-
const def = s.def ?? s._def;
|
|
329
|
-
switch (typeName) {
|
|
330
|
-
case "object": {
|
|
331
|
-
const description = extractDescription(zodSchema);
|
|
332
|
-
result = {
|
|
333
|
-
fields: extractFromObject(zodSchema),
|
|
334
|
-
schema,
|
|
335
|
-
schemaType: "object",
|
|
336
|
-
unknownKeysMode: getUnknownKeysMode(zodSchema),
|
|
337
|
-
...description ? { description } : {}
|
|
338
|
-
};
|
|
339
|
-
break;
|
|
340
|
-
}
|
|
341
|
-
case "union":
|
|
342
|
-
if (def?.discriminator) result = extractFromDiscriminatedUnion(zodSchema);
|
|
343
|
-
else result = extractFromUnionLike(zodSchema, "union");
|
|
344
|
-
break;
|
|
345
|
-
case "xor":
|
|
346
|
-
result = extractFromUnionLike(zodSchema, "xor");
|
|
347
|
-
break;
|
|
348
|
-
case "intersection":
|
|
349
|
-
result = extractFromIntersection(zodSchema);
|
|
350
|
-
break;
|
|
351
|
-
case "optional":
|
|
352
|
-
case "nullable":
|
|
353
|
-
case "default":
|
|
354
|
-
case "pipe": {
|
|
355
|
-
const inner = def?.innerType ?? def?.in ?? def?.schema;
|
|
356
|
-
if (inner) {
|
|
357
|
-
const innerResult = extractZodFields(inner);
|
|
358
|
-
const wrapperDescription = extractDescription(zodSchema);
|
|
359
|
-
result = {
|
|
360
|
-
...innerResult,
|
|
361
|
-
schema,
|
|
362
|
-
...wrapperDescription ? { description: wrapperDescription } : {}
|
|
363
|
-
};
|
|
364
|
-
break;
|
|
365
|
-
}
|
|
366
|
-
const wrapperDescription = extractDescription(zodSchema);
|
|
367
|
-
result = {
|
|
368
|
-
fields: [],
|
|
369
|
-
schema,
|
|
370
|
-
schemaType: "object",
|
|
371
|
-
unknownKeysMode: getUnknownKeysMode(zodSchema),
|
|
372
|
-
...wrapperDescription ? { description: wrapperDescription } : {}
|
|
373
|
-
};
|
|
374
|
-
break;
|
|
375
|
-
}
|
|
376
|
-
default: {
|
|
377
|
-
const description = extractDescription(zodSchema);
|
|
378
|
-
result = {
|
|
379
|
-
fields: [],
|
|
380
|
-
schema,
|
|
381
|
-
schemaType: "object",
|
|
382
|
-
unknownKeysMode: getUnknownKeysMode(zodSchema),
|
|
383
|
-
...description ? { description } : {}
|
|
384
|
-
};
|
|
385
|
-
break;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
extractFieldsCache.set(schema, result);
|
|
389
|
-
return result;
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* Convert ZodError to ValidationError array (zod v4 compatible)
|
|
393
|
-
*/
|
|
394
|
-
function formatZodErrors(error) {
|
|
395
|
-
return error.issues.map((issue) => ({
|
|
396
|
-
path: issue.path.map(String),
|
|
397
|
-
message: issue.message,
|
|
398
|
-
code: issue.code,
|
|
399
|
-
received: "received" in issue ? issue.received : void 0,
|
|
400
|
-
expected: "expected" in issue ? String(issue.expected) : void 0
|
|
401
|
-
}));
|
|
402
|
-
}
|
|
403
|
-
/**
|
|
404
|
-
* Validate raw arguments against a zod schema
|
|
405
|
-
*/
|
|
406
|
-
function validateZodArgs(rawArgs, schema) {
|
|
407
|
-
const result = schema.safeParse(rawArgs);
|
|
408
|
-
if (result.success) return {
|
|
409
|
-
success: true,
|
|
410
|
-
data: result.data
|
|
411
|
-
};
|
|
412
|
-
return {
|
|
413
|
-
success: false,
|
|
414
|
-
errors: formatZodErrors(result.error)
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
/**
|
|
418
|
-
* The zod validator adapter.
|
|
419
|
-
*/
|
|
420
|
-
const zodAdapter = {
|
|
421
|
-
vendor: "zod",
|
|
422
|
-
extractFields: extractZodFields,
|
|
423
|
-
resolveFieldMeta: (name, fieldSchema) => resolveZodFieldMeta(name, fieldSchema),
|
|
424
|
-
getUnknownKeysMode: (schema) => getUnknownKeysMode(schema),
|
|
425
|
-
validate: validateZodArgs
|
|
426
|
-
};
|
|
427
|
-
|
|
428
|
-
//#endregion
|
|
429
|
-
//#region src/register.ts
|
|
430
|
-
/**
|
|
431
|
-
* Import-time side effect: register the zod adapter with `@politty/core`.
|
|
432
|
-
*
|
|
433
|
-
* Every entry module of this package imports this file first, so any code
|
|
434
|
-
* path a user can reach has the adapter installed before core touches a
|
|
435
|
-
* schema. Registration is idempotent.
|
|
436
|
-
*/
|
|
437
|
-
registerValidatorAdapter(zodAdapter);
|
|
438
|
-
|
|
439
|
-
//#endregion
|
|
440
|
-
export { arg as t };
|