@fuzdev/fuz_app 0.156.0 → 0.158.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/actions/CLAUDE.md +1 -0
- package/dist/actions/compile_action_registry.d.ts +5 -1
- package/dist/actions/compile_action_registry.d.ts.map +1 -1
- package/dist/actions/compile_action_registry.js +8 -1
- package/dist/auth/CLAUDE.md +2 -1
- package/dist/auth/actor_lookup_action_specs.d.ts +184 -40
- package/dist/auth/actor_lookup_action_specs.d.ts.map +1 -1
- package/dist/auth/actor_lookup_action_specs.js +16 -14
- package/dist/auth/actor_search_action_specs.d.ts +180 -40
- package/dist/auth/actor_search_action_specs.d.ts.map +1 -1
- package/dist/auth/actor_search_action_specs.js +12 -15
- package/dist/http/CLAUDE.md +26 -11
- 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 +12 -4
- package/dist/http/error_schemas.d.ts.map +1 -1
- package/dist/http/route_spec.d.ts +38 -18
- package/dist/http/route_spec.d.ts.map +1 -1
- package/dist/http/route_spec.js +60 -21
- package/dist/http/schema_helpers.d.ts +24 -7
- package/dist/http/schema_helpers.d.ts.map +1 -1
- package/dist/http/schema_helpers.js +34 -4
- package/dist/http/surface.d.ts +2 -0
- package/dist/http/surface.d.ts.map +1 -1
- package/dist/http/surface.js +13 -5
- package/dist/primitive_schemas.d.ts +10 -6
- package/dist/primitive_schemas.d.ts.map +1 -1
- package/dist/primitive_schemas.js +20 -6
- package/dist/primitive_schemas_fuz.d.ts +22 -9
- package/dist/primitive_schemas_fuz.d.ts.map +1 -1
- package/dist/primitive_schemas_fuz.js +25 -9
- package/dist/schema_issues.d.ts +11 -0
- package/dist/schema_issues.d.ts.map +1 -1
- package/dist/schema_issues.js +15 -0
- 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
|
@@ -5,20 +5,23 @@
|
|
|
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 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,18 @@ 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
|
+
* Every schema slot (`params`, `query`, `input`, `output`, each `errors`
|
|
305
|
+
* entry) must hold a Zod or a `@fuzdev/fuz_schema` schema
|
|
306
|
+
* (`assert_validation_schema`): a value of neither library throws naming the
|
|
307
|
+
* route and the slot. A `@fuzdev/fuz_schema` `params` or `query` must be an
|
|
308
|
+
* object schema (`get_schema_entries`), as the path params and query string
|
|
309
|
+
* are an object: one that isn't throws naming the route.
|
|
310
|
+
*
|
|
291
311
|
* @param resolve_auth_guards - maps `RouteAuth` to middleware — use `fuz_auth_guard_resolver` from `auth/auth_guard_resolver.ts`
|
|
292
312
|
* @param authorize - optional authorization phase; runs after the pre-authorization guards and before the post-authorization guards
|
|
293
313
|
* @param db - used for transaction wrapping and `RouteContext`
|
|
294
314
|
* @mutates `app`
|
|
295
|
-
* @throws Error if two specs share the same `method` + `path` (each combination must be unique),
|
|
315
|
+
* @throws Error if two specs share the same `method` + `path` (each combination must be unique), if a schema slot holds a value of neither library, 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
316
|
*/
|
|
297
317
|
export declare const apply_route_specs: (app: Hono, specs: Array<RouteSpec>, resolve_auth_guards: AuthGuardResolver, log: Logger, db: Db, authorize?: AuthorizationHandler) => void;
|
|
298
318
|
/**
|
|
@@ -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,MAAM,oBAAoB,CAAC;AAW5B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,EAGN,KAAK,SAAS,EACd,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAIN,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,MAAM,qBAAqB,CAAC;AAE7B;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,UAAU;IAC1B,iBAAiB,EAAE,KAAK,CAAC,iBAAiB,CAAC,CAAC;IAC5C,kBAAkB,EAAE,KAAK,CAAC,iBAAiB,CAAC,CAAC;CAC7C;AAED;;;;;;GAMG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,SAAS,KAAK,UAAU,CAAC;AAEhE;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,KAAK,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAC;AAE7F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AM,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 { assert_validation_schema, 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,18 @@ 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
|
+
* Every schema slot (`params`, `query`, `input`, `output`, each `errors`
|
|
360
|
+
* entry) must hold a Zod or a `@fuzdev/fuz_schema` schema
|
|
361
|
+
* (`assert_validation_schema`): a value of neither library throws naming the
|
|
362
|
+
* route and the slot. A `@fuzdev/fuz_schema` `params` or `query` must be an
|
|
363
|
+
* object schema (`get_schema_entries`), as the path params and query string
|
|
364
|
+
* are an object: one that isn't throws naming the route.
|
|
365
|
+
*
|
|
354
366
|
* @param resolve_auth_guards - maps `RouteAuth` to middleware — use `fuz_auth_guard_resolver` from `auth/auth_guard_resolver.ts`
|
|
355
367
|
* @param authorize - optional authorization phase; runs after the pre-authorization guards and before the post-authorization guards
|
|
356
368
|
* @param db - used for transaction wrapping and `RouteContext`
|
|
357
369
|
* @mutates `app`
|
|
358
|
-
* @throws Error if two specs share the same `method` + `path` (each combination must be unique),
|
|
370
|
+
* @throws Error if two specs share the same `method` + `path` (each combination must be unique), if a schema slot holds a value of neither library, 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
371
|
*/
|
|
360
372
|
export const apply_route_specs = (app, specs, resolve_auth_guards, log, db, authorize) => {
|
|
361
373
|
const registered = new Set();
|
|
@@ -365,6 +377,9 @@ export const apply_route_specs = (app, specs, resolve_auth_guards, log, db, auth
|
|
|
365
377
|
throw new Error(`Duplicate route: ${route_key} — each method+path combination must be unique`);
|
|
366
378
|
}
|
|
367
379
|
registered.add(route_key);
|
|
380
|
+
assert_schema_slots(spec, route_key);
|
|
381
|
+
assert_object_slot(spec.params, 'params', route_key);
|
|
382
|
+
assert_object_slot(spec.query, 'query', route_key);
|
|
368
383
|
assert_route_auth_invariants(spec.auth, `Route "${route_key}"`);
|
|
369
384
|
assert_route_auth_acting_biconditional(spec.auth, { input: spec.input, query: spec.query }, `Route "${route_key}"`);
|
|
370
385
|
const { pre_authorization: pre_authorization_guards, post_authorization: post_authorization_guards } = resolve_auth_guards(spec.auth);
|
|
@@ -408,6 +423,30 @@ export const apply_route_specs = (app, specs, resolve_auth_guards, log, db, auth
|
|
|
408
423
|
app.on(spec.method, [spec.path], ...content_type_check, ...params_validation, ...query_validation, ...pre_authorization_guards, ...authorization, ...post_authorization_guards, ...input_validation, handler);
|
|
409
424
|
}
|
|
410
425
|
};
|
|
426
|
+
// throws unless every schema slot holds a Zod or fuz_schema schema; `params`,
|
|
427
|
+
// `query` and an `errors` entry may be absent
|
|
428
|
+
const assert_schema_slots = (spec, route_key) => {
|
|
429
|
+
const label = `Route "${route_key}"`;
|
|
430
|
+
if (spec.params !== undefined)
|
|
431
|
+
assert_validation_schema(spec.params, `${label}: params`);
|
|
432
|
+
if (spec.query !== undefined)
|
|
433
|
+
assert_validation_schema(spec.query, `${label}: query`);
|
|
434
|
+
assert_validation_schema(spec.input, `${label}: input`);
|
|
435
|
+
assert_validation_schema(spec.output, `${label}: output`);
|
|
436
|
+
if (spec.errors) {
|
|
437
|
+
for (const [status, schema] of Object.entries(spec.errors)) {
|
|
438
|
+
if (schema !== undefined)
|
|
439
|
+
assert_validation_schema(schema, `${label}: errors.${status}`);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
// throws unless a fuz_schema `params` / `query` is an object schema under any
|
|
444
|
+
// wrappers; a Zod one is a `z.ZodObject` by its type
|
|
445
|
+
const assert_object_slot = (schema, slot, route_key) => {
|
|
446
|
+
if (schema === undefined || !is_schema(schema) || get_schema_entries(schema))
|
|
447
|
+
return;
|
|
448
|
+
throw new Error(`Route "${route_key}": ${slot} is a @fuzdev/fuz_schema '${schema.kind}' schema, not an object schema`);
|
|
449
|
+
};
|
|
411
450
|
/**
|
|
412
451
|
* Prepend a prefix to all route spec paths.
|
|
413
452
|
*
|
|
@@ -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';
|
|
@@ -174,6 +173,13 @@ export interface SchemaToSurfaceOptions {
|
|
|
174
173
|
* Convert a Zod or `@fuzdev/fuz_schema` schema to its JSON Schema for the
|
|
175
174
|
* surface, with `$schema` and `default` stripped at every depth.
|
|
176
175
|
*
|
|
176
|
+
* A value of neither library throws first (`assert_validation_schema`),
|
|
177
|
+
* naming `options.source`, so a slot that surface generation reads before
|
|
178
|
+
* any registration does — a route's, an event's `params`, a `ws_endpoints`
|
|
179
|
+
* action's — fails server assembly naming the slot, as registration refuses
|
|
180
|
+
* a route's or action's slot, not as a JSON Schema the emitter could not
|
|
181
|
+
* write.
|
|
182
|
+
*
|
|
177
183
|
* Returns `null` for a null schema (`is_null_schema`) and a void schema
|
|
178
184
|
* (`is_void_schema`), in either library: no body, no params. Any other
|
|
179
185
|
* schema is written with its library's emitter (`to_json_schema` for fuz_schema, `z.toJSONSchema`
|
|
@@ -198,7 +204,7 @@ export interface SchemaToSurfaceOptions {
|
|
|
198
204
|
* @param schema - the schema to describe, Zod or fuz_schema
|
|
199
205
|
* @param options - the side to describe, the schema's name for errors, and a listener for gaps
|
|
200
206
|
* @returns the JSON Schema, or `null` for a null or void schema
|
|
201
|
-
* @throws Error naming `options.source` when the schema can't be written, or nothing of it can
|
|
207
|
+
* @throws Error naming `options.source` when the value is of neither library, when the schema can't be written, or when nothing of it can
|
|
202
208
|
*/
|
|
203
209
|
export declare const schema_to_surface: (schema: ValidationSchema, options?: SchemaToSurfaceOptions) => unknown;
|
|
204
210
|
/**
|
|
@@ -232,11 +238,22 @@ export declare const middleware_spec_applies: (spec: {
|
|
|
232
238
|
readonly path: string;
|
|
233
239
|
}> | null;
|
|
234
240
|
}, route_path: string) => boolean;
|
|
241
|
+
/**
|
|
242
|
+
* Copy an error-schema map without its entries left `undefined`, so a
|
|
243
|
+
* status declared `undefined` reads as absent: it overrides nothing beneath
|
|
244
|
+
* it in a merge.
|
|
245
|
+
*
|
|
246
|
+
* @param errors - a route's or middleware's error schemas, keyed by status
|
|
247
|
+
* @returns the entries that hold a schema
|
|
248
|
+
*/
|
|
249
|
+
export declare const strip_undefined_error_schemas: (errors: RouteErrorSchemas) => Record<number, ValidationSchema>;
|
|
235
250
|
/**
|
|
236
251
|
* Merge auto-derived, middleware, and explicit error schemas for a route spec.
|
|
237
252
|
*
|
|
238
253
|
* Merge order: derived -> middleware -> explicit route errors.
|
|
239
|
-
* Later layers override earlier ones for the same status code
|
|
254
|
+
* Later layers override earlier ones for the same status code; an entry left
|
|
255
|
+
* `undefined` is absent (`strip_undefined_error_schemas`), so it leaves the
|
|
256
|
+
* layer beneath it in place.
|
|
240
257
|
*
|
|
241
258
|
* @param spec - the route spec (reads `method`, `auth`, `input`, `params`, `query`, `rate_limit`, `errors`)
|
|
242
259
|
* @param middleware_errors - errors contributed by middleware whose path matches the route
|
|
@@ -245,10 +262,10 @@ export declare const middleware_spec_applies: (spec: {
|
|
|
245
262
|
export declare const merge_error_schemas: (spec: {
|
|
246
263
|
method: RouteMethod;
|
|
247
264
|
auth: RouteAuth;
|
|
248
|
-
input:
|
|
249
|
-
params?:
|
|
250
|
-
query?:
|
|
265
|
+
input: ValidationSchema;
|
|
266
|
+
params?: ValidationSchema;
|
|
267
|
+
query?: ValidationSchema;
|
|
251
268
|
rate_limit?: RateLimitKey;
|
|
252
269
|
errors?: RouteErrorSchemas;
|
|
253
|
-
}, middleware_errors?: RouteErrorSchemas | null) =>
|
|
270
|
+
}, middleware_errors?: RouteErrorSchemas | null) => Record<number, ValidationSchema> | null;
|
|
254
271
|
//# sourceMappingURL=schema_helpers.d.ts.map
|
|
@@ -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,EAIN,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;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,cAAc,GAAI,QAAQ,gBAAgB,KAAG,OAEmB,CAAC;AAE9E;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,cAAc,GAAI,YAAY,OAAO,KAAG,OAUpD,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,eAAe,GAAI,OAAO,gBAAgB,EAAE,YAAY,OAAO,KAAG,OACP,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,sBAAsB,GAAI,OAAO,OAAO,KAAG,OAavD,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,eAAO,MAAM,gCAAgC,GAAI,CAAC,SAAS,gBAAgB,EAC1E,QAAQ,CAAC,EACT,OAAO,OAAO,K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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,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,GAAG,IAerC,CAAC"}
|
|
@@ -13,7 +13,7 @@ import { is_schema } from '@fuzdev/fuz_schema/schema.ts';
|
|
|
13
13
|
import { unwrap_schema } from '@fuzdev/fuz_schema/introspect.ts';
|
|
14
14
|
import { to_json_schema } from '@fuzdev/fuz_schema/json_schema.ts';
|
|
15
15
|
import { derive_error_schemas } from './error_schemas.js';
|
|
16
|
-
import { parse_any, unknown_key_targets } from '../schema_issues.js';
|
|
16
|
+
import { assert_validation_schema, parse_any, unknown_key_targets } from '../schema_issues.js';
|
|
17
17
|
/**
|
|
18
18
|
* Check if a schema is exactly `z.null()`, or a `@fuzdev/fuz_schema`
|
|
19
19
|
* `literal(null)`.
|
|
@@ -330,6 +330,13 @@ const fuz_schema_untransformed_head = (schema) => {
|
|
|
330
330
|
* Convert a Zod or `@fuzdev/fuz_schema` schema to its JSON Schema for the
|
|
331
331
|
* surface, with `$schema` and `default` stripped at every depth.
|
|
332
332
|
*
|
|
333
|
+
* A value of neither library throws first (`assert_validation_schema`),
|
|
334
|
+
* naming `options.source`, so a slot that surface generation reads before
|
|
335
|
+
* any registration does — a route's, an event's `params`, a `ws_endpoints`
|
|
336
|
+
* action's — fails server assembly naming the slot, as registration refuses
|
|
337
|
+
* a route's or action's slot, not as a JSON Schema the emitter could not
|
|
338
|
+
* write.
|
|
339
|
+
*
|
|
333
340
|
* Returns `null` for a null schema (`is_null_schema`) and a void schema
|
|
334
341
|
* (`is_void_schema`), in either library: no body, no params. Any other
|
|
335
342
|
* schema is written with its library's emitter (`to_json_schema` for fuz_schema, `z.toJSONSchema`
|
|
@@ -354,9 +361,10 @@ const fuz_schema_untransformed_head = (schema) => {
|
|
|
354
361
|
* @param schema - the schema to describe, Zod or fuz_schema
|
|
355
362
|
* @param options - the side to describe, the schema's name for errors, and a listener for gaps
|
|
356
363
|
* @returns the JSON Schema, or `null` for a null or void schema
|
|
357
|
-
* @throws Error naming `options.source` when the schema can't be written, or nothing of it can
|
|
364
|
+
* @throws Error naming `options.source` when the value is of neither library, when the schema can't be written, or when nothing of it can
|
|
358
365
|
*/
|
|
359
366
|
export const schema_to_surface = (schema, options) => {
|
|
367
|
+
assert_validation_schema(schema, options?.source ?? 'schema');
|
|
360
368
|
if (is_null_schema(schema) || is_void_schema(schema))
|
|
361
369
|
return null;
|
|
362
370
|
const io = options?.io ?? 'output';
|
|
@@ -449,11 +457,29 @@ export const middleware_paths_overlap = (a, b) => {
|
|
|
449
457
|
*/
|
|
450
458
|
export const middleware_spec_applies = (spec, route_path) => middleware_applies(spec.path, route_path) &&
|
|
451
459
|
!(spec.carve_outs ?? []).some((carve_out) => middleware_applies(carve_out.path, route_path));
|
|
460
|
+
/**
|
|
461
|
+
* Copy an error-schema map without its entries left `undefined`, so a
|
|
462
|
+
* status declared `undefined` reads as absent: it overrides nothing beneath
|
|
463
|
+
* it in a merge.
|
|
464
|
+
*
|
|
465
|
+
* @param errors - a route's or middleware's error schemas, keyed by status
|
|
466
|
+
* @returns the entries that hold a schema
|
|
467
|
+
*/
|
|
468
|
+
export const strip_undefined_error_schemas = (errors) => {
|
|
469
|
+
const defined = {};
|
|
470
|
+
for (const [status, schema] of Object.entries(errors)) {
|
|
471
|
+
if (schema !== undefined)
|
|
472
|
+
defined[Number(status)] = schema;
|
|
473
|
+
}
|
|
474
|
+
return defined;
|
|
475
|
+
};
|
|
452
476
|
/**
|
|
453
477
|
* Merge auto-derived, middleware, and explicit error schemas for a route spec.
|
|
454
478
|
*
|
|
455
479
|
* Merge order: derived -> middleware -> explicit route errors.
|
|
456
|
-
* Later layers override earlier ones for the same status code
|
|
480
|
+
* Later layers override earlier ones for the same status code; an entry left
|
|
481
|
+
* `undefined` is absent (`strip_undefined_error_schemas`), so it leaves the
|
|
482
|
+
* layer beneath it in place.
|
|
457
483
|
*
|
|
458
484
|
* @param spec - the route spec (reads `method`, `auth`, `input`, `params`, `query`, `rate_limit`, `errors`)
|
|
459
485
|
* @param middleware_errors - errors contributed by middleware whose path matches the route
|
|
@@ -468,6 +494,10 @@ export const merge_error_schemas = (spec, middleware_errors) => {
|
|
|
468
494
|
rate_limit: spec.rate_limit,
|
|
469
495
|
checks_content_type: spec.method === 'POST'
|
|
470
496
|
});
|
|
471
|
-
const merged = {
|
|
497
|
+
const merged = {
|
|
498
|
+
...derived,
|
|
499
|
+
...(middleware_errors && strip_undefined_error_schemas(middleware_errors)),
|
|
500
|
+
...(spec.errors && strip_undefined_error_schemas(spec.errors))
|
|
501
|
+
};
|
|
472
502
|
return Object.keys(merged).length > 0 ? merged : null;
|
|
473
503
|
};
|
package/dist/http/surface.d.ts
CHANGED
|
@@ -194,6 +194,8 @@ export interface GenerateAppSurfaceOptions {
|
|
|
194
194
|
}
|
|
195
195
|
/**
|
|
196
196
|
* Collect error schemas from all middleware that applies to a route path.
|
|
197
|
+
* An entry left `undefined` is absent (`strip_undefined_error_schemas`),
|
|
198
|
+
* so it leaves an earlier layer's schema for that status in place.
|
|
197
199
|
*
|
|
198
200
|
* @returns merged middleware error schemas, or `null` if none
|
|
199
201
|
*/
|
|
@@ -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;A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AM,uBAAuB,GAAI,SAAS,yBAAyB,KAAG,cAS5E,CAAC"}
|
package/dist/http/surface.js
CHANGED
|
@@ -7,13 +7,14 @@
|
|
|
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 {
|
|
12
|
-
import { schema_to_surface, middleware_spec_applies, merge_error_schemas, is_null_schema, is_strict_object_schema } from './schema_helpers.js';
|
|
10
|
+
import { is_schema, parse } from '@fuzdev/fuz_schema/schema.ts';
|
|
11
|
+
import { schema_to_surface, middleware_spec_applies, merge_error_schemas, strip_undefined_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 ---
|
|
15
14
|
/**
|
|
16
15
|
* Collect error schemas from all middleware that applies to a route path.
|
|
16
|
+
* An entry left `undefined` is absent (`strip_undefined_error_schemas`),
|
|
17
|
+
* so it leaves an earlier layer's schema for that status in place.
|
|
17
18
|
*
|
|
18
19
|
* @returns merged middleware error schemas, or `null` if none
|
|
19
20
|
*/
|
|
@@ -21,7 +22,7 @@ export const collect_middleware_errors = (middleware, route_path) => {
|
|
|
21
22
|
const errors = {};
|
|
22
23
|
for (const mw of middleware) {
|
|
23
24
|
if (mw.errors && middleware_spec_applies(mw, route_path)) {
|
|
24
|
-
Object.assign(errors, mw.errors);
|
|
25
|
+
Object.assign(errors, strip_undefined_error_schemas(mw.errors));
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
return Object.keys(errors).length > 0 ? errors : null;
|
|
@@ -83,6 +84,9 @@ const create_to_surface = (diagnostics) => (schema, io, source) => schema_to_sur
|
|
|
83
84
|
})
|
|
84
85
|
: undefined
|
|
85
86
|
});
|
|
87
|
+
// a fuz_schema input that is not a strict `object`: a `strip_object` drops
|
|
88
|
+
// unknown keys and a `loose_object` keeps them, so the text names neither
|
|
89
|
+
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
90
|
/**
|
|
87
91
|
* Generate a JSON-serializable attack surface from middleware, route specs,
|
|
88
92
|
* and optional env/event metadata.
|
|
@@ -97,7 +101,9 @@ export const generate_app_surface = (options) => {
|
|
|
97
101
|
diagnostics.push({
|
|
98
102
|
level: 'warning',
|
|
99
103
|
category: 'schema',
|
|
100
|
-
message:
|
|
104
|
+
message: is_schema(r.input)
|
|
105
|
+
? FUZ_SCHEMA_NOT_STRICT_INPUT_MESSAGE
|
|
106
|
+
: 'Input schema is not z.strictObject() — unknown keys will be silently stripped',
|
|
101
107
|
source: `${r.method} ${r.path} input`
|
|
102
108
|
});
|
|
103
109
|
}
|
|
@@ -109,6 +115,8 @@ export const generate_app_surface = (options) => {
|
|
|
109
115
|
if (m.errors) {
|
|
110
116
|
const schemas = {};
|
|
111
117
|
for (const [status, schema] of Object.entries(m.errors)) {
|
|
118
|
+
if (schema === undefined)
|
|
119
|
+
continue;
|
|
112
120
|
const json_schema = to_surface(schema, 'output', `middleware ${m.name} errors.${status}`);
|
|
113
121
|
if (json_schema !== null) {
|
|
114
122
|
schemas[status] = json_schema;
|
|
@@ -27,7 +27,7 @@ export interface BoundedStringOptions {
|
|
|
27
27
|
min?: number;
|
|
28
28
|
/** Longest string admitted. */
|
|
29
29
|
max: number;
|
|
30
|
-
/** Message for the over-`max` issue, in place of
|
|
30
|
+
/** Message for the over-`max` issue, in place of the schema library's default. */
|
|
31
31
|
max_message?: string;
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
@@ -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,
|
|
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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