@fuzdev/fuz_app 0.154.0 → 0.155.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 (47) hide show
  1. package/dist/actions/CLAUDE.md +2 -2
  2. package/dist/actions/action_event.d.ts.map +1 -1
  3. package/dist/actions/action_event.js +14 -14
  4. package/dist/actions/peer_ping.js +3 -3
  5. package/dist/actions/perform_action.d.ts +12 -10
  6. package/dist/actions/perform_action.d.ts.map +1 -1
  7. package/dist/actions/perform_action.js +19 -16
  8. package/dist/auth/cell_actions.d.ts +6 -4
  9. package/dist/auth/cell_actions.d.ts.map +1 -1
  10. package/dist/auth/cell_actions.js +10 -9
  11. package/dist/http/CLAUDE.md +14 -8
  12. package/dist/http/schema_helpers.d.ts +102 -23
  13. package/dist/http/schema_helpers.d.ts.map +1 -1
  14. package/dist/http/schema_helpers.js +241 -38
  15. package/dist/http/surface.d.ts +7 -2
  16. package/dist/http/surface.d.ts.map +1 -1
  17. package/dist/http/surface.js +37 -14
  18. package/dist/primitive_schemas.d.ts +13 -4
  19. package/dist/primitive_schemas.d.ts.map +1 -1
  20. package/dist/primitive_schemas.js +13 -4
  21. package/dist/schema_issues.d.ts +96 -0
  22. package/dist/schema_issues.d.ts.map +1 -0
  23. package/dist/schema_issues.js +110 -0
  24. package/dist/schema_meta.d.ts +35 -12
  25. package/dist/schema_meta.d.ts.map +1 -1
  26. package/dist/schema_meta.js +60 -12
  27. package/dist/testing/CLAUDE.md +22 -9
  28. package/dist/testing/adversarial_404.d.ts +15 -0
  29. package/dist/testing/adversarial_404.d.ts.map +1 -1
  30. package/dist/testing/adversarial_404.js +26 -19
  31. package/dist/testing/adversarial_input_cases.d.ts.map +1 -1
  32. package/dist/testing/adversarial_input_cases.js +2 -2
  33. package/dist/testing/cross_backend/cell_crud.d.ts.map +1 -1
  34. package/dist/testing/cross_backend/cell_crud.js +7 -5
  35. package/dist/testing/cross_backend/identity_parity.js +2 -2
  36. package/dist/testing/cross_backend/standard.d.ts +3 -2
  37. package/dist/testing/cross_backend/standard.d.ts.map +1 -1
  38. package/dist/testing/daemon_info.d.ts +7 -2
  39. package/dist/testing/daemon_info.d.ts.map +1 -1
  40. package/dist/testing/daemon_info.js +8 -3
  41. package/dist/testing/data_exposure.d.ts +43 -9
  42. package/dist/testing/data_exposure.d.ts.map +1 -1
  43. package/dist/testing/data_exposure.js +151 -35
  44. package/dist/testing/schema_generators.d.ts +21 -9
  45. package/dist/testing/schema_generators.d.ts.map +1 -1
  46. package/dist/testing/schema_generators.js +52 -47
  47. package/package.json +3 -3
@@ -1 +1 @@
1
- {"version":3,"file":"schema_helpers.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/schema_helpers.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAEN,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,MAAM,oBAAoB,CAAC;AAE5B;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,GAAI,QAAQ,CAAC,CAAC,OAAO,KAAG,OAAsC,CAAC;AAE1F;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,GAAI,QAAQ,CAAC,CAAC,OAAO,KAAG,OAAsC,CAAC;AAE1F;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,cAAc,GAAI,YAAY,OAAO,KAAG,OAUpD,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,eAAe,GAAI,OAAO,CAAC,CAAC,OAAO,EAAE,YAAY,OAAO,KAAG,OACA,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,sBAAsB,GAAI,OAAO,OAAO,KAAG,OAavD,CAAC;AAEF;;;;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AAC,EAAE,iBAAiB,CAAC;CAC3B,EACD,oBAAoB,iBAAiB,GAAG,IAAI,KAC1C,iBAAiB,GAAG,IAWtB,CAAC"}
@@ -8,15 +8,22 @@
8
8
  * @module
9
9
  */
10
10
  import { z } from 'zod';
11
+ import { to_error_message } from '@fuzdev/fuz_util/error.ts';
12
+ import { is_schema } from '@fuzdev/fuz_schema/schema.ts';
13
+ import { unwrap_schema } from '@fuzdev/fuz_schema/introspect.ts';
14
+ import { to_json_schema } from '@fuzdev/fuz_schema/json_schema.ts';
11
15
  import { derive_error_schemas } from './error_schemas.js';
16
+ import { parse_any, unknown_key_targets } from '../schema_issues.js';
12
17
  /**
13
- * Check if a schema is exactly `z.null()`.
18
+ * Check if a schema is exactly `z.null()`, or a `@fuzdev/fuz_schema`
19
+ * `literal(null)`.
14
20
  *
15
- * Uses `instanceof` rather than runtime parsing to avoid false positives
16
- * from `z.nullable(z.string())` or similar schemas that accept null
17
- * but also accept other values.
21
+ * Reads the schema's kind rather than parsing `null`, to avoid false
22
+ * positives from `z.nullable(z.string())` or similar schemas that accept
23
+ * null but also accept other values.
18
24
  */
19
- export const is_null_schema = (schema) => schema instanceof z.ZodNull;
25
+ export const is_null_schema = (schema) => schema instanceof z.ZodNull ||
26
+ (is_schema(schema) && schema.kind === 'literal' && schema.def === null);
20
27
  /**
21
28
  * Check if a schema is exactly `z.void()`.
22
29
  *
@@ -63,7 +70,7 @@ export const to_void_params = (raw_params) => {
63
70
  *
64
71
  * @param input - the action's input schema
65
72
  * @param raw_params - the call's `params` as parsed off the wire
66
- * @returns the value to run `input.safeParse` on
73
+ * @returns the value the input parse (`parse_any`) reads
67
74
  */
68
75
  export const to_input_params = (input, raw_params) => is_void_schema(input) ? to_void_params(raw_params) : (raw_params ?? {});
69
76
  /**
@@ -105,46 +112,175 @@ export const json_has_own_proto_key = (value) => {
105
112
  return false;
106
113
  };
107
114
  /**
108
- * `schema.safeParse(value)`, except that keys the schema doesn't declare are
115
+ * `parse_any(schema, value)`, except that keys the schema doesn't declare are
109
116
  * dropped rather than refused — the forward-compatible read of a payload a
110
- * remote authored.
117
+ * remote authored. Takes a Zod or a `@fuzdev/fuz_schema` schema.
111
118
  *
112
119
  * Output schemas are strict so the server's DEV output validation catches a
113
120
  * handler leaking fields it shouldn't. The receiving side, though, is often
114
121
  * older than the remote it talks to — a deploy lands before open tabs reload
115
122
  * — and a remote that adds a field is making a compatible change. So when
116
- * every issue is an unrecognized key, those keys are stripped from a copy of
117
- * `value` and it's parsed again; any other issue (a wrong type, a missing
123
+ * every issue is an unknown key (`unknown_key_targets`), those keys are
124
+ * stripped from a copy of `value` and it's parsed again, until a parse
125
+ * succeeds or reports anything else; any other issue (a wrong type, a missing
118
126
  * field, `null` where a value is required) still fails. `value` itself is
119
127
  * never mutated. This matches serde's default on the Rust spine, which
120
- * ignores unknown fields. Keys nested in a `z.union` branch surface as a
121
- * union issue and aren't stripped; a `z.discriminatedUnion` reports its
122
- * matched branch's issues directly, so those are.
128
+ * ignores unknown fields.
129
+ *
130
+ * A fuz_schema parse reports at most a hundred issues, so a pass that only
131
+ * deleted the reported keys would take a parse per hundred unknown keys,
132
+ * quadratic in a payload of many. So after the first parse that reports
133
+ * unknown keys, one schema-guided pass deletes every key a strict fuz_schema
134
+ * `object` doesn't name anywhere the schema reaches — object entries, array
135
+ * items, record values, the `variant` option the discriminator picks — and
136
+ * the next parse usually succeeds. A key under a fuz_schema `union` is never
137
+ * stripped: which option will match isn't known, and the union fails as one
138
+ * issue of its own. A later `pipe` stage is stripped only as reported, as the
139
+ * pass reads only a pipe's first schema, so keys a later stage refuses that
140
+ * the first names still go a hundred a parse. A pass that finds
141
+ * nothing to strip ends it, and the passes are capped at
142
+ * `UNKNOWN_KEY_PASSES_MAX` for both libraries, past which the last failure is
143
+ * returned, so the work stays linear in the payload whatever its shape.
144
+ *
145
+ * Unions differ by library. A Zod `z.union` of strict objects reports the
146
+ * unknown key itself (`unrecognized_keys` on the object), so it is stripped,
147
+ * as are a `z.discriminatedUnion`'s matched-branch keys. A fuz_schema `union`
148
+ * reports one issue of its own, so a key inside one is not stripped and the
149
+ * parse fails; a `variant` reports its matched option's issues, so those are.
123
150
  *
124
151
  * Used for a response's `result` (`ActionEvent`) and for a peer's reply to a
125
152
  * server-initiated request (`peer/ping`).
126
153
  *
127
- * @param schema - the declared shape
154
+ * @param schema - the declared shape, Zod or fuz_schema
128
155
  * @param value - the remote-authored payload
129
156
  * @returns the parse result, with undeclared keys dropped on success
130
157
  */
131
158
  export const safe_parse_dropping_unknown_keys = (schema, value) => {
132
- const parsed = schema.safeParse(value);
133
- if (parsed.success)
134
- return parsed;
135
- const { issues } = parsed.error;
136
- if (!issues.every((issue) => issue.code === 'unrecognized_keys'))
137
- return parsed;
138
- const stripped = structuredClone(value);
139
- for (const issue of issues) {
140
- const target = get_at_path(stripped, issue.path);
141
- if (typeof target !== 'object' || target === null)
159
+ let parsed = parse_any(schema, value);
160
+ let stripped;
161
+ for (let pass = 0; !parsed.ok && pass < UNKNOWN_KEY_PASSES_MAX; pass++) {
162
+ const targets = unknown_key_targets(parsed.issues);
163
+ if (!targets)
164
+ return parsed;
165
+ let removed = false;
166
+ if (stripped === undefined) {
167
+ stripped = structuredClone(value);
168
+ // once: every key a strict `object` the schema reaches doesn't name
169
+ if (is_schema(schema))
170
+ removed = strip_unknown_keys_by_schema(schema, stripped);
171
+ }
172
+ for (const { path, keys } of targets) {
173
+ const target = get_at_path(stripped, path);
174
+ if (typeof target !== 'object' || target === null)
175
+ continue;
176
+ for (const key of keys) {
177
+ if (Object.hasOwn(target, key)) {
178
+ Reflect.deleteProperty(target, key);
179
+ removed = true;
180
+ }
181
+ }
182
+ }
183
+ if (!removed)
142
184
  return parsed;
143
- for (const key of issue.keys) {
144
- Reflect.deleteProperty(target, key);
185
+ parsed = parse_any(schema, stripped);
186
+ }
187
+ return parsed;
188
+ };
189
+ /**
190
+ * The most parses after the first that `safe_parse_dropping_unknown_keys`
191
+ * runs, each after stripping the keys the one before refused.
192
+ */
193
+ export const UNKNOWN_KEY_PASSES_MAX = 4;
194
+ // deletes from `value`, in place, every own key of an object a strict
195
+ // `object` validates that the object doesn't name, walking the schema with
196
+ // the value: object entries, every item of an `array`, every value of a
197
+ // `record` and the `rest` of an `object_with_rest`, a `variant`'s option by
198
+ // its discriminator as the `variant` picks it, and through `optional`,
199
+ // `nullable`, the defaults, a `pipe`'s first schema, and `lazy`
200
+ // (`unwrap_schema`). A `union` is left alone: which option will match isn't
201
+ // known, and one option's strip could delete a key another names. Any key
202
+ // deleted is one the first parse of that position refuses. Each object is
203
+ // walked once, by the first schema that reaches it, so an aliased or cyclic
204
+ // value ends. Returns whether any key was deleted. An explicit stack, as a
205
+ // value can nest deeper than the call stack
206
+ const strip_unknown_keys_by_schema = (schema, value) => {
207
+ let removed = false;
208
+ const seen = new Set();
209
+ // each schema unwrapped once, so a `lazy`'s function is called once per walk
210
+ // for each schema node that reaches it (beside the one call its own `run`
211
+ // makes), not once per value it reaches; a function returning a different
212
+ // schema per call can strip a key the parse's schema names
213
+ const unwrapped = new Map();
214
+ const unwrap = (node) => {
215
+ let s = unwrapped.get(node);
216
+ if (!s)
217
+ unwrapped.set(node, (s = unwrap_schema(node)));
218
+ return s;
219
+ };
220
+ // each `variant`'s options by discriminator, built as the `variant` builds
221
+ // its own: the last option of a value wins, and a `Map` compares keys by
222
+ // SameValueZero
223
+ const variant_options = new Map();
224
+ const stack = [[schema, value]];
225
+ while (stack.length > 0) {
226
+ const [node, current] = stack.pop();
227
+ if (typeof current !== 'object' || current === null || seen.has(current))
228
+ continue;
229
+ seen.add(current);
230
+ let s = unwrap(node);
231
+ if (Array.isArray(current)) {
232
+ if (s.kind === 'array')
233
+ for (const item of current)
234
+ stack.push([s.def, item]);
235
+ continue;
236
+ }
237
+ const object = current;
238
+ if (s.kind === 'variant') {
239
+ let by_value = variant_options.get(s);
240
+ if (!by_value) {
241
+ by_value = new Map();
242
+ for (const option of s.def.options)
243
+ by_value.set(option.def[s.def.key].def, option);
244
+ variant_options.set(s, by_value);
245
+ }
246
+ // read as the `variant` reads it, through the prototype
247
+ const option = by_value.get(object[s.def.key]);
248
+ if (!option)
249
+ continue;
250
+ // an option is an object schema, its discriminator an entry of it
251
+ s = unwrap(option);
252
+ }
253
+ switch (s.kind) {
254
+ case 'object':
255
+ case 'loose_object':
256
+ case 'strip_object':
257
+ for (const key of Object.keys(object)) {
258
+ if (Object.hasOwn(s.def, key))
259
+ stack.push([s.def[key], object[key]]);
260
+ else if (s.kind === 'object') {
261
+ Reflect.deleteProperty(object, key);
262
+ removed = true;
263
+ }
264
+ }
265
+ break;
266
+ case 'object_with_rest':
267
+ for (const key of Object.keys(object)) {
268
+ // the parse drops a `__proto__` key unread, so its value is no rest's
269
+ if (key === '__proto__' && !Object.hasOwn(s.def.entries, key))
270
+ continue;
271
+ const entry = Object.hasOwn(s.def.entries, key) ? s.def.entries[key] : s.def.rest;
272
+ stack.push([entry, object[key]]);
273
+ }
274
+ break;
275
+ case 'record':
276
+ for (const key of Object.keys(object))
277
+ stack.push([s.def, object[key]]);
278
+ break;
279
+ default:
280
+ // a `union` and every other kind: nothing to strip
145
281
  }
146
282
  }
147
- return schema.safeParse(stripped);
283
+ return removed;
148
284
  };
149
285
  const get_at_path = (value, path) => {
150
286
  let current = value;
@@ -156,27 +292,94 @@ const get_at_path = (value, path) => {
156
292
  return current;
157
293
  };
158
294
  /**
159
- * Check if a schema is a strict object (`z.strictObject()`).
295
+ * Check if a schema is a strict object: `z.strictObject()`, or a
296
+ * `@fuzdev/fuz_schema` `object` (which refuses unknown keys, where
297
+ * `strip_object` drops them and `loose_object` keeps them).
160
298
  *
161
- * Strict objects set `catchall` to `ZodNever` to reject unknown keys.
299
+ * Strict Zod objects set `catchall` to `ZodNever` to reject unknown keys.
162
300
  * Regular `z.object()` has `catchall: undefined` (strips unknown keys in Zod 4).
301
+ * A refined strict object reads strict in both libraries: Zod keeps a
302
+ * `.refine()` on the `ZodObject`, and a fuz_schema `pipe` is read through to
303
+ * its first schema when no stage of it is a `transform` (`message`, `flavor`,
304
+ * `brand` and `meta` keep the kind they wrap). A transformed object is not a
305
+ * strict object in either: Zod's is a `ZodPipe`. The two libraries differ on a
306
+ * pipe without a transform: Zod's `O.pipe(O)` is a `ZodPipe` and reads not
307
+ * strict, where fuz_schema's `pipe(F, F)` reads strict through to `F`.
163
308
  */
164
- export const is_strict_object_schema = (schema) => schema instanceof z.ZodObject && schema.def.catchall instanceof z.ZodNever;
309
+ export const is_strict_object_schema = (schema) => is_schema(schema)
310
+ ? fuz_schema_untransformed_head(schema)?.kind === 'object'
311
+ : schema instanceof z.ZodObject && schema.def.catchall instanceof z.ZodNever;
312
+ // the schema a value reaches first through every `pipe` it starts with, or
313
+ // `null` when any of those pipes has a `transform` stage
314
+ const fuz_schema_untransformed_head = (schema) => {
315
+ let s = schema;
316
+ while (s.kind === 'pipe') {
317
+ const stages = s.def;
318
+ if (stages.some((stage) => stage.kind === 'transform'))
319
+ return null;
320
+ s = stages[0];
321
+ }
322
+ return s;
323
+ };
165
324
  /**
166
- * Convert a Zod schema to a JSON-serializable representation for the surface.
325
+ * Convert a Zod or `@fuzdev/fuz_schema` schema to its JSON Schema for the
326
+ * surface, with `$schema` and `default` stripped at every depth.
327
+ *
328
+ * Returns `null` for a null schema (`is_null_schema`) and a `z.void()`
329
+ * (`is_void_schema`): no body, no params. Any other schema is written with
330
+ * its library's emitter (`to_json_schema` for fuz_schema, `z.toJSONSchema`
331
+ * for Zod) on the side `options.io` names. A part JSON Schema can't say (a
332
+ * transform on the output side, a `z.custom()`, a fuz_schema `check`) is left
333
+ * out or written as `{}` (a `pipe(string, check(…))` writes as
334
+ * `{type: 'string'}`), and `options.on_unrepresentable` hears the library's
335
+ * message, so the surface describes what it can and records the gap.
167
336
  *
168
- * Returns `null` for null schemas, JSON Schema for object schemas.
337
+ * It never answers `null` for a schema it failed to write, as that would
338
+ * read as "takes no input" and drop the schema out of every suite reading the
339
+ * surface: a schema with no JSON Schema form at all (`{}` where the schema
340
+ * isn't `z.unknown()` or `z.any()`), and one either emitter refuses outright
341
+ * (a fuz_schema entry named like a member of `Object.prototype`, metadata
342
+ * that isn't JSON), throw. So an output whose root is a transform (a Zod
343
+ * `.transform()`, a fuz_schema `pipe(object(…), transform(…))`), a
344
+ * `z.custom()`, or a `z.date()` throws, and since `generate_app_surface`
345
+ * runs at server assembly, a route or action declaring one fails the server
346
+ * at boot. That includes `z.unknown().transform(f)`, though `{}` would
347
+ * describe it: the output side of a transform is unknown, whatever its input.
348
+ *
349
+ * @param schema - the schema to describe, Zod or fuz_schema
350
+ * @param options - the side to describe, the schema's name for errors, and a listener for gaps
351
+ * @returns the JSON Schema, or `null` for a null or void schema
352
+ * @throws Error naming `options.source` when the schema can't be written, or nothing of it can
169
353
  */
170
- export const schema_to_surface = (schema) => {
171
- if (is_null_schema(schema))
354
+ export const schema_to_surface = (schema, options) => {
355
+ if (is_null_schema(schema) || is_void_schema(schema))
172
356
  return null;
357
+ const io = options?.io ?? 'output';
358
+ const write = (unrepresentable) => strip_json_schema_noise(is_schema(schema)
359
+ ? to_json_schema(schema, { io, unrepresentable })
360
+ : z.toJSONSchema(schema, { io, unrepresentable }));
361
+ const fail = (message, cause) => new Error(`${options?.source ?? 'schema'}: ${message}`, { cause });
362
+ let gap;
173
363
  try {
174
- const json_schema = z.toJSONSchema(schema);
175
- return strip_json_schema_noise(json_schema);
364
+ return write('throw');
176
365
  }
177
- catch {
178
- return null;
366
+ catch (error) {
367
+ gap = error;
368
+ }
369
+ // part of the schema has no JSON Schema form: write the rest
370
+ let json;
371
+ try {
372
+ json = write('any');
373
+ }
374
+ catch (error) {
375
+ throw fail(`cannot be written as JSON Schema: ${to_error_message(error)}`, error);
376
+ }
377
+ const message = to_error_message(gap);
378
+ if (typeof json === 'object' && json !== null && Object.keys(json).length === 0) {
379
+ throw fail(`has no JSON Schema form: ${message}`, gap);
179
380
  }
381
+ options?.on_unrepresentable?.(message);
382
+ return json;
180
383
  };
181
384
  /**
182
385
  * Recursively strip `$schema` and `default` from a JSON Schema value.
@@ -210,9 +210,14 @@ export declare const collect_middleware_errors: (middleware: Array<MiddlewareSpe
210
210
  */
211
211
  export declare const env_schema_to_surface: (schema: AnySchema) => Array<AppSurfaceEnv>;
212
212
  /**
213
- * Convert SSE event specs to surface entries.
213
+ * Convert SSE event specs to surface entries, each event's `params` written
214
+ * as the output side (`schema_to_surface`) — the server sends them.
215
+ *
216
+ * @param event_specs - the events to describe
217
+ * @param diagnostics - receives an `info` diagnostic per part of a schema with no JSON Schema form
218
+ * @throws Error naming the event when its params schema can't be written
214
219
  */
215
- export declare const events_to_surface: (event_specs: Array<EventSpec>) => Array<AppSurfaceEvent>;
220
+ export declare const events_to_surface: (event_specs: Array<EventSpec>, diagnostics?: Array<AppSurfaceDiagnostic>) => Array<AppSurfaceEvent>;
216
221
  /**
217
222
  * Generate a JSON-serializable attack surface from middleware, route specs,
218
223
  * and optional env/event metadata.
@@ -1 +1 @@
1
- {"version":3,"file":"surface.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/surface.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAS,KAAK,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAGrE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC1E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,gCA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,CAAC,SAAS,CAAC,CAAC;IAC9B,gBAAgB,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC;IACxC,2FAA2F;IAC3F,UAAU,CAAC,EAAE,SAAS,CAAC;IACvB,WAAW,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC;IAC/B,aAAa,CAAC,EAAE,KAAK,CAAC,eAAe,CAAC,CAAC;IACvC;;;;;;;OAOG;IACH,YAAY,CAAC,EAAE,aAAa,CAAC,sBAAsB,CAAC,CAAC;CACrD;AAID;;;;GAIG;AACH,eAAO,MAAM,yBAAyB,GACrC,YAAY,KAAK,CAAC,cAAc,CAAC,EACjC,YAAY,MAAM,KAChB,iBAAiB,GAAG,IAQtB,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,qBAAqB,GAAI,QAAQ,SAAS,KAAG,KAAK,CAAC,aAAa,CAc5E,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,GAC7B,aAAa,KAAK,CAAC,SAAS,CAAC,EAC7B,cAAc,KAAK,CAAC,oBAAoB,CAAC,KACvC,KAAK,CAAC,eAAe,CAQvB,CAAC;AAsBF;;;GAGG;AACH,eAAO,MAAM,oBAAoB,GAAI,SAAS,yBAAyB,KAAG,UA+IzE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,uBAAuB,GAAI,SAAS,yBAAyB,KAAG,cAS5E,CAAC"}
@@ -52,16 +52,37 @@ export const env_schema_to_surface = (schema) => {
52
52
  });
53
53
  };
54
54
  /**
55
- * Convert SSE event specs to surface entries.
55
+ * Convert SSE event specs to surface entries, each event's `params` written
56
+ * as the output side (`schema_to_surface`) — the server sends them.
57
+ *
58
+ * @param event_specs - the events to describe
59
+ * @param diagnostics - receives an `info` diagnostic per part of a schema with no JSON Schema form
60
+ * @throws Error naming the event when its params schema can't be written
56
61
  */
57
- export const events_to_surface = (event_specs) => {
62
+ export const events_to_surface = (event_specs, diagnostics) => {
63
+ const to_surface = create_to_surface(diagnostics);
58
64
  return event_specs.map((spec) => ({
59
65
  method: spec.method,
60
66
  description: spec.description,
61
67
  channel: spec.channel ?? null,
62
- params_schema: schema_to_surface(spec.params)
68
+ params_schema: to_surface(spec.params, 'output', `event ${spec.method} params`)
63
69
  }));
64
70
  };
71
+ // `schema_to_surface` with its source named, recording each part of a schema
72
+ // with no JSON Schema form as an `info` diagnostic: a transform's output or a
73
+ // `z.custom()` is ordinary, so it's recorded on the surface, not warned at startup
74
+ const create_to_surface = (diagnostics) => (schema, io, source) => schema_to_surface(schema, {
75
+ io,
76
+ source,
77
+ on_unrepresentable: diagnostics
78
+ ? (message) => diagnostics.push({
79
+ level: 'info',
80
+ category: 'schema',
81
+ message: `Part of the schema has no JSON Schema form and is left out or written as {} there: ${message}`,
82
+ source
83
+ })
84
+ : undefined
85
+ });
65
86
  /**
66
87
  * Generate a JSON-serializable attack surface from middleware, route specs,
67
88
  * and optional env/event metadata.
@@ -69,6 +90,7 @@ export const events_to_surface = (event_specs) => {
69
90
  export const generate_app_surface = (options) => {
70
91
  const { route_specs, middleware_specs, env_schema, event_specs, rpc_endpoints, ws_endpoints } = options;
71
92
  const diagnostics = [];
93
+ const to_surface = create_to_surface(diagnostics);
72
94
  // Spec-level diagnostics: check for non-strict input schemas
73
95
  for (const r of route_specs) {
74
96
  if (!is_null_schema(r.input) && !is_strict_object_schema(r.input)) {
@@ -87,7 +109,7 @@ export const generate_app_surface = (options) => {
87
109
  if (m.errors) {
88
110
  const schemas = {};
89
111
  for (const [status, schema] of Object.entries(m.errors)) {
90
- const json_schema = schema_to_surface(schema);
112
+ const json_schema = to_surface(schema, 'output', `middleware ${m.name} errors.${status}`);
91
113
  if (json_schema !== null) {
92
114
  schemas[status] = json_schema;
93
115
  }
@@ -109,6 +131,7 @@ export const generate_app_surface = (options) => {
109
131
  const applicable_middleware = middleware_specs
110
132
  .filter((m) => middleware_spec_applies(m, r.path))
111
133
  .map((m) => m.name);
134
+ const route = `${r.method} ${r.path}`;
112
135
  // Merge auto-derived + middleware + explicit error schemas
113
136
  const mw_errors = collect_middleware_errors(middleware_specs, r.path);
114
137
  const merged_errors = merge_error_schemas(r, mw_errors);
@@ -116,7 +139,7 @@ export const generate_app_surface = (options) => {
116
139
  if (merged_errors) {
117
140
  const schemas = {};
118
141
  for (const [status, schema] of Object.entries(merged_errors)) {
119
- const json_schema = schema_to_surface(schema);
142
+ const json_schema = to_surface(schema, 'output', `${route} errors.${status}`);
120
143
  if (json_schema !== null) {
121
144
  schemas[status] = json_schema;
122
145
  }
@@ -135,10 +158,10 @@ export const generate_app_surface = (options) => {
135
158
  transaction: r.transaction ?? r.method !== 'GET',
136
159
  raw_body: r.raw_body ?? false,
137
160
  rate_limit_key: r.rate_limit ?? null,
138
- params_schema: r.params ? schema_to_surface(r.params) : null,
139
- query_schema: r.query ? schema_to_surface(r.query) : null,
140
- input_schema: schema_to_surface(r.input),
141
- output_schema: schema_to_surface(r.output),
161
+ params_schema: r.params ? to_surface(r.params, 'input', `${route} params`) : null,
162
+ query_schema: r.query ? to_surface(r.query, 'input', `${route} query`) : null,
163
+ input_schema: to_surface(r.input, 'input', `${route} input`),
164
+ output_schema: to_surface(r.output, 'output', `${route} output`),
142
165
  error_schemas
143
166
  };
144
167
  }),
@@ -148,8 +171,8 @@ export const generate_app_surface = (options) => {
148
171
  methods: ep.actions.map((a) => ({
149
172
  name: a.spec.method,
150
173
  auth: a.spec.auth,
151
- input_schema: schema_to_surface(a.spec.input),
152
- output_schema: schema_to_surface(a.spec.output),
174
+ input_schema: to_surface(a.spec.input, 'input', `${ep.path} ${a.spec.method} input`),
175
+ output_schema: to_surface(a.spec.output, 'output', `${ep.path} ${a.spec.method} output`),
153
176
  side_effects: a.spec.side_effects,
154
177
  description: a.spec.description,
155
178
  rate_limit_key: a.spec.rate_limit ?? null
@@ -173,8 +196,8 @@ export const generate_app_surface = (options) => {
173
196
  // `request_response` carries a `RouteAuth`; notifications
174
197
  // have `auth: null` (server-pushed, no inbound dispatch).
175
198
  auth: a.spec.auth,
176
- input_schema: schema_to_surface(a.spec.input),
177
- output_schema: schema_to_surface(a.spec.output),
199
+ input_schema: to_surface(a.spec.input, 'input', `${ep.path} ${a.spec.method} input`),
200
+ output_schema: to_surface(a.spec.output, 'output', `${ep.path} ${a.spec.method} output`),
178
201
  description: a.spec.description,
179
202
  side_effects: a.spec.side_effects,
180
203
  rate_limit_key: a.spec.kind === 'request_response' ? (a.spec.rate_limit ?? null) : null
@@ -182,7 +205,7 @@ export const generate_app_surface = (options) => {
182
205
  }))
183
206
  : [],
184
207
  env: env_schema ? env_schema_to_surface(env_schema) : [],
185
- events: event_specs?.length ? events_to_surface(event_specs) : []
208
+ events: event_specs?.length ? events_to_surface(event_specs, diagnostics) : []
186
209
  };
187
210
  };
188
211
  /**
@@ -10,8 +10,8 @@
10
10
  * Split out from `auth/account_schema.ts` so the auth module shrinks to
11
11
  * entity types + client-safe JSON shapes (its real responsibility) and
12
12
  * non-auth consumers can import these primitives without dragging the
13
- * auth domain along. Future cross-domain primitives (phone, url, slug)
14
- * land here too.
13
+ * auth domain along. Future cross-domain primitives (phone, slug) land
14
+ * here too; a URL is `Url` from `@fuzdev/fuz_util/url.ts`.
15
15
  *
16
16
  * Client-safe: nothing here imports `@fuzdev/blake3-wasm`, so wire schemas
17
17
  * that validate a hash don't pull the WASM binary into a bundle.
@@ -121,7 +121,9 @@ export declare const create_uuid: () => Uuid;
121
121
  * Branded UUID schema. Accepts either case and outputs lowercase — the form
122
122
  * Postgres renders a `uuid` in and every id the server mints — so a parsed id
123
123
  * compares equal to the same id read back from the database. The Rust spine
124
- * canonicalizes the same way.
124
+ * canonicalizes the same way. One exception: `z.uuid()` takes the max UUID
125
+ * (`ffffffff-ffff-ffff-ffff-ffffffffffff`) only in lowercase, and
126
+ * `.toLowerCase()` runs after the check, so the uppercase max is refused.
125
127
  *
126
128
  * `.toLowerCase()` rather than `.transform()`: an overwrite keeps the output
127
129
  * type and stays invisible to `z.toJSONSchema`, where a transform would leave
@@ -132,7 +134,14 @@ export type Uuid = z.infer<typeof Uuid>;
132
134
  /** `Uuid` that defaults to a fresh `create_uuid()` when the input is `undefined`. */
133
135
  export declare const UuidWithDefault: z.ZodDefault<z.core.$ZodBranded<z.ZodUUID, "Uuid", "out">>;
134
136
  export type UuidWithDefault = z.infer<typeof UuidWithDefault>;
135
- /** Branded ISO 8601 datetime schema. */
137
+ /**
138
+ * Branded datetime schema, zod's default `z.iso.datetime()`: an RFC 3339 date
139
+ * and time in UTC with a real calendar date, `Z` and no offset, and a fraction
140
+ * of any length or none. Zod 4.6 requires the seconds; earlier zod 4, which
141
+ * the `zod` peer range admits, also takes `HH:MMZ`. fuz_schema's
142
+ * `iso_datetime` is this grammar with the seconds required and the fraction
143
+ * capped at 9 digits.
144
+ */
136
145
  export declare const Datetime: z.core.$ZodBranded<z.ZodISODateTime, "Datetime", "out">;
137
146
  export type Datetime = z.infer<typeof Datetime>;
138
147
  /** Returns an ISO 8601 datetime string for the current time. */
@@ -1 +1 @@
1
- {"version":3,"file":"primitive_schemas.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/primitive_schemas.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAmB,KAAK,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAIlF,gEAAgE;AAChE,MAAM,WAAW,oBAAoB;IACpC,yDAAyD;IACzD,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,+BAA+B;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,mEAAmE;IACnE,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,cAAc,GAAI,SAAS,oBAAoB,KAAG,CAAC,CAAC,SA+ChE,CAAC;AAEF,2EAA2E;AAC3E,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAErC,6GAA6G;AAC7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module shrinks to
11
11
  * entity types + client-safe JSON shapes (its real responsibility) and
12
12
  * non-auth consumers can import these primitives without dragging the
13
- * auth domain along. Future cross-domain primitives (phone, url, slug)
14
- * land here too.
13
+ * auth domain along. Future cross-domain primitives (phone, slug) land
14
+ * here too; a URL is `Url` from `@fuzdev/fuz_util/url.ts`.
15
15
  *
16
16
  * Client-safe: nothing here imports `@fuzdev/blake3-wasm`, so wire schemas
17
17
  * that validate a hash don't pull the WASM binary into a bundle.
@@ -186,7 +186,9 @@ export const create_uuid = () => crypto.randomUUID();
186
186
  * Branded UUID schema. Accepts either case and outputs lowercase — the form
187
187
  * Postgres renders a `uuid` in and every id the server mints — so a parsed id
188
188
  * compares equal to the same id read back from the database. The Rust spine
189
- * canonicalizes the same way.
189
+ * canonicalizes the same way. One exception: `z.uuid()` takes the max UUID
190
+ * (`ffffffff-ffff-ffff-ffff-ffffffffffff`) only in lowercase, and
191
+ * `.toLowerCase()` runs after the check, so the uppercase max is refused.
190
192
  *
191
193
  * `.toLowerCase()` rather than `.transform()`: an overwrite keeps the output
192
194
  * type and stays invisible to `z.toJSONSchema`, where a transform would leave
@@ -195,7 +197,14 @@ export const create_uuid = () => crypto.randomUUID();
195
197
  export const Uuid = z.uuid().toLowerCase().brand('Uuid');
196
198
  /** `Uuid` that defaults to a fresh `create_uuid()` when the input is `undefined`. */
197
199
  export const UuidWithDefault = Uuid.default(create_uuid);
198
- /** Branded ISO 8601 datetime schema. */
200
+ /**
201
+ * Branded datetime schema, zod's default `z.iso.datetime()`: an RFC 3339 date
202
+ * and time in UTC with a real calendar date, `Z` and no offset, and a fraction
203
+ * of any length or none. Zod 4.6 requires the seconds; earlier zod 4, which
204
+ * the `zod` peer range admits, also takes `HH:MMZ`. fuz_schema's
205
+ * `iso_datetime` is this grammar with the seconds required and the fraction
206
+ * capped at 9 digits.
207
+ */
199
208
  export const Datetime = z.iso.datetime().brand('Datetime');
200
209
  /** Returns an ISO 8601 datetime string for the current time. */
201
210
  export const get_datetime_now = () => new Date().toISOString();