@optique/zod 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
 
26
27
  //#region src/index.ts
27
28
  /**
@@ -47,7 +48,7 @@ const __optique_core_message = __toESM(require("@optique/core/message"));
47
48
  function inferMetavar(schema) {
48
49
  const def = schema._def;
49
50
  if (!def) return "VALUE";
50
- const typeName = def.typeName || def.type;
51
+ const typeName = def.typeName ?? def.type;
51
52
  if (typeName === "ZodString" || typeName === "string") {
52
53
  if (Array.isArray(def.checks)) for (const check of def.checks) {
53
54
  const kind = check.kind || check.format;
@@ -76,8 +77,15 @@ function inferMetavar(schema) {
76
77
  }
77
78
  if (typeName === "ZodBoolean" || typeName === "boolean") return "BOOLEAN";
78
79
  if (typeName === "ZodDate" || typeName === "date") return "DATE";
79
- if (typeName === "ZodEnum" || typeName === "enum" || typeName === "ZodNativeEnum" || typeName === "nativeEnum") return "CHOICE";
80
- if (typeName === "ZodUnion" || typeName === "union" || typeName === "ZodLiteral" || typeName === "literal") return "VALUE";
80
+ if (typeName === "ZodEnum" || typeName === "enum" || typeName === "ZodNativeEnum" || typeName === "nativeEnum") return inferChoices(schema) != null ? "CHOICE" : "VALUE";
81
+ if (typeName === "ZodLiteral" || typeName === "literal") {
82
+ if (inferChoices(schema) != null) return "CHOICE";
83
+ return "VALUE";
84
+ }
85
+ if (typeName === "ZodUnion" || typeName === "union") {
86
+ if (inferChoices(schema) != null) return "CHOICE";
87
+ return "VALUE";
88
+ }
81
89
  if (typeName === "ZodOptional" || typeName === "optional" || typeName === "ZodNullable" || typeName === "nullable") {
82
90
  const innerType = def.innerType;
83
91
  if (innerType != null) return inferMetavar(innerType);
@@ -91,6 +99,70 @@ function inferMetavar(schema) {
91
99
  return "VALUE";
92
100
  }
93
101
  /**
102
+ * Extracts valid choices from a Zod schema that represents a fixed set of
103
+ * values (enum, literal, or union of literals).
104
+ *
105
+ * @param schema A Zod 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 def = schema._def;
111
+ if (!def) return void 0;
112
+ const typeName = def.typeName ?? def.type;
113
+ if (typeName === "ZodEnum" || typeName === "enum") {
114
+ const values = def.values;
115
+ if (Array.isArray(values)) return values.map(String);
116
+ const entries = def.entries;
117
+ if (entries != null && typeof entries === "object") {
118
+ const result = /* @__PURE__ */ new Set();
119
+ for (const val of Object.values(entries)) if (typeof val === "string") result.add(val);
120
+ else return void 0;
121
+ return result.size > 0 ? [...result] : void 0;
122
+ }
123
+ return void 0;
124
+ }
125
+ if (typeName === "ZodNativeEnum" || typeName === "nativeEnum") {
126
+ const values = def.values;
127
+ if (values != null && typeof values === "object" && !Array.isArray(values)) {
128
+ const result = /* @__PURE__ */ new Set();
129
+ for (const val of Object.values(values)) if (typeof val === "string") result.add(val);
130
+ else return void 0;
131
+ return result.size > 0 ? [...result] : void 0;
132
+ }
133
+ return void 0;
134
+ }
135
+ if (typeName === "ZodLiteral" || typeName === "literal") {
136
+ const value = def.value;
137
+ if (typeof value === "string") return [value];
138
+ const values = def.values;
139
+ if (Array.isArray(values)) {
140
+ const result = [];
141
+ for (const v of values) if (typeof v === "string") result.push(v);
142
+ else return void 0;
143
+ return result.length > 0 ? result : void 0;
144
+ }
145
+ return void 0;
146
+ }
147
+ if (typeName === "ZodUnion" || typeName === "union") {
148
+ const options = def.options;
149
+ if (!Array.isArray(options)) return void 0;
150
+ const allChoices = /* @__PURE__ */ new Set();
151
+ for (const opt of options) {
152
+ const sub = inferChoices(opt);
153
+ if (sub == null) return void 0;
154
+ for (const choice of sub) allChoices.add(choice);
155
+ }
156
+ return allChoices.size > 0 ? [...allChoices] : void 0;
157
+ }
158
+ if (typeName === "ZodOptional" || typeName === "optional" || typeName === "ZodNullable" || typeName === "nullable" || typeName === "ZodDefault" || typeName === "default") {
159
+ const innerType = def.innerType;
160
+ if (innerType != null) return inferChoices(innerType);
161
+ return void 0;
162
+ }
163
+ return void 0;
164
+ }
165
+ /**
94
166
  * Creates a value parser from a Zod schema.
95
167
  *
96
168
  * This parser validates CLI argument strings using Zod schemas, enabling
@@ -158,12 +230,25 @@ function inferMetavar(schema) {
158
230
  * }));
159
231
  * ```
160
232
  *
233
+ * @throws {TypeError} If the resolved `metavar` is an empty string.
161
234
  * @since 0.7.0
162
235
  */
163
236
  function zod(schema, options = {}) {
164
- return {
237
+ const choices = inferChoices(schema);
238
+ const metavar = options.metavar ?? inferMetavar(schema);
239
+ (0, __optique_core_nonempty.ensureNonEmptyString)(metavar);
240
+ const parser = {
165
241
  $mode: "sync",
166
- metavar: options.metavar ?? inferMetavar(schema),
242
+ metavar,
243
+ ...choices != null && choices.length > 0 ? {
244
+ choices: Object.freeze(choices),
245
+ *suggest(prefix) {
246
+ for (const c of choices) if (c.startsWith(prefix)) yield {
247
+ kind: "literal",
248
+ text: c
249
+ };
250
+ }
251
+ } : {},
167
252
  parse(input) {
168
253
  const result = schema.safeParse(input);
169
254
  if (result.success) return {
@@ -192,6 +277,7 @@ function zod(schema, options = {}) {
192
277
  return String(value);
193
278
  }
194
279
  };
280
+ return parser;
195
281
  }
196
282
 
197
283
  //#endregion
package/dist/index.d.cts CHANGED
@@ -97,6 +97,7 @@ interface ZodParserOptions {
97
97
  * }));
98
98
  * ```
99
99
  *
100
+ * @throws {TypeError} If the resolved `metavar` is an empty string.
100
101
  * @since 0.7.0
101
102
  */
102
103
  declare function zod<T>(schema: z.Schema<T>, options?: ZodParserOptions): ValueParser<"sync", T>;
package/dist/index.d.ts CHANGED
@@ -97,6 +97,7 @@ interface ZodParserOptions {
97
97
  * }));
98
98
  * ```
99
99
  *
100
+ * @throws {TypeError} If the resolved `metavar` is an empty string.
100
101
  * @since 0.7.0
101
102
  */
102
103
  declare function zod<T>(schema: z.Schema<T>, options?: ZodParserOptions): 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
 
3
4
  //#region src/index.ts
4
5
  /**
@@ -24,7 +25,7 @@ import { message } from "@optique/core/message";
24
25
  function inferMetavar(schema) {
25
26
  const def = schema._def;
26
27
  if (!def) return "VALUE";
27
- const typeName = def.typeName || def.type;
28
+ const typeName = def.typeName ?? def.type;
28
29
  if (typeName === "ZodString" || typeName === "string") {
29
30
  if (Array.isArray(def.checks)) for (const check of def.checks) {
30
31
  const kind = check.kind || check.format;
@@ -53,8 +54,15 @@ function inferMetavar(schema) {
53
54
  }
54
55
  if (typeName === "ZodBoolean" || typeName === "boolean") return "BOOLEAN";
55
56
  if (typeName === "ZodDate" || typeName === "date") return "DATE";
56
- if (typeName === "ZodEnum" || typeName === "enum" || typeName === "ZodNativeEnum" || typeName === "nativeEnum") return "CHOICE";
57
- if (typeName === "ZodUnion" || typeName === "union" || typeName === "ZodLiteral" || typeName === "literal") return "VALUE";
57
+ if (typeName === "ZodEnum" || typeName === "enum" || typeName === "ZodNativeEnum" || typeName === "nativeEnum") return inferChoices(schema) != null ? "CHOICE" : "VALUE";
58
+ if (typeName === "ZodLiteral" || typeName === "literal") {
59
+ if (inferChoices(schema) != null) return "CHOICE";
60
+ return "VALUE";
61
+ }
62
+ if (typeName === "ZodUnion" || typeName === "union") {
63
+ if (inferChoices(schema) != null) return "CHOICE";
64
+ return "VALUE";
65
+ }
58
66
  if (typeName === "ZodOptional" || typeName === "optional" || typeName === "ZodNullable" || typeName === "nullable") {
59
67
  const innerType = def.innerType;
60
68
  if (innerType != null) return inferMetavar(innerType);
@@ -68,6 +76,70 @@ function inferMetavar(schema) {
68
76
  return "VALUE";
69
77
  }
70
78
  /**
79
+ * Extracts valid choices from a Zod schema that represents a fixed set of
80
+ * values (enum, literal, or union of literals).
81
+ *
82
+ * @param schema A Zod 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 def = schema._def;
88
+ if (!def) return void 0;
89
+ const typeName = def.typeName ?? def.type;
90
+ if (typeName === "ZodEnum" || typeName === "enum") {
91
+ const values = def.values;
92
+ if (Array.isArray(values)) return values.map(String);
93
+ const entries = def.entries;
94
+ if (entries != null && typeof entries === "object") {
95
+ const result = /* @__PURE__ */ new Set();
96
+ for (const val of Object.values(entries)) if (typeof val === "string") result.add(val);
97
+ else return void 0;
98
+ return result.size > 0 ? [...result] : void 0;
99
+ }
100
+ return void 0;
101
+ }
102
+ if (typeName === "ZodNativeEnum" || typeName === "nativeEnum") {
103
+ const values = def.values;
104
+ if (values != null && typeof values === "object" && !Array.isArray(values)) {
105
+ const result = /* @__PURE__ */ new Set();
106
+ for (const val of Object.values(values)) if (typeof val === "string") result.add(val);
107
+ else return void 0;
108
+ return result.size > 0 ? [...result] : void 0;
109
+ }
110
+ return void 0;
111
+ }
112
+ if (typeName === "ZodLiteral" || typeName === "literal") {
113
+ const value = def.value;
114
+ if (typeof value === "string") return [value];
115
+ const values = def.values;
116
+ if (Array.isArray(values)) {
117
+ const result = [];
118
+ for (const v of values) if (typeof v === "string") result.push(v);
119
+ else return void 0;
120
+ return result.length > 0 ? result : void 0;
121
+ }
122
+ return void 0;
123
+ }
124
+ if (typeName === "ZodUnion" || typeName === "union") {
125
+ const options = def.options;
126
+ if (!Array.isArray(options)) return void 0;
127
+ const allChoices = /* @__PURE__ */ new Set();
128
+ for (const opt of options) {
129
+ const sub = inferChoices(opt);
130
+ if (sub == null) return void 0;
131
+ for (const choice of sub) allChoices.add(choice);
132
+ }
133
+ return allChoices.size > 0 ? [...allChoices] : void 0;
134
+ }
135
+ if (typeName === "ZodOptional" || typeName === "optional" || typeName === "ZodNullable" || typeName === "nullable" || typeName === "ZodDefault" || typeName === "default") {
136
+ const innerType = def.innerType;
137
+ if (innerType != null) return inferChoices(innerType);
138
+ return void 0;
139
+ }
140
+ return void 0;
141
+ }
142
+ /**
71
143
  * Creates a value parser from a Zod schema.
72
144
  *
73
145
  * This parser validates CLI argument strings using Zod schemas, enabling
@@ -135,12 +207,25 @@ function inferMetavar(schema) {
135
207
  * }));
136
208
  * ```
137
209
  *
210
+ * @throws {TypeError} If the resolved `metavar` is an empty string.
138
211
  * @since 0.7.0
139
212
  */
140
213
  function zod(schema, options = {}) {
141
- return {
214
+ const choices = inferChoices(schema);
215
+ const metavar = options.metavar ?? inferMetavar(schema);
216
+ ensureNonEmptyString(metavar);
217
+ const parser = {
142
218
  $mode: "sync",
143
- metavar: options.metavar ?? inferMetavar(schema),
219
+ metavar,
220
+ ...choices != null && choices.length > 0 ? {
221
+ choices: Object.freeze(choices),
222
+ *suggest(prefix) {
223
+ for (const c of choices) if (c.startsWith(prefix)) yield {
224
+ kind: "literal",
225
+ text: c
226
+ };
227
+ }
228
+ } : {},
144
229
  parse(input) {
145
230
  const result = schema.safeParse(input);
146
231
  if (result.success) return {
@@ -169,6 +254,7 @@ function zod(schema, options = {}) {
169
254
  return String(value);
170
255
  }
171
256
  };
257
+ return parser;
172
258
  }
173
259
 
174
260
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/zod",
3
- "version": "1.0.0-dev.1290+a999711d",
3
+ "version": "1.0.0-dev.1300+12293782",
4
4
  "description": "Zod value parsers for Optique",
5
5
  "keywords": [
6
6
  "CLI",
@@ -57,7 +57,7 @@
57
57
  "zod": "^3.25.0 || ^4.0.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",