@optique/valibot 1.0.0-dev.1375 → 1.0.0-dev.1390

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
@@ -401,7 +401,17 @@ function valibot$1(schema, options = {}) {
401
401
  };
402
402
  },
403
403
  format(value) {
404
- return String(value);
404
+ if (options.format) return options.format(value);
405
+ if (value instanceof Date) return Number.isNaN(value.getTime()) ? String(value) : value.toISOString();
406
+ if (typeof value !== "object" || value === null) return String(value);
407
+ if (Array.isArray(value)) return String(value);
408
+ const str = String(value);
409
+ if (str !== "[object Object]") return str;
410
+ const proto = Object.getPrototypeOf(value);
411
+ if (proto === Object.prototype || proto === null) try {
412
+ return JSON.stringify(value) ?? str;
413
+ } catch {}
414
+ return str;
405
415
  }
406
416
  };
407
417
  return parser;
package/dist/index.d.cts CHANGED
@@ -8,7 +8,7 @@ import * as v from "valibot";
8
8
  * Options for creating a Valibot value parser.
9
9
  * @since 0.7.0
10
10
  */
11
- interface ValibotParserOptions {
11
+ interface ValibotParserOptions<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 ValibotParserOptions {
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 Valibot validation failures.
21
33
  */
@@ -111,6 +123,6 @@ interface ValibotParserOptions {
111
123
  * executed synchronously.
112
124
  * @since 0.7.0
113
125
  */
114
- declare function valibot<T>(schema: v.BaseSchema<unknown, T, v.BaseIssue<unknown>>, options?: ValibotParserOptions): ValueParser<"sync", T>;
126
+ declare function valibot<T>(schema: v.BaseSchema<unknown, T, v.BaseIssue<unknown>>, options?: ValibotParserOptions<T>): ValueParser<"sync", T>;
115
127
  //#endregion
116
128
  export { ValibotParserOptions, valibot };
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ import { NonEmptyString, ValueParser } from "@optique/core/valueparser";
8
8
  * Options for creating a Valibot value parser.
9
9
  * @since 0.7.0
10
10
  */
11
- interface ValibotParserOptions {
11
+ interface ValibotParserOptions<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 ValibotParserOptions {
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 Valibot validation failures.
21
33
  */
@@ -111,6 +123,6 @@ interface ValibotParserOptions {
111
123
  * executed synchronously.
112
124
  * @since 0.7.0
113
125
  */
114
- declare function valibot<T>(schema: v.BaseSchema<unknown, T, v.BaseIssue<unknown>>, options?: ValibotParserOptions): ValueParser<"sync", T>;
126
+ declare function valibot<T>(schema: v.BaseSchema<unknown, T, v.BaseIssue<unknown>>, options?: ValibotParserOptions<T>): ValueParser<"sync", T>;
115
127
  //#endregion
116
128
  export { ValibotParserOptions, valibot };
package/dist/index.js CHANGED
@@ -378,7 +378,17 @@ function valibot(schema, options = {}) {
378
378
  };
379
379
  },
380
380
  format(value) {
381
- return String(value);
381
+ if (options.format) return options.format(value);
382
+ if (value instanceof Date) return Number.isNaN(value.getTime()) ? String(value) : value.toISOString();
383
+ if (typeof value !== "object" || value === null) return String(value);
384
+ if (Array.isArray(value)) return String(value);
385
+ const str = String(value);
386
+ if (str !== "[object Object]") return str;
387
+ const proto = Object.getPrototypeOf(value);
388
+ if (proto === Object.prototype || proto === null) try {
389
+ return JSON.stringify(value) ?? str;
390
+ } catch {}
391
+ return str;
382
392
  }
383
393
  };
384
394
  return parser;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/valibot",
3
- "version": "1.0.0-dev.1375+69c819ec",
3
+ "version": "1.0.0-dev.1390+84b0f473",
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.1375+69c819ec"
60
+ "@optique/core": "1.0.0-dev.1390+84b0f473"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@types/node": "^20.19.9",