@optique/zod 0.7.0-dev.1 → 0.7.0-dev.144

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
@@ -25,12 +25,78 @@ const __optique_core_message = __toESM(require("@optique/core/message"));
25
25
 
26
26
  //#region src/index.ts
27
27
  /**
28
+ * Infers an appropriate metavar string from a Zod schema.
29
+ *
30
+ * This function analyzes the Zod schema's internal structure to determine
31
+ * the most appropriate metavar string for help text generation.
32
+ *
33
+ * @param schema A Zod schema to analyze.
34
+ * @returns The inferred metavar string.
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * inferMetavar(z.string()) // "STRING"
39
+ * inferMetavar(z.string().email()) // "EMAIL"
40
+ * inferMetavar(z.coerce.number()) // "NUMBER"
41
+ * inferMetavar(z.coerce.number().int()) // "INTEGER"
42
+ * inferMetavar(z.enum(["a", "b"])) // "CHOICE"
43
+ * ```
44
+ *
45
+ * @since 0.7.0
46
+ */
47
+ function inferMetavar(schema) {
48
+ const def = schema._def;
49
+ if (!def) return "VALUE";
50
+ const typeName = def.typeName || def.type;
51
+ if (typeName === "ZodString" || typeName === "string") {
52
+ if (Array.isArray(def.checks)) for (const check of def.checks) {
53
+ const kind = check.kind || check.format;
54
+ if (kind === "email") return "EMAIL";
55
+ if (kind === "url") return "URL";
56
+ if (kind === "uuid") return "UUID";
57
+ if (kind === "datetime") return "DATETIME";
58
+ if (kind === "date") return "DATE";
59
+ if (kind === "time") return "TIME";
60
+ if (kind === "duration") return "DURATION";
61
+ if (kind === "ip") return check.version === "v4" ? "IPV4" : check.version === "v6" ? "IPV6" : "IP";
62
+ if (kind === "jwt") return "JWT";
63
+ if (kind === "emoji") return "EMOJI";
64
+ if (kind === "cuid") return "CUID";
65
+ if (kind === "cuid2") return "CUID2";
66
+ if (kind === "ulid") return "ULID";
67
+ if (kind === "base64") return "BASE64";
68
+ }
69
+ return "STRING";
70
+ }
71
+ if (typeName === "ZodNumber" || typeName === "number") {
72
+ if (Array.isArray(def.checks)) {
73
+ for (const check of def.checks) if (check.kind === "int" || check.isInt === true || check.format === "safeint") return "INTEGER";
74
+ }
75
+ return "NUMBER";
76
+ }
77
+ if (typeName === "ZodBoolean" || typeName === "boolean") return "BOOLEAN";
78
+ 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";
81
+ if (typeName === "ZodOptional" || typeName === "optional" || typeName === "ZodNullable" || typeName === "nullable") return inferMetavar(def.innerType);
82
+ if (typeName === "ZodDefault" || typeName === "default") return inferMetavar(def.innerType);
83
+ return "VALUE";
84
+ }
85
+ /**
28
86
  * Creates a value parser from a Zod schema.
29
87
  *
30
88
  * This parser validates CLI argument strings using Zod schemas, enabling
31
89
  * powerful validation and transformation capabilities for command-line
32
90
  * interfaces.
33
91
  *
92
+ * The metavar is automatically inferred from the schema type unless explicitly
93
+ * provided in options. For example:
94
+ * - `z.string()` → `"STRING"`
95
+ * - `z.string().email()` → `"EMAIL"`
96
+ * - `z.coerce.number()` → `"NUMBER"`
97
+ * - `z.coerce.number().int()` → `"INTEGER"`
98
+ * - `z.enum([...])` → `"CHOICE"`
99
+ *
34
100
  * @template T The output type of the Zod schema.
35
101
  * @param schema A Zod schema to validate input against.
36
102
  * @param options Optional configuration for the parser.
@@ -88,7 +154,7 @@ const __optique_core_message = __toESM(require("@optique/core/message"));
88
154
  */
89
155
  function zod(schema, options = {}) {
90
156
  return {
91
- metavar: options.metavar ?? "VALUE",
157
+ metavar: options.metavar ?? inferMetavar(schema),
92
158
  parse(input) {
93
159
  const result = schema.safeParse(input);
94
160
  if (result.success) return {
package/dist/index.d.cts CHANGED
@@ -36,6 +36,14 @@ interface ZodParserOptions {
36
36
  * powerful validation and transformation capabilities for command-line
37
37
  * interfaces.
38
38
  *
39
+ * The metavar is automatically inferred from the schema type unless explicitly
40
+ * provided in options. For example:
41
+ * - `z.string()` → `"STRING"`
42
+ * - `z.string().email()` → `"EMAIL"`
43
+ * - `z.coerce.number()` → `"NUMBER"`
44
+ * - `z.coerce.number().int()` → `"INTEGER"`
45
+ * - `z.enum([...])` → `"CHOICE"`
46
+ *
39
47
  * @template T The output type of the Zod schema.
40
48
  * @param schema A Zod schema to validate input against.
41
49
  * @param options Optional configuration for the parser.
package/dist/index.d.ts CHANGED
@@ -36,6 +36,14 @@ interface ZodParserOptions {
36
36
  * powerful validation and transformation capabilities for command-line
37
37
  * interfaces.
38
38
  *
39
+ * The metavar is automatically inferred from the schema type unless explicitly
40
+ * provided in options. For example:
41
+ * - `z.string()` → `"STRING"`
42
+ * - `z.string().email()` → `"EMAIL"`
43
+ * - `z.coerce.number()` → `"NUMBER"`
44
+ * - `z.coerce.number().int()` → `"INTEGER"`
45
+ * - `z.enum([...])` → `"CHOICE"`
46
+ *
39
47
  * @template T The output type of the Zod schema.
40
48
  * @param schema A Zod schema to validate input against.
41
49
  * @param options Optional configuration for the parser.
package/dist/index.js CHANGED
@@ -2,12 +2,78 @@ import { message } from "@optique/core/message";
2
2
 
3
3
  //#region src/index.ts
4
4
  /**
5
+ * Infers an appropriate metavar string from a Zod schema.
6
+ *
7
+ * This function analyzes the Zod schema's internal structure to determine
8
+ * the most appropriate metavar string for help text generation.
9
+ *
10
+ * @param schema A Zod schema to analyze.
11
+ * @returns The inferred metavar string.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * inferMetavar(z.string()) // "STRING"
16
+ * inferMetavar(z.string().email()) // "EMAIL"
17
+ * inferMetavar(z.coerce.number()) // "NUMBER"
18
+ * inferMetavar(z.coerce.number().int()) // "INTEGER"
19
+ * inferMetavar(z.enum(["a", "b"])) // "CHOICE"
20
+ * ```
21
+ *
22
+ * @since 0.7.0
23
+ */
24
+ function inferMetavar(schema) {
25
+ const def = schema._def;
26
+ if (!def) return "VALUE";
27
+ const typeName = def.typeName || def.type;
28
+ if (typeName === "ZodString" || typeName === "string") {
29
+ if (Array.isArray(def.checks)) for (const check of def.checks) {
30
+ const kind = check.kind || check.format;
31
+ if (kind === "email") return "EMAIL";
32
+ if (kind === "url") return "URL";
33
+ if (kind === "uuid") return "UUID";
34
+ if (kind === "datetime") return "DATETIME";
35
+ if (kind === "date") return "DATE";
36
+ if (kind === "time") return "TIME";
37
+ if (kind === "duration") return "DURATION";
38
+ if (kind === "ip") return check.version === "v4" ? "IPV4" : check.version === "v6" ? "IPV6" : "IP";
39
+ if (kind === "jwt") return "JWT";
40
+ if (kind === "emoji") return "EMOJI";
41
+ if (kind === "cuid") return "CUID";
42
+ if (kind === "cuid2") return "CUID2";
43
+ if (kind === "ulid") return "ULID";
44
+ if (kind === "base64") return "BASE64";
45
+ }
46
+ return "STRING";
47
+ }
48
+ if (typeName === "ZodNumber" || typeName === "number") {
49
+ if (Array.isArray(def.checks)) {
50
+ for (const check of def.checks) if (check.kind === "int" || check.isInt === true || check.format === "safeint") return "INTEGER";
51
+ }
52
+ return "NUMBER";
53
+ }
54
+ if (typeName === "ZodBoolean" || typeName === "boolean") return "BOOLEAN";
55
+ 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";
58
+ if (typeName === "ZodOptional" || typeName === "optional" || typeName === "ZodNullable" || typeName === "nullable") return inferMetavar(def.innerType);
59
+ if (typeName === "ZodDefault" || typeName === "default") return inferMetavar(def.innerType);
60
+ return "VALUE";
61
+ }
62
+ /**
5
63
  * Creates a value parser from a Zod schema.
6
64
  *
7
65
  * This parser validates CLI argument strings using Zod schemas, enabling
8
66
  * powerful validation and transformation capabilities for command-line
9
67
  * interfaces.
10
68
  *
69
+ * The metavar is automatically inferred from the schema type unless explicitly
70
+ * provided in options. For example:
71
+ * - `z.string()` → `"STRING"`
72
+ * - `z.string().email()` → `"EMAIL"`
73
+ * - `z.coerce.number()` → `"NUMBER"`
74
+ * - `z.coerce.number().int()` → `"INTEGER"`
75
+ * - `z.enum([...])` → `"CHOICE"`
76
+ *
11
77
  * @template T The output type of the Zod schema.
12
78
  * @param schema A Zod schema to validate input against.
13
79
  * @param options Optional configuration for the parser.
@@ -65,7 +131,7 @@ import { message } from "@optique/core/message";
65
131
  */
66
132
  function zod(schema, options = {}) {
67
133
  return {
68
- metavar: options.metavar ?? "VALUE",
134
+ metavar: options.metavar ?? inferMetavar(schema),
69
135
  parse(input) {
70
136
  const result = schema.safeParse(input);
71
137
  if (result.success) return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/zod",
3
- "version": "0.7.0-dev.1",
3
+ "version": "0.7.0-dev.144+da70df70",
4
4
  "description": "Zod value parsers for Optique",
5
5
  "keywords": [
6
6
  "CLI",
@@ -63,7 +63,7 @@
63
63
  "@types/node": "^20.19.9",
64
64
  "tsdown": "^0.13.0",
65
65
  "typescript": "^5.8.3",
66
- "zod": "^4.0.1"
66
+ "zod": "^3.25.0 || ^4.0.0"
67
67
  },
68
68
  "scripts": {
69
69
  "build": "tsdown",