@optique/zod 1.0.0-dev.1375 → 1.0.0-dev.1388

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
@@ -296,7 +296,17 @@ function zod(schema, options = {}) {
296
296
  };
297
297
  },
298
298
  format(value) {
299
- return String(value);
299
+ if (options.format) return options.format(value);
300
+ if (value instanceof Date) return Number.isNaN(value.getTime()) ? String(value) : value.toISOString();
301
+ if (typeof value !== "object" || value === null) return String(value);
302
+ if (Array.isArray(value)) return String(value);
303
+ const str = String(value);
304
+ if (str !== "[object Object]") return str;
305
+ const proto = Object.getPrototypeOf(value);
306
+ if (proto === Object.prototype || proto === null) try {
307
+ return JSON.stringify(value) ?? str;
308
+ } catch {}
309
+ return str;
300
310
  }
301
311
  };
302
312
  return parser;
package/dist/index.d.cts CHANGED
@@ -8,7 +8,7 @@ import { z } from "zod";
8
8
  * Options for creating a Zod value parser.
9
9
  * @since 0.7.0
10
10
  */
11
- interface ZodParserOptions {
11
+ interface ZodParserOptions<T = unknown> {
12
12
  /**
13
13
  * The metavariable name for this parser. This is used in help messages to
14
14
  * indicate what kind of value this parser expects. Usually a single
@@ -16,6 +16,18 @@ interface ZodParserOptions {
16
16
  * @default `"VALUE"`
17
17
  */
18
18
  readonly metavar?: NonEmptyString;
19
+ /**
20
+ * Custom formatter for displaying parsed values in help messages.
21
+ * When not provided, the default formatter is used: primitives use
22
+ * `String()`, valid `Date` values use `.toISOString()`, and plain
23
+ * objects use `JSON.stringify()`. All other objects (arrays, class
24
+ * instances, etc.) use `String()`.
25
+ *
26
+ * @param value The parsed value to format.
27
+ * @returns A string representation of the value.
28
+ * @since 1.0.0
29
+ */
30
+ readonly format?: (value: T) => string;
19
31
  /**
20
32
  * Custom error messages for Zod validation failures.
21
33
  */
@@ -102,6 +114,6 @@ interface ZodParserOptions {
102
114
  * operations that cannot be executed synchronously.
103
115
  * @since 0.7.0
104
116
  */
105
- declare function zod<T>(schema: z.Schema<T>, options?: ZodParserOptions): ValueParser<"sync", T>;
117
+ declare function zod<T>(schema: z.Schema<T>, options?: ZodParserOptions<T>): ValueParser<"sync", T>;
106
118
  //#endregion
107
119
  export { ZodParserOptions, zod };
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ import { z } from "zod";
8
8
  * Options for creating a Zod value parser.
9
9
  * @since 0.7.0
10
10
  */
11
- interface ZodParserOptions {
11
+ interface ZodParserOptions<T = unknown> {
12
12
  /**
13
13
  * The metavariable name for this parser. This is used in help messages to
14
14
  * indicate what kind of value this parser expects. Usually a single
@@ -16,6 +16,18 @@ interface ZodParserOptions {
16
16
  * @default `"VALUE"`
17
17
  */
18
18
  readonly metavar?: NonEmptyString;
19
+ /**
20
+ * Custom formatter for displaying parsed values in help messages.
21
+ * When not provided, the default formatter is used: primitives use
22
+ * `String()`, valid `Date` values use `.toISOString()`, and plain
23
+ * objects use `JSON.stringify()`. All other objects (arrays, class
24
+ * instances, etc.) use `String()`.
25
+ *
26
+ * @param value The parsed value to format.
27
+ * @returns A string representation of the value.
28
+ * @since 1.0.0
29
+ */
30
+ readonly format?: (value: T) => string;
19
31
  /**
20
32
  * Custom error messages for Zod validation failures.
21
33
  */
@@ -102,6 +114,6 @@ interface ZodParserOptions {
102
114
  * operations that cannot be executed synchronously.
103
115
  * @since 0.7.0
104
116
  */
105
- declare function zod<T>(schema: z.Schema<T>, options?: ZodParserOptions): ValueParser<"sync", T>;
117
+ declare function zod<T>(schema: z.Schema<T>, options?: ZodParserOptions<T>): ValueParser<"sync", T>;
106
118
  //#endregion
107
119
  export { ZodParserOptions, zod };
package/dist/index.js CHANGED
@@ -273,7 +273,17 @@ function zod(schema, options = {}) {
273
273
  };
274
274
  },
275
275
  format(value) {
276
- return String(value);
276
+ if (options.format) return options.format(value);
277
+ if (value instanceof Date) return Number.isNaN(value.getTime()) ? String(value) : value.toISOString();
278
+ if (typeof value !== "object" || value === null) return String(value);
279
+ if (Array.isArray(value)) return String(value);
280
+ const str = String(value);
281
+ if (str !== "[object Object]") return str;
282
+ const proto = Object.getPrototypeOf(value);
283
+ if (proto === Object.prototype || proto === null) try {
284
+ return JSON.stringify(value) ?? str;
285
+ } catch {}
286
+ return str;
277
287
  }
278
288
  };
279
289
  return parser;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/zod",
3
- "version": "1.0.0-dev.1375+69c819ec",
3
+ "version": "1.0.0-dev.1388+3660675f",
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.1375+69c819ec"
60
+ "@optique/core": "1.0.0-dev.1388+3660675f"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@types/node": "^20.19.9",