@fuzdev/fuz_app 0.156.0 → 0.157.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.
- package/dist/http/CLAUDE.md +17 -9
- package/dist/http/auth_shape.d.ts +5 -8
- package/dist/http/auth_shape.d.ts.map +1 -1
- package/dist/http/auth_shape.js +5 -8
- package/dist/http/error_schemas.d.ts +9 -4
- package/dist/http/error_schemas.d.ts.map +1 -1
- package/dist/http/route_spec.d.ts +35 -18
- package/dist/http/route_spec.d.ts.map +1 -1
- package/dist/http/route_spec.js +39 -21
- package/dist/http/schema_helpers.d.ts +3 -4
- package/dist/http/schema_helpers.d.ts.map +1 -1
- package/dist/http/surface.d.ts.map +1 -1
- package/dist/http/surface.js +7 -3
- package/dist/primitive_schemas.d.ts +9 -5
- package/dist/primitive_schemas.d.ts.map +1 -1
- package/dist/primitive_schemas.js +20 -6
- package/dist/primitive_schemas_fuz.d.ts +3 -5
- package/dist/primitive_schemas_fuz.d.ts.map +1 -1
- package/dist/primitive_schemas_fuz.js +3 -5
- package/dist/testing/CLAUDE.md +6 -6
- package/dist/testing/admin_integration.d.ts.map +1 -1
- package/dist/testing/admin_integration.js +3 -2
- package/dist/testing/adversarial_404.d.ts +2 -2
- package/dist/testing/adversarial_404.d.ts.map +1 -1
- package/dist/testing/adversarial_404.js +8 -5
- package/dist/testing/adversarial_input.js +1 -1
- package/dist/testing/assertions.d.ts +6 -5
- package/dist/testing/assertions.d.ts.map +1 -1
- package/dist/testing/assertions.js +9 -4
- package/dist/testing/error_coverage.d.ts +9 -17
- package/dist/testing/error_coverage.d.ts.map +1 -1
- package/dist/testing/error_coverage.js +20 -3
- package/dist/testing/integration_helpers.js +3 -3
- package/package.json +1 -1
package/dist/http/CLAUDE.md
CHANGED
|
@@ -48,19 +48,26 @@ consumers.
|
|
|
48
48
|
- `auth: RouteAuth` — flat record `{account, actor, roles?, credential_types?, required_scope?}` from `http/auth_shape.ts`. `account` / `actor` are `'none' | 'optional' | 'required'`. `required_scope` names the capability a narrowed api token's scope must admit — `surface:<name>`, a non-RPC surface refused to every narrowed token (an `rpc:<method>` capability throws at registration: it is the dispatcher's per-method gate, which a route never reaches) — and is route-spec-only: `compile_action_registry` rejects it on an `ActionSpec`, where the dispatcher derives `rpc:<method>` from `spec.method`. Otherwise the same shape governs `ActionSpec.auth` (see `actions/CLAUDE.md`).
|
|
49
49
|
- `handler: RouteHandler` — `(c: Context, route: RouteContext) => Response | Promise<Response>`
|
|
50
50
|
- `description` — free-text, surfaced in `AppSurface`
|
|
51
|
-
- `params?: z.ZodObject` — strict-object schema for URL path params
|
|
52
|
-
- `query?: z.ZodObject` — strict-object schema for URL query string
|
|
53
|
-
- `input:
|
|
54
|
-
- `output:
|
|
51
|
+
- `params?: z.ZodObject | AnySchema` — strict-object schema for URL path params
|
|
52
|
+
- `query?: z.ZodObject | AnySchema` — strict-object schema for URL query string
|
|
53
|
+
- `input: ValidationSchema` — request body schema; `z.null()` (or fuz_schema `literal(null)`) for no-body (GET/DELETE)
|
|
54
|
+
- `output: ValidationSchema` — success response schema
|
|
55
55
|
- `mints_credential?: boolean` — the route issues a session cookie while declaring no auth (login, signup, bootstrap); assembly refuses it outside `AUTH_MIDDLEWARE_PATH`, as it does any route whose `auth.account` is not `'none'` (`assert_credential_routes_in_auth_scope` in `auth/middleware.ts`)
|
|
56
56
|
- `reads_credential?: boolean` — the handler reads the resolved account while the route declares no auth gate, answering an anonymous caller itself (account status, the cell-scoped fact read, `create_rpc_endpoint`'s pair, whose dispatcher gates per action); held inside `AUTH_MIDDLEWARE_PATH` by the same assertion. Such a handler reads the account through `get_public_account_id` (`auth/request_context.ts`), which answers a scope-narrowed token as anonymous — the RPC pair excepted, its dispatcher gating each method
|
|
57
57
|
- `rate_limit?: RateLimitKey` — metadata only (`'ip' | 'account' | 'both'`); auto-derives 429
|
|
58
58
|
- `errors?: RouteErrorSchemas` — handler-specific error schemas keyed by HTTP status
|
|
59
59
|
- `transaction?: boolean` — declarative transaction wrapping (see below)
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
Every schema field takes a Zod or a `@fuzdev/fuz_schema` schema
|
|
62
|
+
(`ValidationSchema`, `schema_issues.ts`), validated through `parse_any`, its
|
|
63
|
+
refusals answered with `to_wire_issues`'s `WireIssue`s either way. A fuz_schema
|
|
64
|
+
`params` or `query` must be an object schema under any wrappers
|
|
65
|
+
(`get_schema_entries`); `apply_route_specs` throws naming the route on one that
|
|
66
|
+
isn't. Input/output naming mirrors SAES `ActionSpec`. Use a strict object for
|
|
67
|
+
inputs — `z.strictObject()` or fuz_schema's `object` — the surface diagnostic
|
|
68
|
+
warns on non-strict objects because unknown keys are silently stripped by Zod
|
|
69
|
+
4's default `z.object()` (and by fuz_schema's `strip_object`, kept by its
|
|
70
|
+
`loose_object`); the warning names the library's strict form.
|
|
64
71
|
|
|
65
72
|
### `RouteContext` — per-request deps
|
|
66
73
|
|
|
@@ -124,7 +131,8 @@ Duplicate `method path` pairs throw at registration.
|
|
|
124
131
|
|
|
125
132
|
Validated values are accessed via `get_route_input(c, schema)`,
|
|
126
133
|
`get_route_params(c, schema)`, `get_route_query(c, schema)` — pass the
|
|
127
|
-
matching Zod
|
|
134
|
+
matching schema, Zod or fuz_schema, and the return type infers as
|
|
135
|
+
`ValidationOutput<typeof schema>`.
|
|
128
136
|
Each helper has a `<T>(c)` overload (no schema arg) for callers without the
|
|
129
137
|
schema in scope.
|
|
130
138
|
|
|
@@ -157,7 +165,7 @@ WS); see ../../../docs/architecture.md §DEV-only Output Validation.
|
|
|
157
165
|
|
|
158
166
|
- `ERROR_*` `snake_case` string constants — single source of truth; use `.literal(ERROR_*)` in Zod schemas and inline checks in handlers
|
|
159
167
|
- `ApiError`, `ValidationError`, `PermissionError`, `CredentialTypeRequiredError`, `RateLimitError`, `PayloadTooLargeError`, `UnsupportedContentTypeError`, `ForeignKeyError` — standard shapes
|
|
160
|
-
- `RouteErrorSchemas = Partial<Record<number,
|
|
168
|
+
- `RouteErrorSchemas = Partial<Record<number, ValidationSchema>>` — Zod or fuz_schema, on `RouteSpec.errors` and `MiddlewareSpec.errors`; `derive_error_schemas` returns the Zod-only `DerivedErrorSchemas`
|
|
161
169
|
- `RateLimitKey = 'ip' | 'account' | 'both'`
|
|
162
170
|
|
|
163
171
|
All standard shapes use `z.looseObject` — intentional because multiple
|
|
@@ -80,14 +80,11 @@ export type ActingActor = z.infer<typeof ActingActor>;
|
|
|
80
80
|
* an input schema behind it, so there a malformed selector is final (the
|
|
81
81
|
* status route answers account-only).
|
|
82
82
|
*
|
|
83
|
-
* The grammar is the fuz_schema twin's,
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* that schema runs ahead of the authorization phase and refuses the uppercase
|
|
89
|
-
* max with a 400, a declared gap against the Rust spine until those query
|
|
90
|
-
* schemas move to the twin.
|
|
83
|
+
* The grammar is the fuz_schema twin's, which is the Zod `ActingActor`'s and
|
|
84
|
+
* the Rust spine's, so every value reads as the Zod `ActingActor` would, the
|
|
85
|
+
* max UUID in any case included. The id comes back lowercased. A route
|
|
86
|
+
* declaring `acting` on its Zod `query` schema doesn't read it here: that
|
|
87
|
+
* schema runs ahead of the authorization phase, with the same grammar.
|
|
91
88
|
*
|
|
92
89
|
* @param value - the raw `acting` field, from params / query / body
|
|
93
90
|
* @returns the actor id, or `undefined` when absent or malformed
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth_shape.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/auth_shape.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAUxB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MA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BAAwB,GAAI,MAAM,SAAS,KAAG,OAC3B,CAAC;AAEjC;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM,SAAS,KAAG,OACQ,CAAC;AAE1D;;;;;;;;;;GAUG;AACH,eAAO,MAAM,2BAA2B,GAAI,MAAM,SAAS,KAAG,OACwB,CAAC;AAEvF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,4BAA4B,GAAI,MAAM,SAAS,EAAE,SAAS,MAAM,KAAG,IAM/E,CAAC;AAYF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,4BAA4B,GAAI,QAAQ,gBAAgB,KAAG,OAKvE,CAAC;AAEF;;;;;GAKG;AACH,MAAM,WAAW,WAAW;IAC3B,KAAK,EAAE,gBAAgB,CAAC;IACxB,KAAK,CAAC,EAAE,gBAAgB,CAAC;CACzB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,sCAAsC,GAClD,MAAM,SAAS,EACf,OAAO,WAAW,EAClB,SAAS,MAAM,KACb,IAmBF,CAAC"}
|
package/dist/http/auth_shape.js
CHANGED
|
@@ -83,14 +83,11 @@ export const ActingActor = Uuid.optional().meta({ description: ACTING_ACTOR_DESC
|
|
|
83
83
|
* an input schema behind it, so there a malformed selector is final (the
|
|
84
84
|
* status route answers account-only).
|
|
85
85
|
*
|
|
86
|
-
* The grammar is the fuz_schema twin's,
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* that schema runs ahead of the authorization phase and refuses the uppercase
|
|
92
|
-
* max with a 400, a declared gap against the Rust spine until those query
|
|
93
|
-
* schemas move to the twin.
|
|
86
|
+
* The grammar is the fuz_schema twin's, which is the Zod `ActingActor`'s and
|
|
87
|
+
* the Rust spine's, so every value reads as the Zod `ActingActor` would, the
|
|
88
|
+
* max UUID in any case included. The id comes back lowercased. A route
|
|
89
|
+
* declaring `acting` on its Zod `query` schema doesn't read it here: that
|
|
90
|
+
* schema runs ahead of the authorization phase, with the same grammar.
|
|
94
91
|
*
|
|
95
92
|
* @param value - the raw `acting` field, from params / query / body
|
|
96
93
|
* @returns the actor id, or `undefined` when absent or malformed
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
*/
|
|
14
14
|
import { z } from 'zod';
|
|
15
15
|
import { type RouteAuth } from './auth_shape.js';
|
|
16
|
+
import { type ValidationSchema } from '../schema_issues.js';
|
|
16
17
|
/** Request body failed Zod validation. */
|
|
17
18
|
export declare const ERROR_INVALID_REQUEST_BODY: "invalid_request_body";
|
|
18
19
|
/** Request body is not valid JSON or not an object. */
|
|
@@ -308,11 +309,15 @@ export declare const AccountVanishedError: z.ZodObject<{
|
|
|
308
309
|
}, z.core.$loose>;
|
|
309
310
|
export type AccountVanishedError = z.infer<typeof AccountVanishedError>;
|
|
310
311
|
/**
|
|
311
|
-
* Error schema map — maps HTTP status codes to Zod
|
|
312
|
+
* Error schema map — maps HTTP status codes to Zod or `@fuzdev/fuz_schema`
|
|
313
|
+
* schemas.
|
|
312
314
|
*
|
|
313
|
-
* Used on `RouteSpec.errors` and
|
|
315
|
+
* Used on `RouteSpec.errors` and `MiddlewareSpec.errors`; `derive_error_schemas`
|
|
316
|
+
* returns its Zod-only form, `DerivedErrorSchemas`.
|
|
314
317
|
*/
|
|
315
|
-
export type RouteErrorSchemas = Partial<Record<number,
|
|
318
|
+
export type RouteErrorSchemas = Partial<Record<number, ValidationSchema>>;
|
|
319
|
+
/** The error schemas `derive_error_schemas` builds, all of them Zod. */
|
|
320
|
+
export type DerivedErrorSchemas = Partial<Record<number, z.ZodType>>;
|
|
316
321
|
/**
|
|
317
322
|
* Rate limit key type — declares what a route or RPC action's rate limiter
|
|
318
323
|
* is keyed on.
|
|
@@ -366,5 +371,5 @@ export interface DeriveErrorSchemasOptions {
|
|
|
366
371
|
rate_limit?: RateLimitKey;
|
|
367
372
|
checks_content_type?: boolean;
|
|
368
373
|
}
|
|
369
|
-
export declare const derive_error_schemas: ({ auth, has_input, has_params, has_query, rate_limit, checks_content_type }: DeriveErrorSchemasOptions) =>
|
|
374
|
+
export declare const derive_error_schemas: ({ auth, has_input, has_params, has_query, rate_limit, checks_content_type }: DeriveErrorSchemasOptions) => DerivedErrorSchemas;
|
|
370
375
|
//# sourceMappingURL=error_schemas.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error_schemas.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/error_schemas.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAe,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"error_schemas.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/error_schemas.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAe,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC9D,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAIvE,0CAA0C;AAC1C,eAAO,MAAM,0BAA0B,EAAG,sBAA+B,CAAC;AAE1E,uDAAuD;AACvD,eAAO,MAAM,uBAAuB,EAAG,mBAA4B,CAAC;AAEpE,6CAA6C;AAC7C,eAAO,MAAM,0BAA0B,EAAG,sBAA+B,CAAC;AAE1E,8CAA8C;AAC9C,eAAO,MAAM,0BAA0B,EAAG,sBAA+B,CAAC;AAE1E;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,EAAG,kBAA2B,CAAC;AAIlE,wCAAwC;AACxC,eAAO,MAAM,6BAA6B,EAAG,yBAAkC,CAAC;AAEhF,+CAA+C;AAC/C,eAAO,MAAM,8BAA8B,EAAG,0BAAmC,CAAC;AAElF;;;;;;;;GAQG;AACH,eAAO,MAAM,8BAA8B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always complete.
|
|
7
7
|
*
|
|
8
|
-
* Input/output schemas align with SAES `ActionSpec` conventions
|
|
9
|
-
*
|
|
10
|
-
* - `
|
|
11
|
-
* - `
|
|
8
|
+
* Input/output schemas align with SAES `ActionSpec` conventions, and each is
|
|
9
|
+
* a Zod or `@fuzdev/fuz_schema` schema (`ValidationSchema`):
|
|
10
|
+
* - `input`: the request body schema (`z.null()` or `literal(null)` for no body)
|
|
11
|
+
* - `output`: the success response body schema
|
|
12
|
+
* - a strict object for inputs (reject unknown keys): `z.strictObject()` or `object`
|
|
12
13
|
*
|
|
13
14
|
* @module
|
|
14
15
|
*/
|
|
15
16
|
import type { Context, Hono, MiddlewareHandler } from 'hono';
|
|
16
17
|
import type { z } from 'zod';
|
|
18
|
+
import { type AnySchema } from '@fuzdev/fuz_schema/schema.ts';
|
|
17
19
|
import type { Logger } from '@fuzdev/fuz_util/log.ts';
|
|
18
20
|
import type { Db } from '../db/db.js';
|
|
19
21
|
import { type RouteErrorSchemas, type RateLimitKey } from './error_schemas.js';
|
|
20
22
|
import type { MiddlewareSpec } from './middleware_spec.js';
|
|
21
23
|
import { type RouteAuth } from './auth_shape.js';
|
|
24
|
+
import { type ValidationOutput, type ValidationSchema } from '../schema_issues.js';
|
|
22
25
|
/**
|
|
23
26
|
* Two-phase auth guard set returned by `AuthGuardResolver`.
|
|
24
27
|
*
|
|
@@ -110,8 +113,10 @@ export type RouteHandler = (c: Context, route: RouteContext) => Response | Promi
|
|
|
110
113
|
/**
|
|
111
114
|
* A single route definition — the unit of the surface map.
|
|
112
115
|
*
|
|
113
|
-
* `input` and `output` schemas align with SAES `ActionSpec` naming.
|
|
114
|
-
*
|
|
116
|
+
* `input` and `output` schemas align with SAES `ActionSpec` naming. Every
|
|
117
|
+
* schema field takes a Zod or a `@fuzdev/fuz_schema` schema, validated through
|
|
118
|
+
* `parse_any`. Use `z.null()` (or fuz_schema's `literal(null)`) for routes with
|
|
119
|
+
* no request body (GET, DELETE without body).
|
|
115
120
|
*/
|
|
116
121
|
export interface RouteSpec {
|
|
117
122
|
method: RouteMethod;
|
|
@@ -120,18 +125,25 @@ export interface RouteSpec {
|
|
|
120
125
|
handler: RouteHandler;
|
|
121
126
|
description: string;
|
|
122
127
|
/**
|
|
123
|
-
* URL path parameter schema. Use `z.strictObject()`
|
|
128
|
+
* URL path parameter schema. Use `z.strictObject()` (or fuz_schema's
|
|
129
|
+
* `object`) with string fields matching `:param` segments. A fuz_schema
|
|
130
|
+
* schema must be an object schema under any wrappers (`get_schema_entries`);
|
|
131
|
+
* `apply_route_specs` throws on one that isn't.
|
|
124
132
|
*
|
|
125
133
|
* REST-only — actions dispatch through a single JSON-RPC endpoint and encode
|
|
126
134
|
* everything in `input`, so `params` doesn't appear on `ActionSpec`.
|
|
127
135
|
*/
|
|
128
|
-
params?: z.ZodObject;
|
|
129
|
-
/**
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
136
|
+
params?: z.ZodObject | AnySchema;
|
|
137
|
+
/**
|
|
138
|
+
* URL query parameter schema. Use `z.strictObject()` (or fuz_schema's
|
|
139
|
+
* `object`) with string fields; a fuz_schema schema must be an object
|
|
140
|
+
* schema, as for `params`.
|
|
141
|
+
*/
|
|
142
|
+
query?: z.ZodObject | AnySchema;
|
|
143
|
+
/** Request body schema. Use `z.null()` (or `literal(null)`) for routes with no body. */
|
|
144
|
+
input: ValidationSchema;
|
|
133
145
|
/** Success response body schema. */
|
|
134
|
-
output:
|
|
146
|
+
output: ValidationSchema;
|
|
135
147
|
/**
|
|
136
148
|
* Marks a route whose request and/or response carries **raw bytes or a
|
|
137
149
|
* streaming protocol** rather than JSON — git smart-HTTP, file-store
|
|
@@ -213,7 +225,8 @@ export interface RouteSpec {
|
|
|
213
225
|
* Get validated input from the Hono context.
|
|
214
226
|
*
|
|
215
227
|
* Call after the input validation middleware has run. Pass the route's
|
|
216
|
-
* `input` Zod
|
|
228
|
+
* `input` schema, Zod or `@fuzdev/fuz_schema`, to infer the typed shape
|
|
229
|
+
* directly (`ValidationOutput`):
|
|
217
230
|
*
|
|
218
231
|
* ```ts
|
|
219
232
|
* const input = get_route_input(c, my_route_spec.input);
|
|
@@ -225,7 +238,7 @@ export interface RouteSpec {
|
|
|
225
238
|
* const input = get_route_input<MyInput>(c);
|
|
226
239
|
* ```
|
|
227
240
|
*/
|
|
228
|
-
export declare function get_route_input<S extends
|
|
241
|
+
export declare function get_route_input<S extends ValidationSchema>(c: Context, schema: S): ValidationOutput<S>;
|
|
229
242
|
export declare function get_route_input<T = unknown>(c: Context): T;
|
|
230
243
|
/**
|
|
231
244
|
* Get validated URL path params from the Hono context.
|
|
@@ -234,7 +247,7 @@ export declare function get_route_input<T = unknown>(c: Context): T;
|
|
|
234
247
|
* `params` schema to infer the typed shape, or supply an explicit type
|
|
235
248
|
* argument. See `get_route_input` for the two call shapes.
|
|
236
249
|
*/
|
|
237
|
-
export declare function get_route_params<S extends
|
|
250
|
+
export declare function get_route_params<S extends ValidationSchema>(c: Context, schema: S): ValidationOutput<S>;
|
|
238
251
|
export declare function get_route_params<T = unknown>(c: Context): T;
|
|
239
252
|
/**
|
|
240
253
|
* Get validated URL query params from the Hono context.
|
|
@@ -243,7 +256,7 @@ export declare function get_route_params<T = unknown>(c: Context): T;
|
|
|
243
256
|
* `query` schema to infer the typed shape, or supply an explicit type
|
|
244
257
|
* argument. See `get_route_input` for the two call shapes.
|
|
245
258
|
*/
|
|
246
|
-
export declare function get_route_query<S extends
|
|
259
|
+
export declare function get_route_query<S extends ValidationSchema>(c: Context, schema: S): ValidationOutput<S>;
|
|
247
260
|
export declare function get_route_query<T = unknown>(c: Context): T;
|
|
248
261
|
/**
|
|
249
262
|
* Apply named middleware specs to a Hono app.
|
|
@@ -288,11 +301,15 @@ export declare const apply_middleware_specs: (app: Hono, specs: Array<Middleware
|
|
|
288
301
|
* registries (`compile_action_registry`) share the same helper with
|
|
289
302
|
* `input` only — `ActionSpec` has no `query` shape.
|
|
290
303
|
*
|
|
304
|
+
* A `@fuzdev/fuz_schema` `params` or `query` must be an object schema
|
|
305
|
+
* (`get_schema_entries`), as the path params and query string are an object:
|
|
306
|
+
* one that isn't throws naming the route.
|
|
307
|
+
*
|
|
291
308
|
* @param resolve_auth_guards - maps `RouteAuth` to middleware — use `fuz_auth_guard_resolver` from `auth/auth_guard_resolver.ts`
|
|
292
309
|
* @param authorize - optional authorization phase; runs after the pre-authorization guards and before the post-authorization guards
|
|
293
310
|
* @param db - used for transaction wrapping and `RouteContext`
|
|
294
311
|
* @mutates `app`
|
|
295
|
-
* @throws Error if two specs share the same `method` + `path` (each combination must be unique),
|
|
312
|
+
* @throws Error if two specs share the same `method` + `path` (each combination must be unique), if any spec violates a cross-axis auth invariant or the actor-acting biconditional, or if a fuz_schema `params` or `query` is not an object schema
|
|
296
313
|
*/
|
|
297
314
|
export declare const apply_route_specs: (app: Hono, specs: Array<RouteSpec>, resolve_auth_guards: AuthGuardResolver, log: Logger, db: Db, authorize?: AuthorizationHandler) => void;
|
|
298
315
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route_spec.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/route_spec.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"route_spec.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/route_spec.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAW,IAAI,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AACtE,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,OAAO,EAAa,KAAK,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAEzE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAEtC,OAAO,EACN,KAAK,iBAAiB,EACtB,KAAK,YAAY,EAOjB,M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;AAK7D;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,gBAAgB,EACzD,CAAC,EAAE,OAAO,EACV,MAAM,EAAE,CAAC,GACP,gBAAgB,CAAC,CAAC,CAAC,CAAC;AACvB,wBAAgB,eAAe,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC;AA4M5D;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,GAAI,KAAK,IAAI,EAAE,OAAO,KAAK,CAAC,cAAc,CAAC,KAAG,IAIhF,CAAC;AAsGF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,eAAO,MAAM,iBAAiB,GAC7B,KAAK,IAAI,EACT,OAAO,KAAK,CAAC,SAAS,CAAC,EACvB,qBAAqB,iBAAiB,EACtC,KAAK,MAAM,EACX,IAAI,EAAE,EACN,YAAY,oBAAoB,KAC9B,IAyEF,CAAC;AAeF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,QAAQ,MAAM,EAAE,OAAO,KAAK,CAAC,SAAS,CAAC,KAAG,KAAK,CAAC,SAAS,CAK3F,CAAC"}
|
package/dist/http/route_spec.js
CHANGED
|
@@ -5,14 +5,17 @@
|
|
|
5
5
|
* then applied to Hono. The attack surface is generated from the specs —
|
|
6
6
|
* always accurate, always complete.
|
|
7
7
|
*
|
|
8
|
-
* Input/output schemas align with SAES `ActionSpec` conventions
|
|
9
|
-
*
|
|
10
|
-
* - `
|
|
11
|
-
* - `
|
|
8
|
+
* Input/output schemas align with SAES `ActionSpec` conventions, and each is
|
|
9
|
+
* a Zod or `@fuzdev/fuz_schema` schema (`ValidationSchema`):
|
|
10
|
+
* - `input`: the request body schema (`z.null()` or `literal(null)` for no body)
|
|
11
|
+
* - `output`: the success response body schema
|
|
12
|
+
* - a strict object for inputs (reject unknown keys): `z.strictObject()` or `object`
|
|
12
13
|
*
|
|
13
14
|
* @module
|
|
14
15
|
*/
|
|
15
16
|
import { DEV } from 'esm-env';
|
|
17
|
+
import { is_schema } from '@fuzdev/fuz_schema/schema.ts';
|
|
18
|
+
import { get_schema_entries } from '@fuzdev/fuz_schema/introspect.ts';
|
|
16
19
|
import { is_db_checkout_error } from '../db/db_checkout_error.js';
|
|
17
20
|
import { ERROR_INVALID_JSON_BODY, ERROR_INVALID_REQUEST_BODY, ERROR_INVALID_ROUTE_PARAMS, ERROR_INVALID_QUERY_PARAMS, ERROR_SERVICE_UNAVAILABLE, ERROR_UNSUPPORTED_CONTENT_TYPE } from './error_schemas.js';
|
|
18
21
|
import { is_post_content_type_refused } from './simple_request.js';
|
|
@@ -20,7 +23,7 @@ import { jsonrpc_error_code_to_http_status, jsonrpc_error_code_to_name, dev_only
|
|
|
20
23
|
import { is_null_schema, merge_error_schemas } from './schema_helpers.js';
|
|
21
24
|
import { dispatch_with_post_commit_rollback } from './pending_effects.js';
|
|
22
25
|
import { assert_route_auth_acting_biconditional, assert_route_auth_invariants } from './auth_shape.js';
|
|
23
|
-
import { to_wire_issues } from '../schema_issues.js';
|
|
26
|
+
import { parse_any, to_wire_issues } from '../schema_issues.js';
|
|
24
27
|
export function get_route_input(c, _schema) {
|
|
25
28
|
return c.get('validated_input');
|
|
26
29
|
}
|
|
@@ -83,14 +86,14 @@ const create_input_validation = (input_schema, method) => {
|
|
|
83
86
|
if (typeof body !== 'object' || body === null || Array.isArray(body)) {
|
|
84
87
|
return c.json({ error: ERROR_INVALID_JSON_BODY }, 400);
|
|
85
88
|
}
|
|
86
|
-
const result = input_schema
|
|
87
|
-
if (!result.
|
|
89
|
+
const result = parse_any(input_schema, body);
|
|
90
|
+
if (!result.ok) {
|
|
88
91
|
return c.json({
|
|
89
92
|
error: ERROR_INVALID_REQUEST_BODY,
|
|
90
|
-
issues: dev_only(() => to_wire_issues(result.
|
|
93
|
+
issues: dev_only(() => to_wire_issues(result.issues, body))
|
|
91
94
|
}, 400);
|
|
92
95
|
}
|
|
93
|
-
c.set('validated_input', result.
|
|
96
|
+
c.set('validated_input', result.value);
|
|
94
97
|
await next();
|
|
95
98
|
};
|
|
96
99
|
return [validate];
|
|
@@ -109,14 +112,14 @@ const create_params_validation = (params_schema) => {
|
|
|
109
112
|
return [];
|
|
110
113
|
const validate = async (c, next) => {
|
|
111
114
|
const raw_params = c.req.param();
|
|
112
|
-
const result = params_schema
|
|
113
|
-
if (!result.
|
|
115
|
+
const result = parse_any(params_schema, raw_params);
|
|
116
|
+
if (!result.ok) {
|
|
114
117
|
return c.json({
|
|
115
118
|
error: ERROR_INVALID_ROUTE_PARAMS,
|
|
116
|
-
issues: dev_only(() => to_wire_issues(result.
|
|
119
|
+
issues: dev_only(() => to_wire_issues(result.issues, raw_params))
|
|
117
120
|
}, 400);
|
|
118
121
|
}
|
|
119
|
-
c.set('validated_params', result.
|
|
122
|
+
c.set('validated_params', result.value);
|
|
120
123
|
await next();
|
|
121
124
|
};
|
|
122
125
|
return [validate];
|
|
@@ -135,14 +138,14 @@ const create_query_validation = (query_schema) => {
|
|
|
135
138
|
return [];
|
|
136
139
|
const validate = async (c, next) => {
|
|
137
140
|
const raw_query = c.req.query();
|
|
138
|
-
const result = query_schema
|
|
139
|
-
if (!result.
|
|
141
|
+
const result = parse_any(query_schema, raw_query);
|
|
142
|
+
if (!result.ok) {
|
|
140
143
|
return c.json({
|
|
141
144
|
error: ERROR_INVALID_QUERY_PARAMS,
|
|
142
|
-
issues: dev_only(() => to_wire_issues(result.
|
|
145
|
+
issues: dev_only(() => to_wire_issues(result.issues, raw_query))
|
|
143
146
|
}, 400);
|
|
144
147
|
}
|
|
145
|
-
c.set('validated_query', result.
|
|
148
|
+
c.set('validated_query', result.value);
|
|
146
149
|
await next();
|
|
147
150
|
};
|
|
148
151
|
return [validate];
|
|
@@ -163,6 +166,8 @@ const is_service_unavailable_body = (body) => body !== null &&
|
|
|
163
166
|
* catch layer, so the body built from a thrown error is validated too — except
|
|
164
167
|
* a 500 `internal_error` and a failed checkout's 503 `service_unavailable`,
|
|
165
168
|
* which the catch layer can answer on any route and so are always accepted.
|
|
169
|
+
* Each schema is Zod or `@fuzdev/fuz_schema`, parsed with `parse_any`; the
|
|
170
|
+
* log carries the library's own issues.
|
|
166
171
|
* Logs an error for mismatches and returns the response unchanged —
|
|
167
172
|
* does not throw. Non-JSON responses (SSE, binary) are skipped. In
|
|
168
173
|
* production, returns the handler unchanged.
|
|
@@ -197,12 +202,12 @@ const wrap_output_validation = (handler, output_schema, error_schemas, log) => {
|
|
|
197
202
|
// the same for a failed pool checkout's 503, which any route can answer
|
|
198
203
|
if (response.status === 503 && is_service_unavailable_body(body))
|
|
199
204
|
return response;
|
|
200
|
-
const result = schema
|
|
201
|
-
if (!result.
|
|
205
|
+
const result = parse_any(schema, body);
|
|
206
|
+
if (!result.ok) {
|
|
202
207
|
const label = response.ok
|
|
203
208
|
? 'Output schema mismatch'
|
|
204
209
|
: `Error schema mismatch (${response.status})`;
|
|
205
|
-
log.error(`${label}: ${c.req.method} ${c.req.path}`, result.
|
|
210
|
+
log.error(`${label}: ${c.req.method} ${c.req.path}`, result.issues);
|
|
206
211
|
}
|
|
207
212
|
return response;
|
|
208
213
|
};
|
|
@@ -351,11 +356,15 @@ const build_rest_error_body = (err) => {
|
|
|
351
356
|
* registries (`compile_action_registry`) share the same helper with
|
|
352
357
|
* `input` only — `ActionSpec` has no `query` shape.
|
|
353
358
|
*
|
|
359
|
+
* A `@fuzdev/fuz_schema` `params` or `query` must be an object schema
|
|
360
|
+
* (`get_schema_entries`), as the path params and query string are an object:
|
|
361
|
+
* one that isn't throws naming the route.
|
|
362
|
+
*
|
|
354
363
|
* @param resolve_auth_guards - maps `RouteAuth` to middleware — use `fuz_auth_guard_resolver` from `auth/auth_guard_resolver.ts`
|
|
355
364
|
* @param authorize - optional authorization phase; runs after the pre-authorization guards and before the post-authorization guards
|
|
356
365
|
* @param db - used for transaction wrapping and `RouteContext`
|
|
357
366
|
* @mutates `app`
|
|
358
|
-
* @throws Error if two specs share the same `method` + `path` (each combination must be unique),
|
|
367
|
+
* @throws Error if two specs share the same `method` + `path` (each combination must be unique), if any spec violates a cross-axis auth invariant or the actor-acting biconditional, or if a fuz_schema `params` or `query` is not an object schema
|
|
359
368
|
*/
|
|
360
369
|
export const apply_route_specs = (app, specs, resolve_auth_guards, log, db, authorize) => {
|
|
361
370
|
const registered = new Set();
|
|
@@ -365,6 +374,8 @@ export const apply_route_specs = (app, specs, resolve_auth_guards, log, db, auth
|
|
|
365
374
|
throw new Error(`Duplicate route: ${route_key} — each method+path combination must be unique`);
|
|
366
375
|
}
|
|
367
376
|
registered.add(route_key);
|
|
377
|
+
assert_object_slot(spec.params, 'params', route_key);
|
|
378
|
+
assert_object_slot(spec.query, 'query', route_key);
|
|
368
379
|
assert_route_auth_invariants(spec.auth, `Route "${route_key}"`);
|
|
369
380
|
assert_route_auth_acting_biconditional(spec.auth, { input: spec.input, query: spec.query }, `Route "${route_key}"`);
|
|
370
381
|
const { pre_authorization: pre_authorization_guards, post_authorization: post_authorization_guards } = resolve_auth_guards(spec.auth);
|
|
@@ -408,6 +419,13 @@ export const apply_route_specs = (app, specs, resolve_auth_guards, log, db, auth
|
|
|
408
419
|
app.on(spec.method, [spec.path], ...content_type_check, ...params_validation, ...query_validation, ...pre_authorization_guards, ...authorization, ...post_authorization_guards, ...input_validation, handler);
|
|
409
420
|
}
|
|
410
421
|
};
|
|
422
|
+
// throws unless a fuz_schema `params` / `query` is an object schema under any
|
|
423
|
+
// wrappers; a Zod one is a `z.ZodObject` by its type
|
|
424
|
+
const assert_object_slot = (schema, slot, route_key) => {
|
|
425
|
+
if (schema === undefined || !is_schema(schema) || get_schema_entries(schema))
|
|
426
|
+
return;
|
|
427
|
+
throw new Error(`Route "${route_key}": ${slot} is a @fuzdev/fuz_schema '${schema.kind}' schema, not an object schema`);
|
|
428
|
+
};
|
|
411
429
|
/**
|
|
412
430
|
* Prepend a prefix to all route spec paths.
|
|
413
431
|
*
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @module
|
|
9
9
|
*/
|
|
10
|
-
import { z } from 'zod';
|
|
11
10
|
import type { RouteAuth } from './auth_shape.js';
|
|
12
11
|
import type { RouteMethod } from './route_spec.js';
|
|
13
12
|
import { type RateLimitKey, type RouteErrorSchemas } from './error_schemas.js';
|
|
@@ -245,9 +244,9 @@ export declare const middleware_spec_applies: (spec: {
|
|
|
245
244
|
export declare const merge_error_schemas: (spec: {
|
|
246
245
|
method: RouteMethod;
|
|
247
246
|
auth: RouteAuth;
|
|
248
|
-
input:
|
|
249
|
-
params?:
|
|
250
|
-
query?:
|
|
247
|
+
input: ValidationSchema;
|
|
248
|
+
params?: ValidationSchema;
|
|
249
|
+
query?: ValidationSchema;
|
|
251
250
|
rate_limit?: RateLimitKey;
|
|
252
251
|
errors?: RouteErrorSchemas;
|
|
253
252
|
}, middleware_errors?: RouteErrorSchemas | null) => RouteErrorSchemas | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema_helpers.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/schema_helpers.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;
|
|
1
|
+
{"version":3,"file":"schema_helpers.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/schema_helpers.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAQH,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;AAC5B,OAAO,EAGN,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,MAAM,qBAAqB,CAAC;AAE7B;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,GAAI,QAAQ,gBAAgB,KAAG,OAEc,CAAC;AAEzE;;;;;;;;;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AO,MAAM,wBAAwB,GAAI,GAAG,MAAM,EAAE,GAAG,MAAM,KAAG,OAM/D,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,GACnC,MAAM;IACL,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,UAAU,CAAC,EAAE,aAAa,CAAC;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,GAAG,IAAI,CAAC;CACtE,EACD,YAAY,MAAM,KAChB,OAE0F,CAAC;AAE9F;;;;;;;;;GASG;AACH,eAAO,MAAM,mBAAmB,GAC/B,MAAM;IACL,MAAM,EAAE,WAAW,CAAC;IACpB,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,EAAE,gBAAgB,CAAC;IACxB,MAAM,CAAC,EAAE,gBAAgB,CAAC;IAC1B,KAAK,CAAC,EAAE,gBAAgB,CAAC;IACzB,UAAU,CAAC,EAAE,YAAY,CAAC;IAC1B,MAAM,CAAC,EAAE,iBAAiB,CAAC;CAC3B,EACD,oBAAoB,iBAAiB,GAAG,IAAI,KAC1C,iBAAiB,GAAG,IAWtB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"surface.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/surface.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,
|
|
1
|
+
{"version":3,"file":"surface.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/http/surface.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAoB,KAAK,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAEhF,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,E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eAAe;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC;CAC1B;AAED,0CAA0C;AAC1C,MAAM,WAAW,yBAAyB;IACzC,WAAW,EAAE,KAAK,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;AA2BF;;;GAGG;AACH,eAAO,MAAM,oBAAoB,GAAI,SAAS,yBAAyB,KAAG,UA6IzE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,uBAAuB,GAAI,SAAS,yBAAyB,KAAG,cAS5E,CAAC"}
|
package/dist/http/surface.js
CHANGED
|
@@ -7,8 +7,7 @@
|
|
|
7
7
|
* @module
|
|
8
8
|
*/
|
|
9
9
|
import { get_schema_entries, get_schema_fields } from '@fuzdev/fuz_schema/introspect.ts';
|
|
10
|
-
import { parse } from '@fuzdev/fuz_schema/schema.ts';
|
|
11
|
-
import { z } from 'zod';
|
|
10
|
+
import { is_schema, parse } from '@fuzdev/fuz_schema/schema.ts';
|
|
12
11
|
import { schema_to_surface, middleware_spec_applies, merge_error_schemas, is_null_schema, is_strict_object_schema } from './schema_helpers.js';
|
|
13
12
|
import { find_schema_sensitivity } from '../sensitivity.js';
|
|
14
13
|
// --- Surface generation ---
|
|
@@ -83,6 +82,9 @@ const create_to_surface = (diagnostics) => (schema, io, source) => schema_to_sur
|
|
|
83
82
|
})
|
|
84
83
|
: undefined
|
|
85
84
|
});
|
|
85
|
+
// a fuz_schema input that is not a strict `object`: a `strip_object` drops
|
|
86
|
+
// unknown keys and a `loose_object` keeps them, so the text names neither
|
|
87
|
+
const FUZ_SCHEMA_NOT_STRICT_INPUT_MESSAGE = 'Input schema is not a strict object (@fuzdev/fuz_schema `object`) — unknown keys may not be refused';
|
|
86
88
|
/**
|
|
87
89
|
* Generate a JSON-serializable attack surface from middleware, route specs,
|
|
88
90
|
* and optional env/event metadata.
|
|
@@ -97,7 +99,9 @@ export const generate_app_surface = (options) => {
|
|
|
97
99
|
diagnostics.push({
|
|
98
100
|
level: 'warning',
|
|
99
101
|
category: 'schema',
|
|
100
|
-
message:
|
|
102
|
+
message: is_schema(r.input)
|
|
103
|
+
? FUZ_SCHEMA_NOT_STRICT_INPUT_MESSAGE
|
|
104
|
+
: 'Input schema is not z.strictObject() — unknown keys will be silently stripped',
|
|
101
105
|
source: `${r.method} ${r.path} input`
|
|
102
106
|
});
|
|
103
107
|
}
|
|
@@ -122,11 +122,15 @@ export declare const create_uuid: () => Uuid;
|
|
|
122
122
|
* Branded UUID schema. Accepts either case and outputs lowercase — the form
|
|
123
123
|
* Postgres renders a `uuid` in and every id the server mints — so a parsed id
|
|
124
124
|
* compares equal to the same id read back from the database. The Rust spine
|
|
125
|
-
* canonicalizes the same way
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
125
|
+
* canonicalizes the same way, and its grammar is this one, the fuz_schema
|
|
126
|
+
* twin's in `primitive_schemas_fuz.ts`: `z.uuid()` with the max UUID taken in
|
|
127
|
+
* any case. Issues and JSON Schema keep `format: 'uuid'`; only the `pattern`
|
|
128
|
+
* differs from a bare `z.uuid()`'s, rendered or on a Zod issue (the wire
|
|
129
|
+
* issue drops it).
|
|
130
|
+
*
|
|
131
|
+
* `pattern` is a `$ZodUUIDDef` field that `z.uuid()` honors (its default
|
|
132
|
+
* applies only when none is given) but whose public params type omits it,
|
|
133
|
+
* hence the cast on the params.
|
|
130
134
|
*
|
|
131
135
|
* `.toLowerCase()` rather than `.transform()`: an overwrite keeps the output
|
|
132
136
|
* type and stays invisible to `z.toJSONSchema`, where a transform would leave
|
|
@@ -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,KAAK,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AAC5D,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;AAC7G,eAAO,MAAM,mBAAmB,KAAK,CAAC;AAEtC,gNAAgN;AAChN,eAAO,MAAM,4BAA4B,MAAM,CAAC;AAEhD;;;;;;;;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F,eAAO,MAAM,WAAW,uEAAqC,CAAC;AAC9D,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,WAAW,CAAC,CAAC;AAEtD,uFAAuF;AACvF,eAAO,MAAM,UAAU,aAEuD,CAAC;AAC/E,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,UAAU,CAAC,CAAC;AAEpD;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,cAAc,EAAE,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,CAEA,CAAC"}
|
|
@@ -182,15 +182,29 @@ export const Email = z
|
|
|
182
182
|
});
|
|
183
183
|
/** Returns a new random (v4) `Uuid`. */
|
|
184
184
|
export const create_uuid = () => crypto.randomUUID();
|
|
185
|
+
/**
|
|
186
|
+
* The UUID grammar of RFC 9562, in either case: five groups of hex digits,
|
|
187
|
+
* 8-4-4-4-12, with a version from 1 to 8 and the RFC's variant, or the nil or
|
|
188
|
+
* the max UUID. It is `z.uuid()`'s own but for the max UUID, which `z.uuid()`
|
|
189
|
+
* spells in lowercase only, where RFC 9562 §4 reads a UUID case-insensitively.
|
|
190
|
+
* The same source as the pattern fuz_schema's `uuid` renders to JSON Schema,
|
|
191
|
+
* which a test holds it to, and the grammar of the Rust spine's
|
|
192
|
+
* `is_valid_uuid`.
|
|
193
|
+
*/
|
|
194
|
+
const UUID_REGEX = /^(?:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|[fF]{8}-[fF]{4}-[fF]{4}-[fF]{4}-[fF]{12})$/;
|
|
185
195
|
/**
|
|
186
196
|
* Branded UUID schema. Accepts either case and outputs lowercase — the form
|
|
187
197
|
* Postgres renders a `uuid` in and every id the server mints — so a parsed id
|
|
188
198
|
* compares equal to the same id read back from the database. The Rust spine
|
|
189
|
-
* canonicalizes the same way
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
199
|
+
* canonicalizes the same way, and its grammar is this one, the fuz_schema
|
|
200
|
+
* twin's in `primitive_schemas_fuz.ts`: `z.uuid()` with the max UUID taken in
|
|
201
|
+
* any case. Issues and JSON Schema keep `format: 'uuid'`; only the `pattern`
|
|
202
|
+
* differs from a bare `z.uuid()`'s, rendered or on a Zod issue (the wire
|
|
203
|
+
* issue drops it).
|
|
204
|
+
*
|
|
205
|
+
* `pattern` is a `$ZodUUIDDef` field that `z.uuid()` honors (its default
|
|
206
|
+
* applies only when none is given) but whose public params type omits it,
|
|
207
|
+
* hence the cast on the params.
|
|
194
208
|
*
|
|
195
209
|
* `.toLowerCase()` rather than `.transform()`: an overwrite keeps the output
|
|
196
210
|
* type and stays invisible to `z.toJSONSchema`, where a transform would leave
|
|
@@ -201,7 +215,7 @@ export const create_uuid = () => crypto.randomUUID();
|
|
|
201
215
|
* brand, which a fuz_schema `Uuid` isn't assignable to.
|
|
202
216
|
*/
|
|
203
217
|
export const Uuid = z
|
|
204
|
-
.uuid()
|
|
218
|
+
.uuid({ pattern: UUID_REGEX })
|
|
205
219
|
.toLowerCase();
|
|
206
220
|
/** `Uuid` that defaults to a fresh `create_uuid()` when the input is `undefined`. */
|
|
207
221
|
export const UuidWithDefault = Uuid.default(create_uuid);
|
|
@@ -7,11 +7,9 @@
|
|
|
7
7
|
* `Branded<string, 'Uuid'>` too, so a value parsed by either twin goes where
|
|
8
8
|
* the other's is taken.
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* lowercase only. Every other string gets the same verdict and output from
|
|
14
|
-
* both.
|
|
10
|
+
* Both twins have one grammar, RFC 9562's read case-insensitively as its §4
|
|
11
|
+
* says, the max UUID included, and the Rust spine's: every input gets the same
|
|
12
|
+
* verdict and output from both, and their JSON Schema is the same.
|
|
15
13
|
*
|
|
16
14
|
* The exports are `@nodocs` while the Zod schemas of the same names stay, as
|
|
17
15
|
* the docs build refuses two exports of one name.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"primitive_schemas_fuz.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/primitive_schemas_fuz.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"primitive_schemas_fuz.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/primitive_schemas_fuz.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,EAQN,KAAK,KAAK,EACV,MAAM,8BAA8B,CAAC;AAItC;;;;;;;GAOG;AACH,eAAO,MAAM,IAAI;;;;;CAAmD,CAAC;AACrE,cAAc;AACd,MAAM,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,IAAI,CAAC,CAAC;AAEtC;;;;GAIG;AACH,eAAO,MAAM,eAAe;;;;;GAAqC,CAAC;AAClE,cAAc;AACd,MAAM,MAAM,eAAe,GAAG,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AAE5D,wDAAwD;AACxD,eAAO,MAAM,wBAAwB,0HACmF,CAAC;AAEzH;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW;;;;;GAAkE,CAAC;AAC3F,cAAc;AACd,MAAM,MAAM,WAAW,GAAG,KAAK,CAAC,OAAO,WAAW,CAAC,CAAC"}
|
|
@@ -7,11 +7,9 @@
|
|
|
7
7
|
* `Branded<string, 'Uuid'>` too, so a value parsed by either twin goes where
|
|
8
8
|
* the other's is taken.
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* lowercase only. Every other string gets the same verdict and output from
|
|
14
|
-
* both.
|
|
10
|
+
* Both twins have one grammar, RFC 9562's read case-insensitively as its §4
|
|
11
|
+
* says, the max UUID included, and the Rust spine's: every input gets the same
|
|
12
|
+
* verdict and output from both, and their JSON Schema is the same.
|
|
15
13
|
*
|
|
16
14
|
* The exports are `@nodocs` while the Zod schemas of the same names stay, as
|
|
17
15
|
* the docs build refuses two exports of one name.
|
package/dist/testing/CLAUDE.md
CHANGED
|
@@ -403,8 +403,8 @@ or Postgres and runs in the `cross-ts` CI job.
|
|
|
403
403
|
- `assert_surface_deterministic(build_surface)` — build twice, `deepStrictEqual` results; catches nondeterminism in surface generation.
|
|
404
404
|
- `assert_only_expected_public_routes(surface, list)` — bidirectional: no unexpected public routes, no missing expected ones. Format: `['GET /health', 'POST /api/account/login']`.
|
|
405
405
|
- `assert_full_middleware_stack(surface, prefix, mws)` — every route under `prefix` has exactly `mws` as its middleware chain.
|
|
406
|
-
- `get_route_error_schema(lookup, route, status)` — reads from a pre-built merged-error-schema map.
|
|
407
|
-
- `assert_error_schema_valid(lookup, route, status, body)` — assert a schema exists + parses the body.
|
|
406
|
+
- `get_route_error_schema(lookup, route, status)` — reads from a pre-built merged-error-schema map; the schema is Zod or fuz_schema (`ValidationSchema`).
|
|
407
|
+
- `assert_error_schema_valid(lookup, route, status, body)` — assert a schema exists + parses the body (`parse_any`); a mismatch throws an `Error` carrying the issues.
|
|
408
408
|
|
|
409
409
|
### `surface_invariants.ts` — structural + policy invariants
|
|
410
410
|
|
|
@@ -474,7 +474,7 @@ Methods:
|
|
|
474
474
|
|
|
475
475
|
Support functions:
|
|
476
476
|
|
|
477
|
-
- `extract_declared_error_codes(schema)` — reads `schema.shape.error
|
|
477
|
+
- `extract_declared_error_codes(schema)` — reads the object's `error` field, Zod (`schema.shape.error`) or fuz_schema (`get_schema_entries`, any object kind under any wrappers); returns the literal value(s) for `z.literal` / `z.enum` / `literal` / `one_of`, `null` otherwise.
|
|
478
478
|
- `assert_error_coverage(collector, specs, options?)` — logs `[error coverage] covered/total (N.M%)` with uncovered list; fails when `min_coverage > 0` and the ratio falls below.
|
|
479
479
|
- `DEFAULT_INTEGRATION_ERROR_COVERAGE = 0.2` — conservative baseline for the standard integration/admin suites; consumers tighten as their own test coverage matures.
|
|
480
480
|
|
|
@@ -564,9 +564,9 @@ rather than `ERROR_INVALID_JSON_BODY`.
|
|
|
564
564
|
|
|
565
565
|
`describe_adversarial_404({build, roles})` — for every route with `params` +
|
|
566
566
|
404 in `error_schemas` + an extractable error code (`z.literal` or first
|
|
567
|
-
`z.enum`), replaces the handler with a
|
|
568
|
-
fires with nil-UUID params, asserts 404 +
|
|
569
|
-
schema. No DB needed. The code comes from `extract_404_error_code(schema)`,
|
|
567
|
+
`z.enum`, fuz_schema `literal` or first `one_of`), replaces the handler with a
|
|
568
|
+
stub returning `{error: <code>}`, fires with nil-UUID params, asserts 404 +
|
|
569
|
+
body matches the declared 404 schema, Zod or fuz_schema (`parse_any`). No DB needed. The code comes from `extract_404_error_code(schema)`,
|
|
570
570
|
which reads the output-side JSON Schema of a Zod or fuz_schema schema with
|
|
571
571
|
`unrepresentable: 'any'`, so a field JSON Schema can't say beside `error`
|
|
572
572
|
(a transform) no longer drops the route from the suite.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"admin_integration.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/admin_integration.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAgC7B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,EAAc,KAAK,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAY3E,OAAO,EAMN,KAAK,uBAAuB,EAC5B,MAAM,kBAAkB,CAAC;AA4B1B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAE,SAAS,EAAe,MAAM,0BAA0B,CAAC;AAavE;;GAEG;AACH,MAAM,WAAW,mCAAmC;IACnD;;;;OAIG;IACH,UAAU,EAAE,SAAS,CAAC;IACtB;;;;OAIG;IACH,cAAc,EAAE,cAAc,CAAC;IAC/B,uFAAuF;IACvF,eAAe,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;IACxC,4GAA4G;IAC5G,KAAK,EAAE,gBAAgB,CAAC;IACxB;;;OAGG;IACH,aAAa,EAAE,uBAAuB,CAAC;IACvC;;;;;OAKG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB;AAiBD;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,yCAAyC,GACrD,SAAS,mCAAmC,KAC1C,
|
|
1
|
+
{"version":3,"file":"admin_integration.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/admin_integration.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAgC7B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,EAAc,KAAK,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAY3E,OAAO,EAMN,KAAK,uBAAuB,EAC5B,MAAM,kBAAkB,CAAC;AA4B1B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAE,SAAS,EAAe,MAAM,0BAA0B,CAAC;AAavE;;GAEG;AACH,MAAM,WAAW,mCAAmC;IACnD;;;;OAIG;IACH,UAAU,EAAE,SAAS,CAAC;IACtB;;;;OAIG;IACH,cAAc,EAAE,cAAc,CAAC;IAC/B,uFAAuF;IACvF,eAAe,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;IACxC,4GAA4G;IAC5G,KAAK,EAAE,gBAAgB,CAAC;IACxB;;;OAGG;IACH,aAAa,EAAE,uBAAuB,CAAC;IACvC;;;;;OAKG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB;AAiBD;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,yCAAyC,GACrD,SAAS,mCAAmC,KAC1C,IAg+BF,CAAC"}
|
|
@@ -510,8 +510,9 @@ export const describe_standard_admin_integration_tests = (options) => {
|
|
|
510
510
|
});
|
|
511
511
|
test('admin session revoke-all 404 emits failure audit', async () => {
|
|
512
512
|
const fixture = await options.setup_test();
|
|
513
|
-
// `Uuid
|
|
514
|
-
// handler's account lookup rather than failing at param
|
|
513
|
+
// `Uuid` checks the RFC 9562 version and variant; use a valid v4 shape
|
|
514
|
+
// so we hit the handler's account lookup rather than failing at param
|
|
515
|
+
// validation.
|
|
515
516
|
const missing_id = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaa01';
|
|
516
517
|
const res = await rpc_call_for_spec({
|
|
517
518
|
app: { request: fixture.transport },
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import './assert_dev_env.js';
|
|
2
|
-
import type
|
|
2
|
+
import { type ValidationSchema } from '../schema_issues.js';
|
|
3
3
|
import type { AdversarialTestOptions } from './attack_surface.js';
|
|
4
4
|
/**
|
|
5
5
|
* Extract the error code from a 404 error schema, Zod or `@fuzdev/fuz_schema`,
|
|
@@ -22,7 +22,7 @@ export declare const extract_404_error_code: (schema: ValidationSchema) => strin
|
|
|
22
22
|
* 1. Creates a stub handler returning 404 with the declared error code
|
|
23
23
|
* 2. Fires a request with valid-format params (nil UUIDs for UUID params)
|
|
24
24
|
* 3. Validates response status is 404
|
|
25
|
-
* 4. Validates response body matches the declared 404
|
|
25
|
+
* 4. Validates response body matches the declared 404 schema (`parse_any`)
|
|
26
26
|
*/
|
|
27
27
|
export declare const describe_adversarial_404: (options: AdversarialTestOptions) => void;
|
|
28
28
|
//# sourceMappingURL=adversarial_404.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adversarial_404.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/adversarial_404.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"adversarial_404.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/adversarial_404.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAsB7B,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvE,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAOlE;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,sBAAsB,GAAI,QAAQ,gBAAgB,KAAG,MAAM,GAAG,IAe1E,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,wBAAwB,GAAI,SAAS,sBAAsB,KAAG,IAqF1E,CAAC"}
|
|
@@ -4,7 +4,8 @@ import './assert_dev_env.js';
|
|
|
4
4
|
*
|
|
5
5
|
* Creates stub handlers that return 404 with the declared error code,
|
|
6
6
|
* fires requests with valid-format-but-nonexistent params (nil UUIDs),
|
|
7
|
-
* and validates response bodies against the declared 404 Zod
|
|
7
|
+
* and validates response bodies against the declared 404 schemas, Zod or
|
|
8
|
+
* `@fuzdev/fuz_schema`.
|
|
8
9
|
*
|
|
9
10
|
* No DB needed — tests schema conformance of 404 responses, not real handlers.
|
|
10
11
|
*
|
|
@@ -15,6 +16,7 @@ import { z } from 'zod';
|
|
|
15
16
|
import { is_schema } from '@fuzdev/fuz_schema/schema.ts';
|
|
16
17
|
import { to_json_schema } from '@fuzdev/fuz_schema/json_schema.ts';
|
|
17
18
|
import { is_null_schema } from '../http/schema_helpers.js';
|
|
19
|
+
import { parse_any } from '../schema_issues.js';
|
|
18
20
|
import { create_auth_test_apps, create_route_skip_filter, select_auth_app } from './auth_apps.js';
|
|
19
21
|
import { resolve_valid_path, generate_valid_body, to_route_content_type } from './schema_generators.js';
|
|
20
22
|
/**
|
|
@@ -53,13 +55,13 @@ export const extract_404_error_code = (schema) => {
|
|
|
53
55
|
* 1. Creates a stub handler returning 404 with the declared error code
|
|
54
56
|
* 2. Fires a request with valid-format params (nil UUIDs for UUID params)
|
|
55
57
|
* 3. Validates response status is 404
|
|
56
|
-
* 4. Validates response body matches the declared 404
|
|
58
|
+
* 4. Validates response body matches the declared 404 schema (`parse_any`)
|
|
57
59
|
*/
|
|
58
60
|
export const describe_adversarial_404 = (options) => {
|
|
59
61
|
const { build, roles } = options;
|
|
60
62
|
const { surface, route_specs } = build();
|
|
61
63
|
const is_skipped = create_route_skip_filter(options.skip_routes);
|
|
62
|
-
// Build spec lookup for
|
|
64
|
+
// Build spec lookup for schema access
|
|
63
65
|
const spec_lookup = new Map();
|
|
64
66
|
for (const spec of route_specs) {
|
|
65
67
|
spec_lookup.set(`${spec.method} ${spec.path}`, spec);
|
|
@@ -120,8 +122,9 @@ export const describe_adversarial_404 = (options) => {
|
|
|
120
122
|
assert.strictEqual(res.status, 404, `Expected 404 for ${key}, got ${res.status}`);
|
|
121
123
|
const body = await res.json();
|
|
122
124
|
assert.strictEqual(body.error, error_code, `Expected error '${error_code}' for ${key}, got: ${body.error}`);
|
|
123
|
-
// Validate against declared 404
|
|
124
|
-
spec.errors[404]
|
|
125
|
+
// Validate against the declared 404 schema
|
|
126
|
+
const parsed = parse_any(spec.errors[404], body);
|
|
127
|
+
assert.ok(parsed.ok, `404 body for ${key} does not match its declared schema: ${parsed.ok ? '' : JSON.stringify(parsed.issues)}`);
|
|
125
128
|
});
|
|
126
129
|
}
|
|
127
130
|
});
|
|
@@ -46,7 +46,7 @@ export const describe_adversarial_input = (options) => {
|
|
|
46
46
|
routes_with_params.length === 0 &&
|
|
47
47
|
routes_with_query.length === 0)
|
|
48
48
|
return;
|
|
49
|
-
// lookup RouteSpec by method+path for
|
|
49
|
+
// lookup RouteSpec by method+path for schema access
|
|
50
50
|
const spec_lookup = new Map();
|
|
51
51
|
for (const spec of route_specs) {
|
|
52
52
|
spec_lookup.set(`${spec.method} ${spec.path}`, spec);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import './assert_dev_env.js';
|
|
2
|
-
import type { z } from 'zod';
|
|
3
2
|
import type { AppSurface, AppSurfaceRoute } from '../http/surface.js';
|
|
4
3
|
import type { RouteErrorSchemas } from '../http/error_schemas.js';
|
|
4
|
+
import { type ValidationSchema } from '../schema_issues.js';
|
|
5
5
|
/**
|
|
6
6
|
* Resolve an absolute path relative to the caller's module.
|
|
7
7
|
*
|
|
@@ -42,10 +42,10 @@ export declare const assert_only_expected_public_routes: (surface: AppSurface, e
|
|
|
42
42
|
* @param lookup - map from `"METHOD /path"` to merged error schemas
|
|
43
43
|
* @param route - the surface route to look up
|
|
44
44
|
* @param status - HTTP status code
|
|
45
|
-
* @returns the Zod schema for that route+status, or
|
|
46
|
-
* schema is declared for the status code
|
|
45
|
+
* @returns the Zod or `@fuzdev/fuz_schema` schema for that route+status, or
|
|
46
|
+
* `undefined` when no error schema is declared for the status code
|
|
47
47
|
*/
|
|
48
|
-
export declare const get_route_error_schema: (lookup: Map<string, RouteErrorSchemas>, route: AppSurfaceRoute, status: number) =>
|
|
48
|
+
export declare const get_route_error_schema: (lookup: Map<string, RouteErrorSchemas>, route: AppSurfaceRoute, status: number) => ValidationSchema | undefined;
|
|
49
49
|
/**
|
|
50
50
|
* Assert that an error schema exists for a route+status and validate the body against it.
|
|
51
51
|
*
|
|
@@ -57,7 +57,8 @@ export declare const get_route_error_schema: (lookup: Map<string, RouteErrorSche
|
|
|
57
57
|
* @param status - expected HTTP status code
|
|
58
58
|
* @param body - the parsed response body to validate
|
|
59
59
|
* @throws AssertionError if no schema is declared for the route+status pair.
|
|
60
|
-
* @throws
|
|
60
|
+
* @throws Error if the body does not satisfy the declared schema, its message
|
|
61
|
+
* carrying the failed parse's issues (`parse_any`).
|
|
61
62
|
*/
|
|
62
63
|
export declare const assert_error_schema_valid: (lookup: Map<string, RouteErrorSchemas>, route: AppSurfaceRoute, status: number, body: unknown) => void;
|
|
63
64
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assertions.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/assertions.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"assertions.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/assertions.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAgB7B,OAAO,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAElE,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvE;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,GAAI,UAAU,MAAM,EAAE,iBAAiB,MAAM,KAAG,MACtB,CAAC;AAE5D;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,+BAA+B,GAC3C,SAAS,UAAU,EACnB,eAAe,MAAM,KACnB,IAOF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,4BAA4B,GAAI,eAAe,MAAM,UAAU,KAAG,IAE9E,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,kCAAkC,GAC9C,SAAS,UAAU,EACnB,iBAAiB,KAAK,CAAC,MAAM,CAAC,KAC5B,IAWF,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,sBAAsB,GAClC,QAAQ,GAAG,CAAC,MAAM,EAAE,iBAAiB,CAAC,EACtC,OAAO,eAAe,EACtB,QAAQ,MAAM,KACZ,gBAAgB,GAAG,SAGrB,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,yBAAyB,GACrC,QAAQ,GAAG,CAAC,MAAM,EAAE,iBAAiB,CAAC,EACtC,OAAO,eAAe,EACtB,QAAQ,MAAM,EACd,MAAM,OAAO,KACX,IAWF,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,4BAA4B,GACxC,SAAS,UAAU,EACnB,aAAa,MAAM,EACnB,qBAAqB,KAAK,CAAC,MAAM,CAAC,KAChC,IAUF,CAAC"}
|
|
@@ -12,6 +12,7 @@ import { resolve, dirname } from 'node:path';
|
|
|
12
12
|
import { fileURLToPath } from 'node:url';
|
|
13
13
|
import { assert } from 'vitest';
|
|
14
14
|
import { is_public_auth } from '../http/auth_shape.js';
|
|
15
|
+
import { parse_any } from '../schema_issues.js';
|
|
15
16
|
/**
|
|
16
17
|
* Resolve an absolute path relative to the caller's module.
|
|
17
18
|
*
|
|
@@ -66,8 +67,8 @@ export const assert_only_expected_public_routes = (surface, expected_public) =>
|
|
|
66
67
|
* @param lookup - map from `"METHOD /path"` to merged error schemas
|
|
67
68
|
* @param route - the surface route to look up
|
|
68
69
|
* @param status - HTTP status code
|
|
69
|
-
* @returns the Zod schema for that route+status, or
|
|
70
|
-
* schema is declared for the status code
|
|
70
|
+
* @returns the Zod or `@fuzdev/fuz_schema` schema for that route+status, or
|
|
71
|
+
* `undefined` when no error schema is declared for the status code
|
|
71
72
|
*/
|
|
72
73
|
export const get_route_error_schema = (lookup, route, status) => {
|
|
73
74
|
const key = `${route.method} ${route.path}`;
|
|
@@ -84,12 +85,16 @@ export const get_route_error_schema = (lookup, route, status) => {
|
|
|
84
85
|
* @param status - expected HTTP status code
|
|
85
86
|
* @param body - the parsed response body to validate
|
|
86
87
|
* @throws AssertionError if no schema is declared for the route+status pair.
|
|
87
|
-
* @throws
|
|
88
|
+
* @throws Error if the body does not satisfy the declared schema, its message
|
|
89
|
+
* carrying the failed parse's issues (`parse_any`).
|
|
88
90
|
*/
|
|
89
91
|
export const assert_error_schema_valid = (lookup, route, status, body) => {
|
|
90
92
|
const schema = get_route_error_schema(lookup, route, status);
|
|
91
93
|
assert.ok(schema, `missing error schema for ${status} on ${route.method} ${route.path}`);
|
|
92
|
-
schema
|
|
94
|
+
const result = parse_any(schema, body);
|
|
95
|
+
if (!result.ok) {
|
|
96
|
+
throw new Error(`error schema mismatch for ${status} on ${route.method} ${route.path}: ${JSON.stringify(result.issues)}`);
|
|
97
|
+
}
|
|
93
98
|
};
|
|
94
99
|
/**
|
|
95
100
|
* Verify every route under a path prefix has the exact expected middleware stack.
|
|
@@ -1,30 +1,22 @@
|
|
|
1
1
|
import './assert_dev_env.js';
|
|
2
|
-
/**
|
|
3
|
-
* Error reachability coverage tracking.
|
|
4
|
-
*
|
|
5
|
-
* Tracks which declared error statuses (and specific error codes) are
|
|
6
|
-
* actually exercised in tests. `ErrorCoverageCollector` records status
|
|
7
|
-
* codes (optionally with body `error` codes) observed during test runs,
|
|
8
|
-
* then `assert_error_coverage` compares against declared error schemas
|
|
9
|
-
* to find uncovered error paths — reporting per-code when the declared
|
|
10
|
-
* schema is a literal or enum, per-status otherwise.
|
|
11
|
-
*
|
|
12
|
-
* @module
|
|
13
|
-
*/
|
|
14
|
-
import { z } from 'zod';
|
|
15
2
|
import type { RouteSpec } from '../http/route_spec.js';
|
|
3
|
+
import type { ValidationSchema } from '../schema_issues.js';
|
|
16
4
|
/**
|
|
17
5
|
* Extract declared error code values from an error response schema.
|
|
18
6
|
*
|
|
19
7
|
* Recognizes schemas shaped like `z.object({error: z.literal(...)})` or
|
|
20
|
-
* `z.object({error: z.enum([...])})` (incl. `looseObject`/`strictObject`)
|
|
21
|
-
*
|
|
22
|
-
*
|
|
8
|
+
* `z.object({error: z.enum([...])})` (incl. `looseObject`/`strictObject`),
|
|
9
|
+
* and the `@fuzdev/fuz_schema` forms `object({error: literal(...)})` or
|
|
10
|
+
* `object({error: one_of([...])})` (any object kind, under any wrappers —
|
|
11
|
+
* `get_schema_entries`; the `error` entry itself read as given, as the Zod
|
|
12
|
+
* field is). Returns the set of declared code values, or `null` if the schema
|
|
13
|
+
* doesn't expose a literal/enum `error` field (e.g., bare `ApiError` with
|
|
14
|
+
* `z.string()`).
|
|
23
15
|
*
|
|
24
16
|
* Used by coverage reporting to split a single declared status into per-code
|
|
25
17
|
* rows when the route's error schema names specific codes.
|
|
26
18
|
*/
|
|
27
|
-
export declare const extract_declared_error_codes: (schema:
|
|
19
|
+
export declare const extract_declared_error_codes: (schema: ValidationSchema) => Array<string> | null;
|
|
28
20
|
/** Uncovered entry — either a status-level row (no `code`) or a specific-code row. */
|
|
29
21
|
export interface UncoveredEntry {
|
|
30
22
|
method: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error_coverage.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/error_coverage.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"error_coverage.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/error_coverage.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAoB7B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAGvD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAE5D;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,4BAA4B,GAAI,QAAQ,gBAAgB,KAAG,KAAK,CAAC,MAAM,CAAC,GAAG,IAoBvF,CAAC;AAEF,sFAAsF;AACtF,MAAM,WAAW,cAAc;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,wFAAwF;IACxF,IAAI,CAAC,EAAE,MAAM,CAAC;CACd;AAED,6EAA6E;AAC7E,MAAM,WAAW,qBAAqB;IACrC,kDAAkD;IAClD,aAAa,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAC9B,iCAAiC;IACjC,eAAe,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;CAChC;AAqDD;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,sBAAsB;IAClC;;;;;OAKG;IACH,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC,MAAM,CAAC,CAAa;IAE3C;;;;;;;;;;;;OAYG;IACH,MAAM,CACL,WAAW,EAAE,KAAK,CAAC,SAAS,CAAC,EAC7B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,MAAM,GACX,IAAI;IAUP;;;;;;;;;;;;;;;;OAgBG;IACG,iBAAiB,CACtB,WAAW,EAAE,KAAK,CAAC,SAAS,CAAC,EAC7B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,QAAQ,EAClB,IAAI,CAAC,EAAE,MAAM,GACX,OAAO,CAAC,IAAI,CAAC;IAgBhB;;;;;;;;OAQG;IACH,SAAS,CAAC,WAAW,EAAE,KAAK,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,KAAK,CAAC,cAAc,CAAC;CAKhG;AAED;;;;GAIG;AACH,eAAO,MAAM,kCAAkC,MAAM,CAAC;AAEtD,2CAA2C;AAC3C,MAAM,WAAW,oBAAqB,SAAQ,qBAAqB;IAClE,sEAAsE;IACtE,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB;AAaD;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,qBAAqB,GACjC,WAAW,sBAAsB,EACjC,aAAa,KAAK,CAAC,SAAS,CAAC,EAC7B,UAAU,oBAAoB,KAC5B,IAsBF,CAAC"}
|
|
@@ -13,20 +13,37 @@ import './assert_dev_env.js';
|
|
|
13
13
|
*/
|
|
14
14
|
import { z } from 'zod';
|
|
15
15
|
import { assert } from 'vitest';
|
|
16
|
+
import { is_schema } from '@fuzdev/fuz_schema/schema.ts';
|
|
17
|
+
import { get_schema_entries } from '@fuzdev/fuz_schema/introspect.ts';
|
|
16
18
|
import { merge_error_schemas } from '../http/schema_helpers.js';
|
|
17
19
|
import { find_route_spec, assert_response_matches_spec } from './integration_helpers.js';
|
|
18
20
|
/**
|
|
19
21
|
* Extract declared error code values from an error response schema.
|
|
20
22
|
*
|
|
21
23
|
* Recognizes schemas shaped like `z.object({error: z.literal(...)})` or
|
|
22
|
-
* `z.object({error: z.enum([...])})` (incl. `looseObject`/`strictObject`)
|
|
23
|
-
*
|
|
24
|
-
*
|
|
24
|
+
* `z.object({error: z.enum([...])})` (incl. `looseObject`/`strictObject`),
|
|
25
|
+
* and the `@fuzdev/fuz_schema` forms `object({error: literal(...)})` or
|
|
26
|
+
* `object({error: one_of([...])})` (any object kind, under any wrappers —
|
|
27
|
+
* `get_schema_entries`; the `error` entry itself read as given, as the Zod
|
|
28
|
+
* field is). Returns the set of declared code values, or `null` if the schema
|
|
29
|
+
* doesn't expose a literal/enum `error` field (e.g., bare `ApiError` with
|
|
30
|
+
* `z.string()`).
|
|
25
31
|
*
|
|
26
32
|
* Used by coverage reporting to split a single declared status into per-code
|
|
27
33
|
* rows when the route's error schema names specific codes.
|
|
28
34
|
*/
|
|
29
35
|
export const extract_declared_error_codes = (schema) => {
|
|
36
|
+
if (is_schema(schema)) {
|
|
37
|
+
const error_field = get_schema_entries(schema)?.error;
|
|
38
|
+
if (!error_field)
|
|
39
|
+
return null;
|
|
40
|
+
if (error_field.kind === 'literal')
|
|
41
|
+
return [String(error_field.def)];
|
|
42
|
+
if (error_field.kind === 'one_of') {
|
|
43
|
+
return error_field.def.map(String);
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
30
47
|
if (!(schema instanceof z.ZodObject))
|
|
31
48
|
return null;
|
|
32
49
|
const error_field = schema.shape.error;
|
|
@@ -111,9 +111,9 @@ export const assert_response_matches_spec = async (route_specs, method, path, re
|
|
|
111
111
|
if (merged) {
|
|
112
112
|
const status_schema = merged[response.status];
|
|
113
113
|
if (status_schema) {
|
|
114
|
-
const result = status_schema
|
|
115
|
-
if (!result.
|
|
116
|
-
throw new Error(`Error schema mismatch for ${method} ${path} (${response.status}): ${JSON.stringify(result.
|
|
114
|
+
const result = parse_any(status_schema, body);
|
|
115
|
+
if (!result.ok) {
|
|
116
|
+
throw new Error(`Error schema mismatch for ${method} ${path} (${response.status}): ${JSON.stringify(result.issues)}`);
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
}
|