@optique/standard-schema 1.2.0-dev.0 → 1.2.0-dev.2298

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,19 +25,39 @@ const __optique_core_message = __toESM(require("@optique/core/message"));
25
25
  const __optique_core_nonempty = __toESM(require("@optique/core/nonempty"));
26
26
 
27
27
  //#region src/index.ts
28
+ function getTypeName(value) {
29
+ if (value === null) return "null";
30
+ if (Array.isArray(value)) return "array";
31
+ return typeof value;
32
+ }
33
+ function validateSchema(functionName, schema) {
34
+ if (schema == null || typeof schema !== "object" && typeof schema !== "function" || !("~standard" in schema)) throw new TypeError(`${functionName}() requires a Standard Schema-compatible validator, got ${getTypeName(schema)}.`);
35
+ const standard = schema["~standard"];
36
+ if (standard == null || typeof standard !== "object" && typeof standard !== "function" || typeof standard.validate !== "function") throw new TypeError(`${functionName}() requires a Standard Schema-compatible validator, got ${getTypeName(schema)}.`);
37
+ }
28
38
  function validateOptions(functionName, options) {
29
- if (options == null || typeof options !== "object" || Array.isArray(options)) throw new TypeError(`${functionName}() requires an options object with a placeholder property.`);
30
- if (!("placeholder" in options)) throw new TypeError(`${functionName}() options must include a placeholder property.`);
39
+ if (options == null || typeof options !== "object") throw new TypeError(`${functionName}() requires an options object.`);
40
+ if (Array.isArray(options)) throw new TypeError(`${functionName}() requires an options object, got array.`);
41
+ const prototype = Object.getPrototypeOf(options);
42
+ if (prototype !== Object.prototype && prototype !== null) throw new TypeError(`${functionName}() requires an options object.`);
43
+ if (!Object.hasOwn(options, "placeholder")) throw new TypeError(`${functionName}() options must include a placeholder property.`);
31
44
  }
32
45
  function isPromiseLike(value) {
33
46
  return value != null && (typeof value === "object" || typeof value === "function") && "then" in value && typeof value.then === "function";
34
47
  }
48
+ function safeString(value) {
49
+ try {
50
+ return String(value);
51
+ } catch {
52
+ return "[object Object]";
53
+ }
54
+ }
35
55
  function formatValue(value, format) {
36
56
  if (format) return format(value);
37
- if (value instanceof Date) return Number.isNaN(value.getTime()) ? String(value) : value.toISOString();
38
- if (typeof value !== "object" || value === null) return String(value);
39
- if (Array.isArray(value)) return String(value);
40
- const str = String(value);
57
+ if (value instanceof Date) return Number.isNaN(value.getTime()) ? safeString(value) : value.toISOString();
58
+ if (typeof value !== "object" || value === null) return safeString(value);
59
+ if (Array.isArray(value)) return safeString(value);
60
+ const str = safeString(value);
41
61
  if (str !== "[object Object]") return str;
42
62
  const proto = Object.getPrototypeOf(value);
43
63
  if (proto === Object.prototype || proto === null) try {
@@ -52,7 +72,7 @@ function validationFailure(issues, input, error) {
52
72
  };
53
73
  return {
54
74
  success: false,
55
- error: __optique_core_message.message`${(0, __optique_core_message.text)(issues[0]?.message ?? "Validation failed")}`
75
+ error: __optique_core_message.message`${(0, __optique_core_message.text)(issues[0]?.message ?? "Validation failed.")}`
56
76
  };
57
77
  }
58
78
  function processValidationResult(result, input, options) {
@@ -85,6 +105,7 @@ function processValidationResult(result, input, options) {
85
105
  * @since 1.2.0
86
106
  */
87
107
  function standardSchema(schema, options) {
108
+ validateSchema("standardSchema", schema);
88
109
  validateOptions("standardSchema", options);
89
110
  const metavar = options.metavar ?? "VALUE";
90
111
  (0, __optique_core_nonempty.ensureNonEmptyString)(metavar);
@@ -94,7 +115,10 @@ function standardSchema(schema, options) {
94
115
  placeholder: options.placeholder,
95
116
  parse(input) {
96
117
  const result = schema["~standard"].validate(input);
97
- if (isPromiseLike(result)) throw new TypeError("Async Standard Schema validators are not supported by standardSchema(). Use standardSchemaAsync() instead.");
118
+ if (isPromiseLike(result)) {
119
+ result.then(void 0, () => void 0);
120
+ throw new TypeError("Async Standard Schema validators are not supported by standardSchema(). Use standardSchemaAsync() instead.");
121
+ }
98
122
  return processValidationResult(result, input, options);
99
123
  },
100
124
  format(value) {
@@ -123,6 +147,7 @@ function standardSchema(schema, options) {
123
147
  * @since 1.2.0
124
148
  */
125
149
  function standardSchemaAsync(schema, options) {
150
+ validateSchema("standardSchemaAsync", schema);
126
151
  validateOptions("standardSchemaAsync", options);
127
152
  const metavar = options.metavar ?? "VALUE";
128
153
  (0, __optique_core_nonempty.ensureNonEmptyString)(metavar);
package/dist/index.js CHANGED
@@ -2,19 +2,39 @@ import { message, text } from "@optique/core/message";
2
2
  import { ensureNonEmptyString } from "@optique/core/nonempty";
3
3
 
4
4
  //#region src/index.ts
5
+ function getTypeName(value) {
6
+ if (value === null) return "null";
7
+ if (Array.isArray(value)) return "array";
8
+ return typeof value;
9
+ }
10
+ function validateSchema(functionName, schema) {
11
+ if (schema == null || typeof schema !== "object" && typeof schema !== "function" || !("~standard" in schema)) throw new TypeError(`${functionName}() requires a Standard Schema-compatible validator, got ${getTypeName(schema)}.`);
12
+ const standard = schema["~standard"];
13
+ if (standard == null || typeof standard !== "object" && typeof standard !== "function" || typeof standard.validate !== "function") throw new TypeError(`${functionName}() requires a Standard Schema-compatible validator, got ${getTypeName(schema)}.`);
14
+ }
5
15
  function validateOptions(functionName, options) {
6
- if (options == null || typeof options !== "object" || Array.isArray(options)) throw new TypeError(`${functionName}() requires an options object with a placeholder property.`);
7
- if (!("placeholder" in options)) throw new TypeError(`${functionName}() options must include a placeholder property.`);
16
+ if (options == null || typeof options !== "object") throw new TypeError(`${functionName}() requires an options object.`);
17
+ if (Array.isArray(options)) throw new TypeError(`${functionName}() requires an options object, got array.`);
18
+ const prototype = Object.getPrototypeOf(options);
19
+ if (prototype !== Object.prototype && prototype !== null) throw new TypeError(`${functionName}() requires an options object.`);
20
+ if (!Object.hasOwn(options, "placeholder")) throw new TypeError(`${functionName}() options must include a placeholder property.`);
8
21
  }
9
22
  function isPromiseLike(value) {
10
23
  return value != null && (typeof value === "object" || typeof value === "function") && "then" in value && typeof value.then === "function";
11
24
  }
25
+ function safeString(value) {
26
+ try {
27
+ return String(value);
28
+ } catch {
29
+ return "[object Object]";
30
+ }
31
+ }
12
32
  function formatValue(value, format) {
13
33
  if (format) return format(value);
14
- if (value instanceof Date) return Number.isNaN(value.getTime()) ? String(value) : value.toISOString();
15
- if (typeof value !== "object" || value === null) return String(value);
16
- if (Array.isArray(value)) return String(value);
17
- const str = String(value);
34
+ if (value instanceof Date) return Number.isNaN(value.getTime()) ? safeString(value) : value.toISOString();
35
+ if (typeof value !== "object" || value === null) return safeString(value);
36
+ if (Array.isArray(value)) return safeString(value);
37
+ const str = safeString(value);
18
38
  if (str !== "[object Object]") return str;
19
39
  const proto = Object.getPrototypeOf(value);
20
40
  if (proto === Object.prototype || proto === null) try {
@@ -29,7 +49,7 @@ function validationFailure(issues, input, error) {
29
49
  };
30
50
  return {
31
51
  success: false,
32
- error: message`${text(issues[0]?.message ?? "Validation failed")}`
52
+ error: message`${text(issues[0]?.message ?? "Validation failed.")}`
33
53
  };
34
54
  }
35
55
  function processValidationResult(result, input, options) {
@@ -62,6 +82,7 @@ function processValidationResult(result, input, options) {
62
82
  * @since 1.2.0
63
83
  */
64
84
  function standardSchema(schema, options) {
85
+ validateSchema("standardSchema", schema);
65
86
  validateOptions("standardSchema", options);
66
87
  const metavar = options.metavar ?? "VALUE";
67
88
  ensureNonEmptyString(metavar);
@@ -71,7 +92,10 @@ function standardSchema(schema, options) {
71
92
  placeholder: options.placeholder,
72
93
  parse(input) {
73
94
  const result = schema["~standard"].validate(input);
74
- if (isPromiseLike(result)) throw new TypeError("Async Standard Schema validators are not supported by standardSchema(). Use standardSchemaAsync() instead.");
95
+ if (isPromiseLike(result)) {
96
+ result.then(void 0, () => void 0);
97
+ throw new TypeError("Async Standard Schema validators are not supported by standardSchema(). Use standardSchemaAsync() instead.");
98
+ }
75
99
  return processValidationResult(result, input, options);
76
100
  },
77
101
  format(value) {
@@ -100,6 +124,7 @@ function standardSchema(schema, options) {
100
124
  * @since 1.2.0
101
125
  */
102
126
  function standardSchemaAsync(schema, options) {
127
+ validateSchema("standardSchemaAsync", schema);
103
128
  validateOptions("standardSchemaAsync", options);
104
129
  const metavar = options.metavar ?? "VALUE";
105
130
  ensureNonEmptyString(metavar);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/standard-schema",
3
- "version": "1.2.0-dev.0",
3
+ "version": "1.2.0-dev.2298",
4
4
  "description": "Standard Schema value parsers for Optique",
5
5
  "keywords": [
6
6
  "CLI",
@@ -57,7 +57,7 @@
57
57
  "@standard-schema/spec": "^1.1.0"
58
58
  },
59
59
  "dependencies": {
60
- "@optique/core": "1.2.0"
60
+ "@optique/core": "1.2.0-dev.2298+a0394b06"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@standard-schema/spec": "^1.1.0",