@politty/valibot 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/cli-main-B4FX3h4J.js +1 -0
- package/dist/cli-main-CjYk6tAX.js +2 -0
- package/dist/cli-run-NzniMJwW.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-BnpFle4e.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 -1803
- package/dist/subcommand-router-D8GTMXYL.js +1 -0
- package/dist/{arg-registry-Be4GJADw.d.ts → types-BBh8jmcS.d.ts} +839 -839
- package/dist/{index-B4NylQOW.d.ts → with-completion-command-BmOeO0qq.d.ts} +3 -74
- package/dist/with-completion-command-DDohm8U6.js +3 -0
- package/package.json +3 -3
- package/dist/cli-main-CGU1lFOR.js +0 -3
- package/dist/cli-main-zB-0qgHP.js +0 -314
- package/dist/cli-run-kxxQnIi_.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-B9K5n0KG.js +0 -494
- 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-CNyWzJXg.js +0 -6
- package/dist/src-CoQ2TUba.js +0 -12
- package/dist/subcommand-router-Cskpofdk.js +0 -134
|
@@ -1,494 +0,0 @@
|
|
|
1
|
-
import { n as resolveFieldMeta, o as registerValidatorAdapter } from "./field-meta-CO38jOvl.js";
|
|
2
|
-
import { safeParse } from "valibot";
|
|
3
|
-
|
|
4
|
-
//#region ../core/src/core/arg-registry.ts
|
|
5
|
-
/**
|
|
6
|
-
* Custom registry for politty argument metadata, keyed by schema identity.
|
|
7
|
-
* This avoids polluting Zod's GlobalMeta, and — unlike `z.registry()` — is
|
|
8
|
-
* validator-neutral: any schema object (zod, valibot, internal descriptors)
|
|
9
|
-
* can carry arg metadata without politty importing the schema library.
|
|
10
|
-
*/
|
|
11
|
-
const argRegistry = /* @__PURE__ */ new WeakMap();
|
|
12
|
-
function arg(schema, meta) {
|
|
13
|
-
if (meta) argRegistry.set(schema, meta);
|
|
14
|
-
return schema;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Get metadata for a schema from the registry
|
|
18
|
-
*
|
|
19
|
-
* @param schema - The schema object used as the registry key
|
|
20
|
-
* @returns The metadata if registered, undefined otherwise
|
|
21
|
-
*/
|
|
22
|
-
function getArgMeta$1(schema) {
|
|
23
|
-
return argRegistry.get(schema);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
//#endregion
|
|
27
|
-
//#region src/adapter.ts
|
|
28
|
-
/**
|
|
29
|
-
* Valibot validator adapter: implements politty's neutral `ValidatorAdapter`
|
|
30
|
-
* contract against valibot v1's schema representation.
|
|
31
|
-
*
|
|
32
|
-
* Valibot schemas are plain objects (`kind` / `type` / `entries` / `pipe`
|
|
33
|
-
* properties are public API), so all introspection here is structural.
|
|
34
|
-
* Validation goes through `safeParse` so error output keeps valibot's
|
|
35
|
-
* issue detail (`type` / `expected` / `received`).
|
|
36
|
-
*/
|
|
37
|
-
function asNode(schema) {
|
|
38
|
-
return schema;
|
|
39
|
-
}
|
|
40
|
-
/** Wrapper types that make a field accept a missing / undefined value. */
|
|
41
|
-
const OPTIONAL_WRAPPERS = /* @__PURE__ */ new Set([
|
|
42
|
-
"optional",
|
|
43
|
-
"exact_optional",
|
|
44
|
-
"nullish",
|
|
45
|
-
"undefinedable"
|
|
46
|
-
]);
|
|
47
|
-
/** Wrapper types that carry a `wrapped` inner schema. */
|
|
48
|
-
const UNWRAPPABLE = /* @__PURE__ */ new Set([
|
|
49
|
-
"optional",
|
|
50
|
-
"exact_optional",
|
|
51
|
-
"nullish",
|
|
52
|
-
"undefinedable",
|
|
53
|
-
"nullable",
|
|
54
|
-
"non_optional",
|
|
55
|
-
"non_nullable",
|
|
56
|
-
"non_nullish"
|
|
57
|
-
]);
|
|
58
|
-
/**
|
|
59
|
-
* Get the inner schema, unwrapping optional/nullable/etc. wrappers. Piped
|
|
60
|
-
* schemas need no explicit handling: `v.pipe` spreads its base schema, so
|
|
61
|
-
* the wrapper's own properties are already visible on the pipe result.
|
|
62
|
-
*/
|
|
63
|
-
function unwrapSchema(schema) {
|
|
64
|
-
let current = schema;
|
|
65
|
-
while (current.type && UNWRAPPABLE.has(current.type) && current.wrapped) current = current.wrapped;
|
|
66
|
-
return current;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Detect the base type of a schema.
|
|
70
|
-
*
|
|
71
|
-
* Input-side detection (matching the zod adapter, which unwraps pipes to
|
|
72
|
-
* their input schema): the CLI parser cares about what the user types, not
|
|
73
|
-
* what a transform produces. The one refinement is `unknown`/`any` inputs —
|
|
74
|
-
* the idiomatic valibot coercion pattern is
|
|
75
|
-
* `v.pipe(v.unknown(), v.transform(Number), v.number())`, where the input
|
|
76
|
-
* schema says nothing; in that case the later pipe schemas are scanned so
|
|
77
|
-
* the field still presents as a number in help / prompts / completion.
|
|
78
|
-
*/
|
|
79
|
-
function detectType(schema) {
|
|
80
|
-
const inner = unwrapSchema(schema);
|
|
81
|
-
const detected = bucketOf(inner.type);
|
|
82
|
-
if (detected !== "unknown") return detected;
|
|
83
|
-
if (inner.type === "unknown" || inner.type === "any") return detectTypeFromPipes(schema) ?? "unknown";
|
|
84
|
-
return "unknown";
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Scan the pipe of `schema` and of every wrapper beneath it for something that
|
|
88
|
-
* names a concrete type, outermost pipe first.
|
|
89
|
-
*
|
|
90
|
-
* Both levels have to be checked because `v.pipe` spreads its base schema, so
|
|
91
|
-
* where the `pipe` array ends up depends on the composition order: piping a
|
|
92
|
-
* wrapper leaves it on the wrapper node
|
|
93
|
-
* (`v.pipe(v.optional(v.unknown()), v.transform(Number), v.number())` is an
|
|
94
|
-
* `optional` node carrying the pipe, whose `wrapped` `unknown` has none),
|
|
95
|
-
* while wrapping a pipe leaves it on the wrapped node
|
|
96
|
-
* (`v.optional(v.pipe(v.unknown(), ...))`). Unwrapping first and reading only
|
|
97
|
-
* the inner pipe would miss the former and report the field as `unknown`.
|
|
98
|
-
*/
|
|
99
|
-
function detectTypeFromPipes(schema) {
|
|
100
|
-
if (Array.isArray(schema.pipe)) for (const item of schema.pipe) {
|
|
101
|
-
if (item.kind === "schema") {
|
|
102
|
-
const bucket = bucketOf(item.type);
|
|
103
|
-
if (bucket !== "unknown") return bucket;
|
|
104
|
-
}
|
|
105
|
-
if (item.kind === "validation" && impliesNumber(item)) return "number";
|
|
106
|
-
}
|
|
107
|
-
if (schema.type && UNWRAPPABLE.has(schema.type) && schema.wrapped) return detectTypeFromPipes(schema.wrapped);
|
|
108
|
-
}
|
|
109
|
-
/** Validation actions whose input type is exclusively `number`. */
|
|
110
|
-
const NUMBER_ONLY_VALIDATIONS = /* @__PURE__ */ new Set([
|
|
111
|
-
"integer",
|
|
112
|
-
"safe_integer",
|
|
113
|
-
"finite"
|
|
114
|
-
]);
|
|
115
|
-
/**
|
|
116
|
-
* Whether a validation action constrains its input to a number.
|
|
117
|
-
*
|
|
118
|
-
* `v.multipleOf` is overloaded for both `number` and `bigint` inputs under
|
|
119
|
-
* the same `multiple_of` action type, so it only implies a number field when
|
|
120
|
-
* its requirement is a number — `v.multipleOf(2n)` describes a bigint, which
|
|
121
|
-
* politty has no field type for and leaves as `unknown`.
|
|
122
|
-
*/
|
|
123
|
-
function impliesNumber(action) {
|
|
124
|
-
if (!action.type) return false;
|
|
125
|
-
if (NUMBER_ONLY_VALIDATIONS.has(action.type)) return true;
|
|
126
|
-
return action.type === "multiple_of" && typeof action.requirement === "number";
|
|
127
|
-
}
|
|
128
|
-
function bucketOf(type) {
|
|
129
|
-
switch (type) {
|
|
130
|
-
case "string":
|
|
131
|
-
case "picklist":
|
|
132
|
-
case "enum": return "string";
|
|
133
|
-
case "number": return "number";
|
|
134
|
-
case "boolean": return "boolean";
|
|
135
|
-
case "array": return "array";
|
|
136
|
-
default: return "unknown";
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Extract enum values from a schema if it's enum-like
|
|
141
|
-
* (picklist, enum, array of enums, or a union of string literals).
|
|
142
|
-
*/
|
|
143
|
-
function extractEnumValues(schema) {
|
|
144
|
-
const inner = unwrapSchema(asNode(schema));
|
|
145
|
-
const allStrings = (values) => values.length > 0 && values.every((value) => typeof value === "string");
|
|
146
|
-
if (inner.type === "picklist" && Array.isArray(inner.options)) return allStrings(inner.options) ? inner.options : void 0;
|
|
147
|
-
if (inner.type === "enum") {
|
|
148
|
-
const values = Array.isArray(inner.options) ? inner.options : inner.enum && typeof inner.enum === "object" ? Object.values(inner.enum) : [];
|
|
149
|
-
return allStrings(values) ? values : void 0;
|
|
150
|
-
}
|
|
151
|
-
if (inner.type === "array" && inner.item) return extractEnumValues(inner.item);
|
|
152
|
-
if (inner.type === "union" && Array.isArray(inner.options)) {
|
|
153
|
-
const literalValues = [];
|
|
154
|
-
for (const option of inner.options) {
|
|
155
|
-
const node = asNode(option);
|
|
156
|
-
if (node.type === "literal" && typeof node.literal === "string") literalValues.push(node.literal);
|
|
157
|
-
}
|
|
158
|
-
if (literalValues.length === inner.options.length && literalValues.length > 0) return literalValues;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Check if a schema is required: no optional-ish wrapper anywhere in the
|
|
163
|
-
* wrapper chain. Mirrors the zod adapter's `isOptional()` semantics —
|
|
164
|
-
* `nullable` alone does NOT make a CLI arg optional (CLI values are present
|
|
165
|
-
* or absent, never null).
|
|
166
|
-
*/
|
|
167
|
-
function isRequired(schema) {
|
|
168
|
-
let current = schema;
|
|
169
|
-
while (current.type && UNWRAPPABLE.has(current.type)) {
|
|
170
|
-
if (OPTIONAL_WRAPPERS.has(current.type)) return false;
|
|
171
|
-
if (!current.wrapped) break;
|
|
172
|
-
current = current.wrapped;
|
|
173
|
-
}
|
|
174
|
-
return true;
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Extract the declared default value, walking the wrapper chain.
|
|
178
|
-
* Factory defaults (`v.optional(schema, () => value)`) are invoked, matching
|
|
179
|
-
* `v.getDefault` and the zod adapter's behavior for `.default(() => ...)`.
|
|
180
|
-
*/
|
|
181
|
-
function extractDefaultValue(schema) {
|
|
182
|
-
let current = schema;
|
|
183
|
-
while (current?.type && UNWRAPPABLE.has(current.type)) {
|
|
184
|
-
if (current.default !== void 0) return typeof current.default === "function" ? current.default() : current.default;
|
|
185
|
-
current = current.wrapped;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Extract a description from `v.description(...)` metadata actions in the
|
|
190
|
-
* schema's pipe, recursing into wrappers. The last action wins, matching
|
|
191
|
-
* valibot's `v.getDescription` behavior for a single pipe.
|
|
192
|
-
*
|
|
193
|
-
* Hand-rolled rather than `v.getDescription` / `v.getDefault` because the
|
|
194
|
-
* official utilities do not look through wrapper schemas — e.g.
|
|
195
|
-
* `v.getDescription(v.optional(v.pipe(v.string(), v.description("x"))))`
|
|
196
|
-
* returns `undefined` — while zod-adapter parity requires descriptions and
|
|
197
|
-
* defaults declared on the wrapped inner schema to be found.
|
|
198
|
-
*/
|
|
199
|
-
function extractDescription(schema) {
|
|
200
|
-
if (Array.isArray(schema.pipe)) {
|
|
201
|
-
let description;
|
|
202
|
-
for (const item of schema.pipe) if (item.kind === "metadata" && item.type === "description") {
|
|
203
|
-
if (typeof item.description === "string") description = item.description;
|
|
204
|
-
}
|
|
205
|
-
if (description !== void 0) return description;
|
|
206
|
-
}
|
|
207
|
-
if (schema.type && UNWRAPPABLE.has(schema.type) && schema.wrapped) return extractDescription(schema.wrapped);
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Extract `v.metadata({...})` action content from the schema's pipe,
|
|
211
|
-
* recursing into wrappers. Later actions win key-by-key.
|
|
212
|
-
*/
|
|
213
|
-
function extractMetadataAction(schema) {
|
|
214
|
-
let merged;
|
|
215
|
-
if (schema.type && UNWRAPPABLE.has(schema.type) && schema.wrapped) merged = extractMetadataAction(schema.wrapped);
|
|
216
|
-
if (Array.isArray(schema.pipe)) {
|
|
217
|
-
for (const item of schema.pipe) if (item.kind === "metadata" && item.type === "metadata" && item.metadata) merged = {
|
|
218
|
-
...merged,
|
|
219
|
-
...item.metadata
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
return merged;
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Find arg() registry metadata for a schema. `v.pipe(...)` spreads its base
|
|
226
|
-
* schema into a NEW object, so metadata registered before composing —
|
|
227
|
-
* `v.pipe(arg(v.string(), {...}), v.transform(...))` — is keyed by the
|
|
228
|
-
* original base object, which survives only as an item of the `pipe` array.
|
|
229
|
-
* Walk pipe items and wrapper chains so composition never drops metadata.
|
|
230
|
-
*/
|
|
231
|
-
function lookupRegistryMeta(node, seen) {
|
|
232
|
-
if (seen.has(node)) return void 0;
|
|
233
|
-
seen.add(node);
|
|
234
|
-
const direct = getArgMeta$1(node);
|
|
235
|
-
if (direct) return direct;
|
|
236
|
-
if (Array.isArray(node.pipe)) {
|
|
237
|
-
for (const item of node.pipe) if (item && item.kind === "schema") {
|
|
238
|
-
const hit = lookupRegistryMeta(item, seen);
|
|
239
|
-
if (hit) return hit;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
if (node.wrapped) return lookupRegistryMeta(node.wrapped, seen);
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Get ArgMeta for a field schema.
|
|
246
|
-
* Priority: politty's arg() registry > `v.metadata({...})` pipe actions.
|
|
247
|
-
*/
|
|
248
|
-
function getArgMeta(schema) {
|
|
249
|
-
const fromRegistry = lookupRegistryMeta(schema, /* @__PURE__ */ new Set());
|
|
250
|
-
if (fromRegistry) return fromRegistry;
|
|
251
|
-
const fromMetadataAction = extractMetadataAction(schema);
|
|
252
|
-
if (fromMetadataAction) return fromMetadataAction;
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Resolve field metadata from a valibot field schema. The valibot-specific
|
|
256
|
-
* introspection happens here; the shared alias/negation policy lives in
|
|
257
|
-
* `resolveFieldMeta` (adapter/field-meta.ts).
|
|
258
|
-
*/
|
|
259
|
-
function resolveValibotFieldMeta(name, schema) {
|
|
260
|
-
const node = asNode(schema);
|
|
261
|
-
return resolveFieldMeta(name, {
|
|
262
|
-
argMeta: getArgMeta(node),
|
|
263
|
-
description: extractDescription(node),
|
|
264
|
-
required: isRequired(node),
|
|
265
|
-
defaultValue: extractDefaultValue(node),
|
|
266
|
-
type: detectType(node),
|
|
267
|
-
enumValues: extractEnumValues(node),
|
|
268
|
-
schema
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* Detect unknown keys handling mode from a valibot object schema.
|
|
273
|
-
*
|
|
274
|
-
* - `v.object()`: strips unknown keys (politty warns) → "strip"
|
|
275
|
-
* - `v.strictObject()`: rejects unknown keys → "strict"
|
|
276
|
-
* - `v.looseObject()` / `v.objectWithRest()`: keeps unknown keys → "passthrough"
|
|
277
|
-
*/
|
|
278
|
-
function getUnknownKeysMode(schema) {
|
|
279
|
-
switch (unwrapSchema(asNode(schema)).type) {
|
|
280
|
-
case "strict_object": return "strict";
|
|
281
|
-
case "loose_object":
|
|
282
|
-
case "object_with_rest": return "passthrough";
|
|
283
|
-
default: return "strip";
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
/**
|
|
287
|
-
* Extract fields from an object-like schema's entries
|
|
288
|
-
*/
|
|
289
|
-
function extractFromObject(schema) {
|
|
290
|
-
const entries = schema.entries ?? {};
|
|
291
|
-
return Object.entries(entries).map(([name, fieldSchema]) => resolveValibotFieldMeta(name, fieldSchema));
|
|
292
|
-
}
|
|
293
|
-
/**
|
|
294
|
-
* Get the discriminator value of one variant option for `v.variant(key, ...)`.
|
|
295
|
-
* Supports `v.literal()` and single-value `v.picklist()` / `v.enum()`.
|
|
296
|
-
*/
|
|
297
|
-
function getDiscriminatorValue(option, key) {
|
|
298
|
-
const discriminatorSchema = option.entries?.[key];
|
|
299
|
-
if (!discriminatorSchema) return "";
|
|
300
|
-
const inner = unwrapSchema(discriminatorSchema);
|
|
301
|
-
if (inner.type === "literal" && inner.literal !== void 0) return String(inner.literal);
|
|
302
|
-
const enumValues = extractEnumValues(inner);
|
|
303
|
-
if (enumValues && enumValues.length === 1) return enumValues[0];
|
|
304
|
-
return "";
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Extract fields from `v.variant(key, [...])` (valibot's discriminated union)
|
|
308
|
-
*/
|
|
309
|
-
function extractFromVariant(schema) {
|
|
310
|
-
const discriminator = schema.key ?? "";
|
|
311
|
-
const options = schema.options ?? [];
|
|
312
|
-
const allFieldsMap = /* @__PURE__ */ new Map();
|
|
313
|
-
const variants = [];
|
|
314
|
-
for (const option of options) {
|
|
315
|
-
const optionNode = asNode(option);
|
|
316
|
-
const variantFields = [];
|
|
317
|
-
for (const [name, fieldSchema] of Object.entries(optionNode.entries ?? {})) {
|
|
318
|
-
const fieldMeta = resolveValibotFieldMeta(name, fieldSchema);
|
|
319
|
-
variantFields.push(fieldMeta);
|
|
320
|
-
if (!allFieldsMap.has(name)) allFieldsMap.set(name, fieldMeta);
|
|
321
|
-
}
|
|
322
|
-
const variantDescription = extractDescription(optionNode);
|
|
323
|
-
variants.push({
|
|
324
|
-
discriminatorValue: getDiscriminatorValue(optionNode, discriminator),
|
|
325
|
-
fields: variantFields,
|
|
326
|
-
...variantDescription ? { description: variantDescription } : {}
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
const description = extractDescription(schema);
|
|
330
|
-
return {
|
|
331
|
-
fields: Array.from(allFieldsMap.values()),
|
|
332
|
-
schema,
|
|
333
|
-
schemaType: "discriminatedUnion",
|
|
334
|
-
unknownKeysMode: getUnknownKeysMode(schema),
|
|
335
|
-
discriminator,
|
|
336
|
-
variants,
|
|
337
|
-
...description ? { description } : {}
|
|
338
|
-
};
|
|
339
|
-
}
|
|
340
|
-
/**
|
|
341
|
-
* Extract fields from a union schema
|
|
342
|
-
*/
|
|
343
|
-
function extractFromUnion(schema) {
|
|
344
|
-
const options = schema.options ?? [];
|
|
345
|
-
const allFieldsMap = /* @__PURE__ */ new Map();
|
|
346
|
-
const unionOptions = [];
|
|
347
|
-
for (const option of options) {
|
|
348
|
-
const extracted = extractValibotFields(option);
|
|
349
|
-
unionOptions.push(extracted);
|
|
350
|
-
for (const field of extracted.fields) if (!allFieldsMap.has(field.name)) allFieldsMap.set(field.name, field);
|
|
351
|
-
}
|
|
352
|
-
const description = extractDescription(schema);
|
|
353
|
-
return {
|
|
354
|
-
fields: Array.from(allFieldsMap.values()),
|
|
355
|
-
schema,
|
|
356
|
-
schemaType: "union",
|
|
357
|
-
unknownKeysMode: getUnknownKeysMode(schema),
|
|
358
|
-
unionOptions,
|
|
359
|
-
...description ? { description } : {}
|
|
360
|
-
};
|
|
361
|
-
}
|
|
362
|
-
/**
|
|
363
|
-
* Extract fields from `v.intersect([...])`
|
|
364
|
-
*/
|
|
365
|
-
function extractFromIntersect(schema) {
|
|
366
|
-
const allFieldsMap = /* @__PURE__ */ new Map();
|
|
367
|
-
for (const option of schema.options ?? []) {
|
|
368
|
-
const extracted = extractValibotFields(option);
|
|
369
|
-
for (const field of extracted.fields) if (!allFieldsMap.has(field.name)) allFieldsMap.set(field.name, field);
|
|
370
|
-
}
|
|
371
|
-
const description = extractDescription(schema);
|
|
372
|
-
return {
|
|
373
|
-
fields: Array.from(allFieldsMap.values()),
|
|
374
|
-
schema,
|
|
375
|
-
schemaType: "intersection",
|
|
376
|
-
unknownKeysMode: getUnknownKeysMode(schema),
|
|
377
|
-
...description ? { description } : {}
|
|
378
|
-
};
|
|
379
|
-
}
|
|
380
|
-
/**
|
|
381
|
-
* Cache for extractValibotFields results to avoid redundant schema extraction
|
|
382
|
-
*/
|
|
383
|
-
const extractFieldsCache = /* @__PURE__ */ new WeakMap();
|
|
384
|
-
/**
|
|
385
|
-
* Extract all fields from a valibot args schema
|
|
386
|
-
* (object, strictObject, looseObject, variant, union, intersect)
|
|
387
|
-
*/
|
|
388
|
-
function extractValibotFields(schema) {
|
|
389
|
-
const cached = extractFieldsCache.get(schema);
|
|
390
|
-
if (cached) return cached;
|
|
391
|
-
const node = unwrapSchema(asNode(schema));
|
|
392
|
-
let result;
|
|
393
|
-
switch (node.type) {
|
|
394
|
-
case "object":
|
|
395
|
-
case "strict_object":
|
|
396
|
-
case "loose_object":
|
|
397
|
-
case "object_with_rest": {
|
|
398
|
-
const description = extractDescription(asNode(schema));
|
|
399
|
-
result = {
|
|
400
|
-
fields: extractFromObject(node),
|
|
401
|
-
schema,
|
|
402
|
-
schemaType: "object",
|
|
403
|
-
unknownKeysMode: getUnknownKeysMode(node),
|
|
404
|
-
...description ? { description } : {}
|
|
405
|
-
};
|
|
406
|
-
break;
|
|
407
|
-
}
|
|
408
|
-
case "variant":
|
|
409
|
-
result = {
|
|
410
|
-
...extractFromVariant(node),
|
|
411
|
-
schema
|
|
412
|
-
};
|
|
413
|
-
break;
|
|
414
|
-
case "union":
|
|
415
|
-
result = {
|
|
416
|
-
...extractFromUnion(node),
|
|
417
|
-
schema
|
|
418
|
-
};
|
|
419
|
-
break;
|
|
420
|
-
case "intersect":
|
|
421
|
-
result = {
|
|
422
|
-
...extractFromIntersect(node),
|
|
423
|
-
schema
|
|
424
|
-
};
|
|
425
|
-
break;
|
|
426
|
-
default: {
|
|
427
|
-
const description = extractDescription(asNode(schema));
|
|
428
|
-
result = {
|
|
429
|
-
fields: [],
|
|
430
|
-
schema,
|
|
431
|
-
schemaType: "object",
|
|
432
|
-
unknownKeysMode: getUnknownKeysMode(node),
|
|
433
|
-
...description ? { description } : {}
|
|
434
|
-
};
|
|
435
|
-
break;
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
extractFieldsCache.set(schema, result);
|
|
439
|
-
return result;
|
|
440
|
-
}
|
|
441
|
-
/**
|
|
442
|
-
* Convert valibot issues to ValidationError array
|
|
443
|
-
*/
|
|
444
|
-
function formatValibotIssues(issues) {
|
|
445
|
-
return issues.map((raw) => {
|
|
446
|
-
const issue = raw;
|
|
447
|
-
return {
|
|
448
|
-
path: issue.path?.map((item) => String(item.key)) ?? [],
|
|
449
|
-
message: issue.message,
|
|
450
|
-
code: issue.type,
|
|
451
|
-
received: issue.input,
|
|
452
|
-
expected: issue.expected ?? void 0
|
|
453
|
-
};
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
/**
|
|
457
|
-
* Validate raw arguments against a valibot schema
|
|
458
|
-
*/
|
|
459
|
-
function validateValibotArgs(rawArgs, schema) {
|
|
460
|
-
const result = safeParse(schema, rawArgs);
|
|
461
|
-
if (result.success) return {
|
|
462
|
-
success: true,
|
|
463
|
-
data: result.output
|
|
464
|
-
};
|
|
465
|
-
return {
|
|
466
|
-
success: false,
|
|
467
|
-
errors: formatValibotIssues(result.issues)
|
|
468
|
-
};
|
|
469
|
-
}
|
|
470
|
-
/**
|
|
471
|
-
* The valibot validator adapter.
|
|
472
|
-
*/
|
|
473
|
-
const valibotAdapter = {
|
|
474
|
-
vendor: "valibot",
|
|
475
|
-
extractFields: extractValibotFields,
|
|
476
|
-
resolveFieldMeta: resolveValibotFieldMeta,
|
|
477
|
-
getUnknownKeysMode,
|
|
478
|
-
validate: validateValibotArgs
|
|
479
|
-
};
|
|
480
|
-
|
|
481
|
-
//#endregion
|
|
482
|
-
//#region src/register.ts
|
|
483
|
-
/**
|
|
484
|
-
* Import-time side effect: register the valibot adapter with `@politty/core`.
|
|
485
|
-
*
|
|
486
|
-
* Every entry module of this package imports this file first (except
|
|
487
|
-
* `compile-cache`, which never touches schemas), so any code path a user
|
|
488
|
-
* can reach has the adapter installed before core touches a schema.
|
|
489
|
-
* Registration is idempotent.
|
|
490
|
-
*/
|
|
491
|
-
registerValidatorAdapter(valibotAdapter);
|
|
492
|
-
|
|
493
|
-
//#endregion
|
|
494
|
-
export { arg as t };
|