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

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
@@ -83,8 +83,15 @@ function inferMetavar(schema) {
83
83
  if (schemaType === "boolean") return "BOOLEAN";
84
84
  if (schemaType === "date") return "DATE";
85
85
  if (schemaType === "picklist") return "CHOICE";
86
- if (schemaType === "literal") return "VALUE";
87
- if (schemaType === "union" || schemaType === "variant") return "VALUE";
86
+ if (schemaType === "literal") {
87
+ if (inferChoices(schema) != null) return "CHOICE";
88
+ return "VALUE";
89
+ }
90
+ if (schemaType === "union") {
91
+ if (inferChoices(schema) != null) return "CHOICE";
92
+ return "VALUE";
93
+ }
94
+ if (schemaType === "variant") return "VALUE";
88
95
  if (schemaType === "optional" || schemaType === "nullable" || schemaType === "nullish") {
89
96
  const wrapped = internalSchema.wrapped;
90
97
  if (wrapped) return inferMetavar(wrapped);
@@ -92,6 +99,50 @@ function inferMetavar(schema) {
92
99
  return "VALUE";
93
100
  }
94
101
  /**
102
+ * Extracts valid choices from a Valibot schema that represents a fixed set of
103
+ * values (picklist, literal, or union of literals).
104
+ *
105
+ * @param schema A Valibot schema to analyze.
106
+ * @returns An array of string representations of valid choices, or `undefined`
107
+ * if the schema does not represent a fixed set of values.
108
+ */
109
+ function inferChoices(schema) {
110
+ const internalSchema = schema;
111
+ const schemaType = internalSchema.type;
112
+ if (!schemaType) return void 0;
113
+ if (schemaType === "picklist") {
114
+ const options = internalSchema.options;
115
+ if (Array.isArray(options)) {
116
+ const result = [];
117
+ for (const opt of options) if (typeof opt === "string") result.push(opt);
118
+ else return void 0;
119
+ return result.length > 0 ? result : void 0;
120
+ }
121
+ return void 0;
122
+ }
123
+ if (schemaType === "literal") {
124
+ const value = internalSchema.literal;
125
+ if (typeof value === "string") return [value];
126
+ return void 0;
127
+ }
128
+ if (schemaType === "union") {
129
+ const options = internalSchema.options;
130
+ if (!Array.isArray(options)) return void 0;
131
+ const allChoices = /* @__PURE__ */ new Set();
132
+ for (const opt of options) if (typeof opt === "object" && opt != null && "type" in opt) {
133
+ const sub = inferChoices(opt);
134
+ if (sub == null) return void 0;
135
+ for (const choice of sub) allChoices.add(choice);
136
+ } else return void 0;
137
+ return allChoices.size > 0 ? [...allChoices] : void 0;
138
+ }
139
+ if (schemaType === "optional" || schemaType === "nullable" || schemaType === "nullish") {
140
+ const wrapped = internalSchema.wrapped;
141
+ if (wrapped) return inferChoices(wrapped);
142
+ }
143
+ return void 0;
144
+ }
145
+ /**
95
146
  * Creates a value parser from a Valibot schema.
96
147
  *
97
148
  * This parser validates CLI argument strings using Valibot schemas, enabling
@@ -171,9 +222,19 @@ function inferMetavar(schema) {
171
222
  * @since 0.7.0
172
223
  */
173
224
  function valibot$1(schema, options = {}) {
174
- return {
225
+ const choices = inferChoices(schema);
226
+ const parser = {
175
227
  $mode: "sync",
176
228
  metavar: options.metavar ?? inferMetavar(schema),
229
+ ...choices != null && choices.length > 0 ? {
230
+ choices: Object.freeze(choices),
231
+ *suggest(prefix) {
232
+ for (const c of choices) if (c.startsWith(prefix)) yield {
233
+ kind: "literal",
234
+ text: c
235
+ };
236
+ }
237
+ } : {},
177
238
  parse(input) {
178
239
  const result = (0, valibot.safeParse)(schema, input);
179
240
  if (result.success) return {
@@ -194,6 +255,7 @@ function valibot$1(schema, options = {}) {
194
255
  return String(value);
195
256
  }
196
257
  };
258
+ return parser;
197
259
  }
198
260
 
199
261
  //#endregion
package/dist/index.js CHANGED
@@ -60,8 +60,15 @@ function inferMetavar(schema) {
60
60
  if (schemaType === "boolean") return "BOOLEAN";
61
61
  if (schemaType === "date") return "DATE";
62
62
  if (schemaType === "picklist") return "CHOICE";
63
- if (schemaType === "literal") return "VALUE";
64
- if (schemaType === "union" || schemaType === "variant") return "VALUE";
63
+ if (schemaType === "literal") {
64
+ if (inferChoices(schema) != null) return "CHOICE";
65
+ return "VALUE";
66
+ }
67
+ if (schemaType === "union") {
68
+ if (inferChoices(schema) != null) return "CHOICE";
69
+ return "VALUE";
70
+ }
71
+ if (schemaType === "variant") return "VALUE";
65
72
  if (schemaType === "optional" || schemaType === "nullable" || schemaType === "nullish") {
66
73
  const wrapped = internalSchema.wrapped;
67
74
  if (wrapped) return inferMetavar(wrapped);
@@ -69,6 +76,50 @@ function inferMetavar(schema) {
69
76
  return "VALUE";
70
77
  }
71
78
  /**
79
+ * Extracts valid choices from a Valibot schema that represents a fixed set of
80
+ * values (picklist, literal, or union of literals).
81
+ *
82
+ * @param schema A Valibot schema to analyze.
83
+ * @returns An array of string representations of valid choices, or `undefined`
84
+ * if the schema does not represent a fixed set of values.
85
+ */
86
+ function inferChoices(schema) {
87
+ const internalSchema = schema;
88
+ const schemaType = internalSchema.type;
89
+ if (!schemaType) return void 0;
90
+ if (schemaType === "picklist") {
91
+ const options = internalSchema.options;
92
+ if (Array.isArray(options)) {
93
+ const result = [];
94
+ for (const opt of options) if (typeof opt === "string") result.push(opt);
95
+ else return void 0;
96
+ return result.length > 0 ? result : void 0;
97
+ }
98
+ return void 0;
99
+ }
100
+ if (schemaType === "literal") {
101
+ const value = internalSchema.literal;
102
+ if (typeof value === "string") return [value];
103
+ return void 0;
104
+ }
105
+ if (schemaType === "union") {
106
+ const options = internalSchema.options;
107
+ if (!Array.isArray(options)) return void 0;
108
+ const allChoices = /* @__PURE__ */ new Set();
109
+ for (const opt of options) if (typeof opt === "object" && opt != null && "type" in opt) {
110
+ const sub = inferChoices(opt);
111
+ if (sub == null) return void 0;
112
+ for (const choice of sub) allChoices.add(choice);
113
+ } else return void 0;
114
+ return allChoices.size > 0 ? [...allChoices] : void 0;
115
+ }
116
+ if (schemaType === "optional" || schemaType === "nullable" || schemaType === "nullish") {
117
+ const wrapped = internalSchema.wrapped;
118
+ if (wrapped) return inferChoices(wrapped);
119
+ }
120
+ return void 0;
121
+ }
122
+ /**
72
123
  * Creates a value parser from a Valibot schema.
73
124
  *
74
125
  * This parser validates CLI argument strings using Valibot schemas, enabling
@@ -148,9 +199,19 @@ function inferMetavar(schema) {
148
199
  * @since 0.7.0
149
200
  */
150
201
  function valibot(schema, options = {}) {
151
- return {
202
+ const choices = inferChoices(schema);
203
+ const parser = {
152
204
  $mode: "sync",
153
205
  metavar: options.metavar ?? inferMetavar(schema),
206
+ ...choices != null && choices.length > 0 ? {
207
+ choices: Object.freeze(choices),
208
+ *suggest(prefix) {
209
+ for (const c of choices) if (c.startsWith(prefix)) yield {
210
+ kind: "literal",
211
+ text: c
212
+ };
213
+ }
214
+ } : {},
154
215
  parse(input) {
155
216
  const result = safeParse(schema, input);
156
217
  if (result.success) return {
@@ -171,6 +232,7 @@ function valibot(schema, options = {}) {
171
232
  return String(value);
172
233
  }
173
234
  };
235
+ return parser;
174
236
  }
175
237
 
176
238
  //#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.1291+36a7f4c9",
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.1291+36a7f4c9"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@types/node": "^20.19.9",