@fuzdev/fuz_app 0.169.0 → 0.170.0

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.
Files changed (154) hide show
  1. package/dist/actions/CLAUDE.md +17 -19
  2. package/dist/actions/action_codegen.d.ts +25 -18
  3. package/dist/actions/action_codegen.d.ts.map +1 -1
  4. package/dist/actions/action_codegen.js +83 -67
  5. package/dist/actions/action_event.d.ts +0 -8
  6. package/dist/actions/action_event.d.ts.map +1 -1
  7. package/dist/actions/action_event.js +7 -6
  8. package/dist/actions/action_event_data.d.ts +4 -4
  9. package/dist/actions/action_rpc.d.ts +1 -1
  10. package/dist/actions/action_rpc.js +1 -1
  11. package/dist/actions/action_spec.d.ts +8 -10
  12. package/dist/actions/action_spec.d.ts.map +1 -1
  13. package/dist/actions/action_spec.js +1 -1
  14. package/dist/actions/action_types.d.ts +4 -4
  15. package/dist/actions/action_types.d.ts.map +1 -1
  16. package/dist/actions/action_types.js +2 -2
  17. package/dist/actions/broadcast_api.d.ts +2 -2
  18. package/dist/actions/broadcast_api.js +4 -4
  19. package/dist/actions/compile_action_registry.d.ts +6 -7
  20. package/dist/actions/compile_action_registry.d.ts.map +1 -1
  21. package/dist/actions/compile_action_registry.js +8 -9
  22. package/dist/actions/perform_action.d.ts +6 -6
  23. package/dist/actions/perform_action.d.ts.map +1 -1
  24. package/dist/actions/perform_action.js +11 -10
  25. package/dist/actions/socket.svelte.d.ts +1 -1
  26. package/dist/actions/socket.svelte.js +2 -2
  27. package/dist/auth/CLAUDE.md +11 -11
  28. package/dist/auth/admin_action_specs.d.ts +24 -24
  29. package/dist/auth/audit_emitter.d.ts +6 -7
  30. package/dist/auth/audit_emitter.d.ts.map +1 -1
  31. package/dist/auth/audit_emitter.js +11 -22
  32. package/dist/auth/audit_log_ddl.d.ts +1 -1
  33. package/dist/auth/audit_log_ddl.js +1 -1
  34. package/dist/auth/audit_log_queries.d.ts +0 -13
  35. package/dist/auth/audit_log_queries.d.ts.map +1 -1
  36. package/dist/auth/audit_log_queries.js +3 -3
  37. package/dist/auth/audit_log_schema.d.ts +14 -15
  38. package/dist/auth/audit_log_schema.d.ts.map +1 -1
  39. package/dist/auth/audit_log_schema.js +1 -1
  40. package/dist/auth/auth_ddl.d.ts +1 -1
  41. package/dist/auth/auth_ddl.js +1 -1
  42. package/dist/auth/cell_action_specs.d.ts +54 -54
  43. package/dist/auth/cell_actions.d.ts +2 -2
  44. package/dist/auth/cell_actions.js +5 -5
  45. package/dist/auth/request_context.js +2 -2
  46. package/dist/auth/role_grant_offer_action_specs.d.ts +6 -6
  47. package/dist/auth/role_grant_offer_ddl.d.ts +1 -1
  48. package/dist/auth/role_grant_offer_ddl.js +1 -1
  49. package/dist/auth/role_grant_offer_notifications.d.ts.map +1 -1
  50. package/dist/auth/role_grant_offer_notifications.js +2 -3
  51. package/dist/auth/self_service_role_actions.d.ts +1 -1
  52. package/dist/auth/self_service_role_actions.js +1 -1
  53. package/dist/env/env_schemas.d.ts +1 -2
  54. package/dist/env/env_schemas.d.ts.map +1 -1
  55. package/dist/env/env_schemas.js +3 -4
  56. package/dist/http/CLAUDE.md +29 -31
  57. package/dist/http/auth_shape.d.ts +14 -32
  58. package/dist/http/auth_shape.d.ts.map +1 -1
  59. package/dist/http/auth_shape.js +14 -49
  60. package/dist/http/error_schemas.d.ts +18 -18
  61. package/dist/http/jsonrpc_helpers.js +1 -1
  62. package/dist/http/route_error_check.d.ts +2 -2
  63. package/dist/http/route_error_check.d.ts.map +1 -1
  64. package/dist/http/route_error_check.js +2 -3
  65. package/dist/http/route_errors.d.ts +4 -4
  66. package/dist/http/route_errors.d.ts.map +1 -1
  67. package/dist/http/route_errors.js +0 -13
  68. package/dist/http/route_spec.d.ts +33 -36
  69. package/dist/http/route_spec.d.ts.map +1 -1
  70. package/dist/http/route_spec.js +21 -22
  71. package/dist/http/schema_helpers.d.ts +75 -67
  72. package/dist/http/schema_helpers.d.ts.map +1 -1
  73. package/dist/http/schema_helpers.js +54 -77
  74. package/dist/http/surface.d.ts.map +1 -1
  75. package/dist/http/surface.js +6 -8
  76. package/dist/primitive_schemas.d.ts +9 -17
  77. package/dist/primitive_schemas.d.ts.map +1 -1
  78. package/dist/primitive_schemas.js +9 -17
  79. package/dist/realtime/sse.d.ts +3 -3
  80. package/dist/realtime/sse.d.ts.map +1 -1
  81. package/dist/realtime/sse.js +2 -2
  82. package/dist/schema_issues.d.ts +39 -147
  83. package/dist/schema_issues.d.ts.map +1 -1
  84. package/dist/schema_issues.js +39 -352
  85. package/dist/schema_meta.d.ts +63 -136
  86. package/dist/schema_meta.d.ts.map +1 -1
  87. package/dist/schema_meta.js +37 -184
  88. package/dist/sensitivity.d.ts +3 -5
  89. package/dist/sensitivity.d.ts.map +1 -1
  90. package/dist/sensitivity.js +3 -5
  91. package/dist/testing/CLAUDE.md +20 -22
  92. package/dist/testing/adversarial_headers.js +2 -2
  93. package/dist/testing/adversarial_input_cases.d.ts +19 -18
  94. package/dist/testing/adversarial_input_cases.d.ts.map +1 -1
  95. package/dist/testing/adversarial_input_cases.js +29 -42
  96. package/dist/testing/assertions.d.ts +2 -2
  97. package/dist/testing/assertions.d.ts.map +1 -1
  98. package/dist/testing/assertions.js +3 -3
  99. package/dist/testing/cross_backend/account_lifecycle.js +2 -2
  100. package/dist/testing/cross_backend/action_manifest.d.ts +10 -9
  101. package/dist/testing/cross_backend/action_manifest.d.ts.map +1 -1
  102. package/dist/testing/cross_backend/action_manifest.js +4 -3
  103. package/dist/testing/cross_backend/cell_crud.js +1 -1
  104. package/dist/testing/cross_backend/conformance_rpc_auth.d.ts +0 -62
  105. package/dist/testing/cross_backend/conformance_rpc_auth.d.ts.map +1 -1
  106. package/dist/testing/cross_backend/conformance_table.d.ts.map +1 -1
  107. package/dist/testing/cross_backend/conformance_table.js +3 -3
  108. package/dist/testing/cross_backend/cross_rpc.d.ts +3 -4
  109. package/dist/testing/cross_backend/cross_rpc.d.ts.map +1 -1
  110. package/dist/testing/cross_backend/cross_rpc.js +4 -4
  111. package/dist/testing/cross_backend/differential.js +2 -2
  112. package/dist/testing/cross_backend/identity_parity.js +9 -9
  113. package/dist/testing/cross_backend/origin_conformance.d.ts +9 -9
  114. package/dist/testing/cross_backend/query_shape.js +1 -1
  115. package/dist/testing/cross_backend/rpc_success_fixtures.d.ts.map +1 -1
  116. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts +1 -1
  117. package/dist/testing/cross_backend/setup.d.ts +25 -3
  118. package/dist/testing/cross_backend/setup.d.ts.map +1 -1
  119. package/dist/testing/cross_backend/setup.js +4 -4
  120. package/dist/testing/cross_backend/sse_round_trip.js +2 -2
  121. package/dist/testing/cross_backend/testing_origin_check.d.ts +4 -4
  122. package/dist/testing/cross_backend/token_lifetime.js +3 -3
  123. package/dist/testing/cross_backend/wire_shapes.js +1 -1
  124. package/dist/testing/cross_backend/ws_round_trip.js +1 -1
  125. package/dist/testing/data_exposure.d.ts.map +1 -1
  126. package/dist/testing/data_exposure.js +1 -1
  127. package/dist/testing/integration.js +2 -2
  128. package/dist/testing/integration_helpers.d.ts.map +1 -1
  129. package/dist/testing/integration_helpers.js +2 -2
  130. package/dist/testing/middleware.d.ts.map +1 -1
  131. package/dist/testing/middleware.js +2 -2
  132. package/dist/testing/rpc_attack_surface.js +1 -1
  133. package/dist/testing/rpc_envelope_cases.d.ts +3 -3
  134. package/dist/testing/rpc_envelope_cases.d.ts.map +1 -1
  135. package/dist/testing/rpc_envelope_cases.js +1 -1
  136. package/dist/testing/rpc_helpers.d.ts +12 -14
  137. package/dist/testing/rpc_helpers.d.ts.map +1 -1
  138. package/dist/testing/rpc_helpers.js +9 -10
  139. package/dist/testing/schema_generators.d.ts +67 -60
  140. package/dist/testing/schema_generators.d.ts.map +1 -1
  141. package/dist/testing/schema_generators.js +150 -145
  142. package/dist/testing/sse_round_trip.d.ts.map +1 -1
  143. package/dist/testing/sse_round_trip.js +2 -2
  144. package/dist/testing/surface_invariants.d.ts +1 -1
  145. package/dist/testing/surface_invariants.js +1 -1
  146. package/dist/ui/form_state.svelte.d.ts +1 -1
  147. package/dist/ui/form_state.svelte.js +1 -1
  148. package/package.json +5 -7
  149. package/dist/zod.d.ts +0 -116
  150. package/dist/zod.d.ts.map +0 -1
  151. package/dist/zod.js +0 -218
  152. package/dist/zod_schemas.d.ts +0 -100
  153. package/dist/zod_schemas.d.ts.map +0 -1
  154. package/dist/zod_schemas.js +0 -157
@@ -1,111 +1,47 @@
1
1
  /**
2
- * Validation issues from either schema library the stack's specs use: Zod
3
- * (`z.core.$ZodIssue`) and `@fuzdev/fuz_schema` (`SchemaIssue`). A schema is
4
- * wholly one library, so one parse's issues are all of one kind; these
5
- * helpers read either, so the dispatcher, `ActionEvent` and the
6
- * forward-compatible reads treat a spec the same whichever library it is
7
- * written in, and an error response carries either library's issues in one
8
- * shape, `WireIssue`.
2
+ * Validation issues of a `@fuzdev/fuz_schema` parse (`SchemaIssue`) as the
3
+ * stack reads them: on the input or on a field, unknown keys, text, and the
4
+ * one shape every error response carries them in, `WireIssue`.
9
5
  *
10
6
  * @module
11
7
  */
12
- import { array, format_issue, is_schema, loose_object, number, parse, SchemaError, string, union } from '@fuzdev/fuz_schema/schema.ts';
13
- import { zod_has_trait } from './zod.js';
8
+ import { array, format_issue, is_schema, loose_object, number, SchemaError, string, union } from '@fuzdev/fuz_schema/schema.ts';
14
9
  /**
15
- * Whether `value` is a Zod schema of the classic API, as `z.ZodType`'s
16
- * `instanceof` reads it (`zod_has_trait`): by the schema's traits, so a schema
17
- * made by another copy of Zod is one too. Imports no Zod at runtime.
18
- *
19
- * @param value - any value
20
- */
21
- export const is_zod_schema = (value) => zod_has_trait(value, 'ZodType');
22
- /**
23
- * Throw unless `value` is a Zod schema, naming `label` — what a reader of a
24
- * spec's schema calls once it has ruled out a `@fuzdev/fuz_schema` schema
25
- * (`is_schema`), so a value of neither library fails loudly where it is read,
26
- * rather than as a `TypeError` deeper in or a silent `undefined`.
27
- *
28
- * @param value - the schema, known not to be a fuz_schema schema
29
- * @param label - names the schema in the error, e.g. `` `action 'foo' input` ``
30
- * @throws Error when `value` is not a Zod schema, saying it is of neither library
31
- */
32
- export function assert_zod_schema(value, label) {
33
- if (is_zod_schema(value))
34
- return;
35
- throw new Error(`${label} is neither a Zod schema nor a @fuzdev/fuz_schema schema`);
36
- }
37
- /**
38
- * Throw unless `value` is a schema of either library (`is_schema` or
39
- * `is_zod_schema`), naming `label` — what spec registration runs on each
40
- * schema slot, so a value of neither library fails at registration rather
41
- * than at request time.
10
+ * Throw unless `value` is a `@fuzdev/fuz_schema` schema (`is_schema`), naming
11
+ * `label` — what spec registration runs on each schema slot, so a value that
12
+ * is no schema fails at registration rather than at request time.
42
13
  *
43
14
  * @param value - the value a spec declares as a schema
44
15
  * @param label - names the spec and slot in the error, e.g. `` `RPC action "foo": input` ``
45
- * @throws Error when `value` is neither a Zod nor a fuz_schema schema
16
+ * @throws Error when `value` is not a fuz_schema schema
46
17
  */
47
18
  export function assert_validation_schema(value, label) {
48
19
  if (is_schema(value))
49
20
  return;
50
- assert_zod_schema(value, label);
21
+ throw new Error(`${label} is not a @fuzdev/fuz_schema schema`);
51
22
  }
52
- /**
53
- * Parse `value` with a schema of either library: `parse` for a fuz_schema
54
- * schema (`is_schema`), `safeParse` for a Zod one.
55
- *
56
- * A fuz_schema parse reports at most its first hundred issues, a Zod parse
57
- * every issue.
58
- *
59
- * @param schema - a Zod or fuz_schema schema
60
- * @param value - the value to validate
61
- * @returns the output, or the issues
62
- * @throws Error when `schema` is neither a Zod nor a fuz_schema schema
63
- */
64
- export const parse_any = (schema, value) => {
65
- if (is_schema(schema)) {
66
- const result = parse(schema, value);
67
- return result.ok
68
- ? { ok: true, value: result.value }
69
- : { ok: false, issues: result.issues };
70
- }
71
- assert_zod_schema(schema, 'parse_any: the schema');
72
- const result = schema.safeParse(value);
73
- return result.success
74
- ? { ok: true, value: result.data }
75
- : { ok: false, issues: result.error.issues };
76
- };
77
- /**
78
- * Whether `issue` is Zod's: every Zod issue has a `code`, a fuz_schema issue a `kind`.
79
- */
80
- export const is_zod_issue = (issue) => 'code' in issue;
81
23
  /**
82
24
  * Whether `issue` sits on the input itself rather than on one of its fields:
83
25
  * a non-object input, or an unknown key on the input object — what the Rust
84
26
  * twin refuses before reading any field.
85
27
  *
86
- * Zod reports both at an empty path, its `unrecognized_keys` naming the keys.
87
- * fuz_schema reports a non-object at an empty path and each unknown key as an
28
+ * A non-object is reported at an empty path, and each unknown key as an
88
29
  * `unknown_key` issue whose path ends at the key, so one on the input has a
89
- * path of the key alone. An unknown key on a nested object is on that field,
90
- * as Zod's is.
30
+ * path of the key alone. An unknown key on a nested object is on that field.
91
31
  *
92
32
  * On the wire the `unknown_key` rule changes no answer today: a root
93
33
  * `unknown_key` comes from a root `object`, which `resolve_issues_invalid_reason`
94
34
  * already answers with none from its check of the input's keys, or from a
95
35
  * schema such as a `variant` whose fields resolve no reason. It is kept as the
96
- * direct statement of Zod's rule.
36
+ * direct statement of the rule.
97
37
  *
98
38
  * @param issue - one issue of a failed parse
99
39
  */
100
- export const issue_is_on_input = (issue) => issue.path.length === 0 ||
101
- (!is_zod_issue(issue) && issue.kind === 'unknown_key' && issue.path.length === 1);
40
+ export const issue_is_on_input = (issue) => issue.path.length === 0 || (issue.kind === 'unknown_key' && issue.path.length === 1);
102
41
  /**
103
42
  * The unknown keys among `issues`, each with the path of its object, or
104
- * `null` when any issue is something else.
105
- *
106
- * A Zod `unrecognized_keys` issue is at its object's path and names every
107
- * key; a fuz_schema `unknown_key` issue is one key, the last element of its
108
- * path.
43
+ * `null` when any issue is something else. An `unknown_key` issue is one
44
+ * key, the last element of its path.
109
45
  *
110
46
  * @param issues - a failed parse's issues
111
47
  * @returns a target per issue, or `null` when not every issue is an unknown key
@@ -113,76 +49,29 @@ export const issue_is_on_input = (issue) => issue.path.length === 0 ||
113
49
  export const unknown_key_targets = (issues) => {
114
50
  const targets = [];
115
51
  for (const issue of issues) {
116
- if (is_zod_issue(issue)) {
117
- if (issue.code !== 'unrecognized_keys')
118
- return null;
119
- targets.push({ path: issue.path, keys: issue.keys });
120
- }
121
- else {
122
- if (issue.kind !== 'unknown_key' || issue.path.length === 0)
123
- return null;
124
- targets.push({ path: issue.path.slice(0, -1), keys: [issue.path.at(-1)] });
125
- }
52
+ if (issue.kind !== 'unknown_key' || issue.path.length === 0)
53
+ return null;
54
+ targets.push({ path: issue.path.slice(0, -1), keys: [issue.path.at(-1)] });
126
55
  }
127
56
  return targets;
128
57
  };
129
- // a Zod issue path as `z.prettifyError` writes it: identifier keys dotted,
130
- // numbers, symbols and other keys bracketed
131
- const format_zod_path = (path) => {
132
- const segments = [];
133
- for (const seg of path) {
134
- const key = typeof seg === 'object' && seg !== null ? seg.key : seg;
135
- if (typeof key === 'number')
136
- segments.push(`[${key}]`);
137
- else if (typeof key === 'symbol')
138
- segments.push(`[${JSON.stringify(String(key))}]`);
139
- else if (/[^\w$]/.test(key))
140
- segments.push(`[${JSON.stringify(key)}]`);
141
- else {
142
- if (segments.length)
143
- segments.push('.');
144
- segments.push(key);
145
- }
146
- }
147
- return segments.join('');
148
- };
149
58
  /**
150
- * A failed parse's issues as text. Zod issues read as `z.prettifyError`
151
- * writes them — shortest path first, each `✖ message` and, when it has a
152
- * path, ` → at path` — without importing Zod at runtime; fuz_schema issues
153
- * are their `format_issue` lines.
59
+ * A failed parse's issues as text, a `format_issue` line each.
154
60
  *
155
- * @param issues - a failed parse's issues, all of one library
61
+ * @param issues - a failed parse's issues
156
62
  */
157
- export const format_issues = (issues) => {
158
- const first = issues[0];
159
- if (!first)
160
- return '';
161
- if (is_zod_issue(first)) {
162
- const lines = [];
163
- const sorted = [...issues].sort((a, b) => (a.path ?? []).length - (b.path ?? []).length);
164
- for (const issue of sorted) {
165
- lines.push(`✖ ${issue.message}`);
166
- if (issue.path?.length)
167
- lines.push(` → at ${format_zod_path(issue.path)}`);
168
- }
169
- return lines.join('\n');
170
- }
171
- return issues.map(format_issue).join('\n');
172
- };
63
+ export const format_issues = (issues) => issues.map(format_issue).join('\n');
173
64
  /**
174
- * Whether `err` is a thrown validation failure of either library: a
175
- * `z.ZodError` or a fuz_schema `SchemaError` (`parse_or_throw`). Both carry
176
- * the failed parse's `issues`.
65
+ * Whether `err` is a thrown validation failure, a `SchemaError`
66
+ * (`parse_or_throw`), which carries the failed parse's `issues`.
177
67
  *
178
68
  * @param err - a caught value
179
69
  */
180
- export const is_schema_validation_error = (err) => zod_has_trait(err, 'ZodError') || err instanceof SchemaError;
70
+ export const is_schema_validation_error = (err) => err instanceof SchemaError;
181
71
  /**
182
72
  * A `@fuzdev/fuz_schema` `loose_object`: a validation issue as an error
183
- * response carries it, for either schema library — fuz_schema's `SchemaIssue`
184
- * as JSON, with `message` the text of the issue. `to_wire_issues` builds it
185
- * from either library's issues.
73
+ * response carries it — `SchemaIssue` as JSON, with `message` the text of the
74
+ * issue. `to_wire_issues` builds it.
186
75
  *
187
76
  * Loose: a client reads it, and a newer server may add fields.
188
77
  */
@@ -195,223 +84,21 @@ export const WireIssue = loose_object({
195
84
  expected: string,
196
85
  /** The type of the value that failed: `'null'`, `'array'`, `'NaN'`, `'Infinity'`, `'-Infinity'`, or its `typeof`. */
197
86
  received: string,
198
- /** The issue as text: `format_issue` for a fuz_schema issue, Zod's own message for a Zod one. */
87
+ /** The issue as text, `format_issue`'s. */
199
88
  message: string
200
89
  });
201
90
  /**
202
- * A failed parse's issues as `WireIssue`s, whichever library made them.
203
- *
204
- * A fuz_schema issue keeps its `kind`, `path`, `expected` and `received`, and
205
- * its `message` is `format_issue`'s text, the path included.
206
- *
207
- * A Zod issue maps to the kind fuz_schema reports for the same check, its
208
- * `message` Zod's own text, which names no path:
91
+ * A failed parse's issues as `WireIssue`s: each keeps its `kind`, `path`,
92
+ * `expected` and `received`, and its `message` is `format_issue`'s text, the
93
+ * path included. A symbol key in a path goes as its text.
209
94
  *
210
- * - `invalid_type` — the type's kind (`string`, `number`, `boolean`,
211
- * `bigint`, `object`, `record`, `array`); `int` is `integer`, a `tuple` an
212
- * `array`, `null`, `undefined`, `void` and `nan` a `literal` of the value,
213
- * `nonoptional` a `missing_key`, and a type fuz_schema has no kind for
214
- * (`date`, `map`, `set`, `file`, `symbol`, `function`, `never`) a `check`
215
- * expecting the type's name. A missing key is reported as fuz_schema
216
- * reports it, as the type expected with `received: 'undefined'`.
217
- * - `too_small` / `too_big` — `min_length` / `max_length` for a string,
218
- * array, set or file (`length >= N`), `min_value` / `max_value` otherwise
219
- * (`>= N`); an exclusive bound writes `>` or `<`, which fuz_schema has no
220
- * check for, and an exact length is reported as the bound it broke.
221
- * - `invalid_format` — `uuid` for a `uuid` or `guid`, `iso_datetime` for a
222
- * `datetime`, `regex` for a `regex` (expecting its pattern), and a `check`
223
- * expecting the format's name for every other format.
224
- * - `unrecognized_keys` — an `unknown_key` per key, the key appended to the
225
- * path, as fuz_schema reports them.
226
- * - `invalid_union` — a `variant` expecting the options when Zod names a
227
- * discriminator, else a `union` expecting each option as fuz_schema names
228
- * it: by the type or the values it expected of the value, or by the
229
- * value's type when the option took the type and failed a refinement or a
230
- * field below. A union refused because more than one option matched is a
231
- * `union` expecting `exactly one option`.
232
- * - `invalid_value` — a `literal` of one value, a `one_of` of more.
233
- * - `custom` — a `check`; `not_multiple_of` a `check` expecting
234
- * `multiple of N`; `invalid_key` and `invalid_element` a `check` expecting
235
- * `a valid key` / `a valid element`.
236
- *
237
- * A Zod issue holds no value, so `received` is read from `input` at the
238
- * issue's path: `'undefined'` where a key is missing or the path leads
239
- * through something that isn't an object. Where an earlier step of a Zod
240
- * pipe replaced the value, it names the input's value, not the replacement.
241
- *
242
- * @param issues - a failed parse's issues, either library
243
- * @param input - the value that was parsed
244
- * @returns the issues, one per issue but for a Zod `unrecognized_keys`, which is one per key
95
+ * @param issues - a failed parse's issues
96
+ * @returns one per issue
245
97
  */
246
- export const to_wire_issues = (issues, input) => {
247
- const wire = [];
248
- for (const issue of issues) {
249
- if (is_zod_issue(issue)) {
250
- push_zod_wire_issues(wire, issue, input);
251
- }
252
- else {
253
- wire.push({
254
- kind: issue.kind,
255
- path: to_wire_path(issue.path),
256
- expected: issue.expected,
257
- received: issue.received,
258
- message: format_issue(issue)
259
- });
260
- }
261
- }
262
- return wire;
263
- };
264
- // a symbol key has no JSON form, so it goes as its text
265
- const to_wire_path = (path) => path.map((key) => (typeof key === 'symbol' ? key.toString() : key));
266
- // the type of a value as fuz_schema's `received` names it
267
- const to_received = (value) => value === null
268
- ? 'null'
269
- : Array.isArray(value)
270
- ? 'array'
271
- : typeof value === 'number' && !Number.isFinite(value)
272
- ? String(value)
273
- : typeof value;
274
- // the value at `path` of `value`, by own keys, `undefined` where a key is
275
- // missing or the path leads through something that isn't an object
276
- const get_at = (value, path) => {
277
- let at = value;
278
- for (const key of path) {
279
- if (at === null || typeof at !== 'object')
280
- return undefined;
281
- at = Object.hasOwn(at, key) ? at[key] : undefined;
282
- }
283
- return at;
284
- };
285
- // literals as fuz_schema's `expected` writes them, strings quoted
286
- const format_literals = (values) => values.map((v) => (typeof v === 'string' ? JSON.stringify(v) : String(v))).join(' | ');
287
- // fuz_schema's kind and `expected` for a Zod `invalid_type` issue's `expected`,
288
- // where they differ from the type's own name
289
- const ZOD_TYPE_KINDS = {
290
- string: { kind: 'string', expected: 'string' },
291
- number: { kind: 'number', expected: 'number' },
292
- boolean: { kind: 'boolean', expected: 'boolean' },
293
- bigint: { kind: 'bigint', expected: 'bigint' },
294
- int: { kind: 'integer', expected: 'integer' },
295
- object: { kind: 'object', expected: 'object' },
296
- record: { kind: 'record', expected: 'object' },
297
- array: { kind: 'array', expected: 'array' },
298
- tuple: { kind: 'array', expected: 'array' },
299
- null: { kind: 'literal', expected: 'null' },
300
- undefined: { kind: 'literal', expected: 'undefined' },
301
- void: { kind: 'literal', expected: 'undefined' },
302
- nan: { kind: 'literal', expected: 'NaN' },
303
- nonoptional: { kind: 'missing_key', expected: 'a required key' }
304
- };
305
- // a Zod length origin, whose bounds fuz_schema checks as `min_length` / `max_length`
306
- const ZOD_LENGTH_ORIGINS = new Set(['string', 'array', 'set', 'file']);
307
- // fuz_schema's kind for a Zod `invalid_format` format, a `check` when absent
308
- const ZOD_FORMAT_KINDS = {
309
- uuid: 'uuid',
310
- guid: 'uuid',
311
- datetime: 'iso_datetime',
312
- regex: 'regex'
313
- };
314
- // pushes the wire form of a Zod issue
315
- const push_zod_wire_issues = (wire, issue, input) => {
316
- const { path } = issue;
317
- if (issue.code === 'unrecognized_keys') {
318
- // the object holding the keys, read once so the cost is linear in keys
319
- const holder = get_at(input, path);
320
- for (const key of issue.keys) {
321
- wire.push({
322
- kind: 'unknown_key',
323
- path: to_wire_path([...path, key]),
324
- expected: 'no unknown keys',
325
- received: to_received(get_at(holder, [key])),
326
- // Zod's text names every key, so a key of several gets the one-key form
327
- message: issue.keys.length === 1 ? issue.message : `Unrecognized key: ${JSON.stringify(key)}`
328
- });
329
- }
330
- return;
331
- }
332
- const { kind, expected } = to_zod_kind(issue, input, path);
333
- wire.push({
334
- kind,
335
- path: to_wire_path(path),
336
- expected,
337
- received: to_received(get_at(input, path)),
338
- message: issue.message
339
- });
340
- };
341
- // fuz_schema's kind and `expected` for a Zod issue other than `unrecognized_keys`
342
- const to_zod_kind = (issue, input, path) => {
343
- switch (issue.code) {
344
- case 'invalid_type':
345
- return ZOD_TYPE_KINDS[issue.expected] ?? { kind: 'check', expected: issue.expected };
346
- case 'too_small': {
347
- const op = issue.inclusive === false ? '>' : '>=';
348
- return ZOD_LENGTH_ORIGINS.has(issue.origin)
349
- ? { kind: 'min_length', expected: `length ${op} ${issue.minimum}` }
350
- : { kind: 'min_value', expected: `${op} ${issue.minimum}` };
351
- }
352
- case 'too_big': {
353
- const op = issue.inclusive === false ? '<' : '<=';
354
- return ZOD_LENGTH_ORIGINS.has(issue.origin)
355
- ? { kind: 'max_length', expected: `length ${op} ${issue.maximum}` }
356
- : { kind: 'max_value', expected: `${op} ${issue.maximum}` };
357
- }
358
- case 'invalid_format': {
359
- const kind = ZOD_FORMAT_KINDS[issue.format];
360
- if (kind === undefined)
361
- return { kind: 'check', expected: issue.format };
362
- return {
363
- kind,
364
- expected: kind === 'regex' ? (issue.pattern ?? 'regex') : kind
365
- };
366
- }
367
- case 'not_multiple_of':
368
- return { kind: 'check', expected: `multiple of ${issue.divisor}` };
369
- case 'invalid_union':
370
- if (issue.inclusive === false)
371
- return { kind: 'union', expected: 'exactly one option' };
372
- if (issue.discriminator !== undefined && issue.options) {
373
- return { kind: 'variant', expected: format_literals(issue.options) };
374
- }
375
- return {
376
- kind: 'union',
377
- expected: [...new Set(describe_union_options(issue, input, path))].join(' | ')
378
- };
379
- case 'invalid_key':
380
- return { kind: 'check', expected: 'a valid key' };
381
- case 'invalid_element':
382
- return { kind: 'check', expected: 'a valid element' };
383
- case 'invalid_value':
384
- return {
385
- kind: issue.values.length === 1 ? 'literal' : 'one_of',
386
- expected: format_literals(issue.values)
387
- };
388
- case 'custom':
389
- return { kind: 'check', expected: 'check' };
390
- default: {
391
- // a code a newer Zod added fails to compile here; at runtime it is a `check`
392
- const unmapped = issue;
393
- return { kind: 'check', expected: unmapped.code };
394
- }
395
- }
396
- };
397
- // each option of a failed Zod union as fuz_schema's `expected` names it: by
398
- // the type or the values its issue on the value itself expected, a union's
399
- // own options in its place (a discriminated one is an object), and else by
400
- // the value's type, which the option matched: a refinement failed, or an
401
- // issue below the value, as an object or array option's (an option's issues
402
- // are relative to the union)
403
- const describe_union_options = (issue, input, path) => issue.errors.flatMap((option_issues) => {
404
- if (!option_issues.length)
405
- return [];
406
- const on_value = option_issues.find((i) => i.path.length === 0);
407
- if (on_value?.code === 'invalid_union' && on_value.inclusive !== false) {
408
- return on_value.discriminator === undefined
409
- ? describe_union_options(on_value, input, path)
410
- : ['object'];
411
- }
412
- if ((on_value?.code === 'invalid_type' && on_value.expected !== 'int') ||
413
- on_value?.code === 'invalid_value') {
414
- return [to_zod_kind(on_value, input, path).expected];
415
- }
416
- return [to_received(get_at(input, path))];
417
- });
98
+ export const to_wire_issues = (issues) => issues.map((issue) => ({
99
+ kind: issue.kind,
100
+ path: issue.path.map((key) => (typeof key === 'symbol' ? key.toString() : key)),
101
+ expected: issue.expected,
102
+ received: issue.received,
103
+ message: format_issue(issue)
104
+ }));