@optique/valibot 1.0.0-dev.1290 → 1.0.0-dev.1300

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/index.cjs CHANGED
@@ -22,6 +22,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
22
22
 
23
23
  //#endregion
24
24
  const __optique_core_message = __toESM(require("@optique/core/message"));
25
+ const __optique_core_nonempty = __toESM(require("@optique/core/nonempty"));
25
26
  const valibot = __toESM(require("valibot"));
26
27
 
27
28
  //#region src/index.ts
@@ -83,8 +84,15 @@ function inferMetavar(schema) {
83
84
  if (schemaType === "boolean") return "BOOLEAN";
84
85
  if (schemaType === "date") return "DATE";
85
86
  if (schemaType === "picklist") return "CHOICE";
86
- if (schemaType === "literal") return "VALUE";
87
- if (schemaType === "union" || schemaType === "variant") return "VALUE";
87
+ if (schemaType === "literal") {
88
+ if (inferChoices(schema) != null) return "CHOICE";
89
+ return "VALUE";
90
+ }
91
+ if (schemaType === "union") {
92
+ if (inferChoices(schema) != null) return "CHOICE";
93
+ return "VALUE";
94
+ }
95
+ if (schemaType === "variant") return "VALUE";
88
96
  if (schemaType === "optional" || schemaType === "nullable" || schemaType === "nullish") {
89
97
  const wrapped = internalSchema.wrapped;
90
98
  if (wrapped) return inferMetavar(wrapped);
@@ -92,6 +100,50 @@ function inferMetavar(schema) {
92
100
  return "VALUE";
93
101
  }
94
102
  /**
103
+ * Extracts valid choices from a Valibot schema that represents a fixed set of
104
+ * values (picklist, literal, or union of literals).
105
+ *
106
+ * @param schema A Valibot schema to analyze.
107
+ * @returns An array of string representations of valid choices, or `undefined`
108
+ * if the schema does not represent a fixed set of values.
109
+ */
110
+ function inferChoices(schema) {
111
+ const internalSchema = schema;
112
+ const schemaType = internalSchema.type;
113
+ if (!schemaType) return void 0;
114
+ if (schemaType === "picklist") {
115
+ const options = internalSchema.options;
116
+ if (Array.isArray(options)) {
117
+ const result = [];
118
+ for (const opt of options) if (typeof opt === "string") result.push(opt);
119
+ else return void 0;
120
+ return result.length > 0 ? result : void 0;
121
+ }
122
+ return void 0;
123
+ }
124
+ if (schemaType === "literal") {
125
+ const value = internalSchema.literal;
126
+ if (typeof value === "string") return [value];
127
+ return void 0;
128
+ }
129
+ if (schemaType === "union") {
130
+ const options = internalSchema.options;
131
+ if (!Array.isArray(options)) return void 0;
132
+ const allChoices = /* @__PURE__ */ new Set();
133
+ for (const opt of options) if (typeof opt === "object" && opt != null && "type" in opt) {
134
+ const sub = inferChoices(opt);
135
+ if (sub == null) return void 0;
136
+ for (const choice of sub) allChoices.add(choice);
137
+ } else return void 0;
138
+ return allChoices.size > 0 ? [...allChoices] : void 0;
139
+ }
140
+ if (schemaType === "optional" || schemaType === "nullable" || schemaType === "nullish") {
141
+ const wrapped = internalSchema.wrapped;
142
+ if (wrapped) return inferChoices(wrapped);
143
+ }
144
+ return void 0;
145
+ }
146
+ /**
95
147
  * Creates a value parser from a Valibot schema.
96
148
  *
97
149
  * This parser validates CLI argument strings using Valibot schemas, enabling
@@ -168,12 +220,25 @@ function inferMetavar(schema) {
168
220
  * );
169
221
  * ```
170
222
  *
223
+ * @throws {TypeError} If the resolved `metavar` is an empty string.
171
224
  * @since 0.7.0
172
225
  */
173
226
  function valibot$1(schema, options = {}) {
174
- return {
227
+ const choices = inferChoices(schema);
228
+ const metavar = options.metavar ?? inferMetavar(schema);
229
+ (0, __optique_core_nonempty.ensureNonEmptyString)(metavar);
230
+ const parser = {
175
231
  $mode: "sync",
176
- metavar: options.metavar ?? inferMetavar(schema),
232
+ metavar,
233
+ ...choices != null && choices.length > 0 ? {
234
+ choices: Object.freeze(choices),
235
+ *suggest(prefix) {
236
+ for (const c of choices) if (c.startsWith(prefix)) yield {
237
+ kind: "literal",
238
+ text: c
239
+ };
240
+ }
241
+ } : {},
177
242
  parse(input) {
178
243
  const result = (0, valibot.safeParse)(schema, input);
179
244
  if (result.success) return {
@@ -194,6 +259,7 @@ function valibot$1(schema, options = {}) {
194
259
  return String(value);
195
260
  }
196
261
  };
262
+ return parser;
197
263
  }
198
264
 
199
265
  //#endregion
package/dist/index.d.cts CHANGED
@@ -106,6 +106,7 @@ interface ValibotParserOptions {
106
106
  * );
107
107
  * ```
108
108
  *
109
+ * @throws {TypeError} If the resolved `metavar` is an empty string.
109
110
  * @since 0.7.0
110
111
  */
111
112
  declare function valibot<T>(schema: v.BaseSchema<unknown, T, v.BaseIssue<unknown>>, options?: ValibotParserOptions): ValueParser<"sync", T>;
package/dist/index.d.ts CHANGED
@@ -106,6 +106,7 @@ interface ValibotParserOptions {
106
106
  * );
107
107
  * ```
108
108
  *
109
+ * @throws {TypeError} If the resolved `metavar` is an empty string.
109
110
  * @since 0.7.0
110
111
  */
111
112
  declare function valibot<T>(schema: v.BaseSchema<unknown, T, v.BaseIssue<unknown>>, options?: ValibotParserOptions): ValueParser<"sync", T>;
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { message } from "@optique/core/message";
2
+ import { ensureNonEmptyString } from "@optique/core/nonempty";
2
3
  import { safeParse } from "valibot";
3
4
 
4
5
  //#region src/index.ts
@@ -60,8 +61,15 @@ function inferMetavar(schema) {
60
61
  if (schemaType === "boolean") return "BOOLEAN";
61
62
  if (schemaType === "date") return "DATE";
62
63
  if (schemaType === "picklist") return "CHOICE";
63
- if (schemaType === "literal") return "VALUE";
64
- if (schemaType === "union" || schemaType === "variant") return "VALUE";
64
+ if (schemaType === "literal") {
65
+ if (inferChoices(schema) != null) return "CHOICE";
66
+ return "VALUE";
67
+ }
68
+ if (schemaType === "union") {
69
+ if (inferChoices(schema) != null) return "CHOICE";
70
+ return "VALUE";
71
+ }
72
+ if (schemaType === "variant") return "VALUE";
65
73
  if (schemaType === "optional" || schemaType === "nullable" || schemaType === "nullish") {
66
74
  const wrapped = internalSchema.wrapped;
67
75
  if (wrapped) return inferMetavar(wrapped);
@@ -69,6 +77,50 @@ function inferMetavar(schema) {
69
77
  return "VALUE";
70
78
  }
71
79
  /**
80
+ * Extracts valid choices from a Valibot schema that represents a fixed set of
81
+ * values (picklist, literal, or union of literals).
82
+ *
83
+ * @param schema A Valibot schema to analyze.
84
+ * @returns An array of string representations of valid choices, or `undefined`
85
+ * if the schema does not represent a fixed set of values.
86
+ */
87
+ function inferChoices(schema) {
88
+ const internalSchema = schema;
89
+ const schemaType = internalSchema.type;
90
+ if (!schemaType) return void 0;
91
+ if (schemaType === "picklist") {
92
+ const options = internalSchema.options;
93
+ if (Array.isArray(options)) {
94
+ const result = [];
95
+ for (const opt of options) if (typeof opt === "string") result.push(opt);
96
+ else return void 0;
97
+ return result.length > 0 ? result : void 0;
98
+ }
99
+ return void 0;
100
+ }
101
+ if (schemaType === "literal") {
102
+ const value = internalSchema.literal;
103
+ if (typeof value === "string") return [value];
104
+ return void 0;
105
+ }
106
+ if (schemaType === "union") {
107
+ const options = internalSchema.options;
108
+ if (!Array.isArray(options)) return void 0;
109
+ const allChoices = /* @__PURE__ */ new Set();
110
+ for (const opt of options) if (typeof opt === "object" && opt != null && "type" in opt) {
111
+ const sub = inferChoices(opt);
112
+ if (sub == null) return void 0;
113
+ for (const choice of sub) allChoices.add(choice);
114
+ } else return void 0;
115
+ return allChoices.size > 0 ? [...allChoices] : void 0;
116
+ }
117
+ if (schemaType === "optional" || schemaType === "nullable" || schemaType === "nullish") {
118
+ const wrapped = internalSchema.wrapped;
119
+ if (wrapped) return inferChoices(wrapped);
120
+ }
121
+ return void 0;
122
+ }
123
+ /**
72
124
  * Creates a value parser from a Valibot schema.
73
125
  *
74
126
  * This parser validates CLI argument strings using Valibot schemas, enabling
@@ -145,12 +197,25 @@ function inferMetavar(schema) {
145
197
  * );
146
198
  * ```
147
199
  *
200
+ * @throws {TypeError} If the resolved `metavar` is an empty string.
148
201
  * @since 0.7.0
149
202
  */
150
203
  function valibot(schema, options = {}) {
151
- return {
204
+ const choices = inferChoices(schema);
205
+ const metavar = options.metavar ?? inferMetavar(schema);
206
+ ensureNonEmptyString(metavar);
207
+ const parser = {
152
208
  $mode: "sync",
153
- metavar: options.metavar ?? inferMetavar(schema),
209
+ metavar,
210
+ ...choices != null && choices.length > 0 ? {
211
+ choices: Object.freeze(choices),
212
+ *suggest(prefix) {
213
+ for (const c of choices) if (c.startsWith(prefix)) yield {
214
+ kind: "literal",
215
+ text: c
216
+ };
217
+ }
218
+ } : {},
154
219
  parse(input) {
155
220
  const result = safeParse(schema, input);
156
221
  if (result.success) return {
@@ -171,6 +236,7 @@ function valibot(schema, options = {}) {
171
236
  return String(value);
172
237
  }
173
238
  };
239
+ return parser;
174
240
  }
175
241
 
176
242
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/valibot",
3
- "version": "1.0.0-dev.1290+a999711d",
3
+ "version": "1.0.0-dev.1300+12293782",
4
4
  "description": "Valibot value parsers for Optique",
5
5
  "keywords": [
6
6
  "CLI",
@@ -57,7 +57,7 @@
57
57
  "valibot": "^1.2.0"
58
58
  },
59
59
  "dependencies": {
60
- "@optique/core": "1.0.0-dev.1290+a999711d"
60
+ "@optique/core": "1.0.0-dev.1300+12293782"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@types/node": "^20.19.9",