http/schema_helpers.ts

Shared pure helpers for schema introspection and middleware matching.

Used by both http/route_spec.ts (input validation) and http/surface.ts (attack surface generation). Extracted to avoid circular dependencies between routes and middleware.

view source

Declarations
#

17 declarations

is_null_schema
#

http/schema_helpers.ts view source

(schema: AnySchema): boolean import {is_null_schema} from '@fuzdev/fuz_app/http/schema_helpers.js';

Check if a schema is exactly literal(null).

Reads the schema's kind rather than parsing null, to avoid false positives from nullable(string) or similar schemas that accept null but also accept other values.

schema

type AnySchema

returns

boolean

is_strict_object_schema
#

http/schema_helpers.ts view source

(schema: AnySchema): boolean import {is_strict_object_schema} from '@fuzdev/fuz_app/http/schema_helpers.js';

Check if a schema is a strict object: an object (which refuses unknown keys, where strip_object drops them and loose_object keeps them).

A refined strict object reads strict: a pipe is read through to its first schema when no stage of it is a transform (message, flavor, brand and meta keep the kind they wrap), so pipe(O, O) reads strict through to O. A transformed object is not a strict object.

schema

type AnySchema

returns

boolean

is_void_schema
#

http/schema_helpers.ts view source

(schema: AnySchema): boolean import {is_void_schema} from '@fuzdev/fuz_app/http/schema_helpers.js';

Check if a schema is exactly literal(undefined) (what s.void is, meta copies included).

RPC action specs use a void schema to declare a parameterless method — JSON-RPC 2.0 forbids params: null (params must be omitted or be a Structured value), so void is the correct schema for "no params" and the dispatcher maps absent params to undefined for these specs. Like is_null_schema, it reads the schema's kind rather than parsing undefined, so an optional schema is not void.

schema

type AnySchema

returns

boolean

JsonValueScan
#

middleware_applies
#

http/schema_helpers.ts view source

(mw_path: string, route_path: string): boolean import {middleware_applies} from '@fuzdev/fuz_app/http/schema_helpers.js';

Check if a middleware path pattern applies to a route path.

Supports Hono-style patterns:

  • /api/* matches /api/anything
  • /api/zap/* matches /api/zap/runs but not /api/account/login
  • Exact match: /health matches /health

mw_path

type string

route_path

type string

returns

boolean

middleware_carves_out
#

http/schema_helpers.ts view source

(spec: { readonly carve_outs?: readonly { readonly path: string; }[] | null | undefined; }, route_path: string): boolean import {middleware_carves_out} from '@fuzdev/fuz_app/http/schema_helpers.js';

Check if a middleware layer carves a route path out: one of its carve_outs applies to it (middleware_applies), whether or not its own path does.

spec

the layer's optional carve_outs

type { readonly carve_outs?: readonly { readonly path: string; }[] | null | undefined; }

route_path

the route path to check

type string

returns

boolean

middleware_paths_overlap
#

http/schema_helpers.ts view source

(a: string, b: string): boolean import {middleware_paths_overlap} from '@fuzdev/fuz_app/http/schema_helpers.js';

Check if two middleware path patterns can match a common route path — the pattern-against-pattern form of middleware_applies, over the same grammar (*, an exact path, or a /prefix/* that also matches /prefix).

a

one Hono-style pattern

type string

b

the other

type string

returns

boolean

middleware_spec_applies
#

http/schema_helpers.ts view source

(spec: { readonly path: string; readonly carve_outs?: readonly { readonly path: string; }[] | null | undefined; }, route_path: string): boolean import {middleware_spec_applies} from '@fuzdev/fuz_app/http/schema_helpers.js';

Check if a middleware layer runs on a route path: its path pattern applies (middleware_applies) and none of its carve_outs does (middleware_carves_out).

spec

the layer's path and optional carve_outs

type { readonly path: string; readonly carve_outs?: readonly { readonly path: string; }[] | null | undefined; }

route_path

the route path to check

type string

returns

boolean

safe_parse_dropping_unknown_keys
#

http/schema_helpers.ts view source

<T extends AnySchema>(schema: T, value: unknown): SchemaResult<Infer<T>> import {safe_parse_dropping_unknown_keys} from '@fuzdev/fuz_app/http/schema_helpers.js';

parse(schema, value), except that keys the schema doesn't declare are dropped rather than refused — the forward-compatible read of a payload a remote authored.

Output schemas are strict so the server's DEV output validation catches a handler leaking fields it shouldn't. The receiving side, though, is often older than the remote it talks to — a deploy lands before open tabs reload — and a remote that adds a field is making a compatible change. So when every issue is an unknown key (unknown_key_targets), those keys are stripped from a copy of value and it's parsed again, until a parse succeeds or reports anything else; any other issue (a wrong type, a missing field, null where a value is required) still fails. value itself is never mutated. This matches serde's default on the Rust spine, which ignores unknown fields.

A parse reports at most a hundred issues, so a pass that only deleted the reported keys would take a parse per hundred unknown keys, quadratic in a payload of many. So after the first parse that reports unknown keys, one schema-guided pass deletes every key a strict object doesn't name anywhere the schema reaches — object entries, array items, record values, the variant option the discriminator picks — and the next parse usually succeeds. A key under a union is never stripped: which option will match isn't known, and the union fails as one issue of its own. A later pipe stage is stripped only as reported, as the pass reads only a pipe's first schema, so keys a later stage refuses that the first names still go a hundred a parse. A pass that finds nothing to strip ends it, and the passes are capped at UNKNOWN_KEY_PASSES_MAX, past which the last failure is returned, so the work stays linear in the payload whatever its shape.

A union reports one issue of its own, so a key inside one is not stripped and the parse fails; a variant reports its matched option's issues, so those are.

Used for a response's result (ActionEvent) and for a peer's reply to a server-initiated request (peer/ping).

schema

the declared shape

type T

value

the remote-authored payload

type unknown

returns

SchemaResult

the parse result, with undeclared keys dropped on success

generics

safe_parse_dropping_unknown_keys<T extends AnySchema>
T
constraint AnySchema

scan_json_value
#

http/schema_helpers.ts view source

(value: unknown): JsonValueScan import {scan_json_value} from '@fuzdev/fuz_app/http/schema_helpers.js';

Walks a parsed JSON value once for the two things a request may not store, at any depth: an own __proto__ key, and U+0000 in a string or a key.

JSON.parse defines "__proto__" as an ordinary own key, but the first plain assignment that copies it (target[key] = value, as any shallow copy or merge loop does) hits the prototype setter instead, so stored JSON carrying one is a prototype-pollution sink for whoever reads it back. An own constructor or prototype key is inert on a plain object and is not checked.

U+0000 is valid JSON ("\u0000"), but Postgres refuses it in text and in jsonb, so a value holding one fails its write with a 500. The dispatcher refuses either in params (invalid_params, the proto key first), and a route refuses a NUL in its JSON body (invalid_request_body), both after the authority gates as any input refusal is.

Walks with an explicit stack rather than recursion: JSON.parse accepts nesting deep enough to overflow the call stack, and the walk is linear in the value's nodes, which the request body limit bounds. It reads the parsed value, not the text, so of two duplicate keys only the kept one is walked — as on the Rust spine, whose serde_json::Value keeps the last too. The twin is fuz_http::scan_json_value.

value

a value as JSON.parse produced it

type unknown

returns

JsonValueScan

what the walk found

schema_to_surface
#

http/schema_helpers.ts view source

(schema: AnySchema, options?: SchemaToSurfaceOptions | undefined): unknown import {schema_to_surface} from '@fuzdev/fuz_app/http/schema_helpers.js';

Convert a @fuzdev/fuz_schema schema to its JSON Schema for the surface (to_json_schema), with $schema and default stripped at every depth.

A value that is no schema throws first (assert_validation_schema), naming options.source, so a slot that surface generation reads before any registration does — a route's, an event's params, a ws_endpoints action's — fails server assembly naming the slot, as registration refuses a route's or action's slot, not as a JSON Schema the emitter could not write.

Returns null for a null schema (is_null_schema) and a void schema (is_void_schema): no body, no params. Any other schema is written on the side options.io names. A part JSON Schema can't say (a transform on the output side, a check) is left out or written as {} (a pipe(string, check(…)) writes as {type: 'string'}), and options.on_unrepresentable hears the emitter's message, so the surface describes what it can and records the gap.

It never answers null for a schema it failed to write, as that would read as "takes no input" and drop the schema out of every suite reading the surface: a schema with no JSON Schema form at all ({} where the schema isn't unknown), and one the emitter refuses outright (an entry named like a member of Object.prototype, metadata that isn't JSON), throw. So an output whose root is a transform (pipe(object(…), transform(…))) throws, and since generate_app_surface runs at server assembly, a route or action declaring one fails the server at boot.

schema

the schema to describe

type AnySchema

options?

the side to describe, the schema's name for errors, and a listener for gaps

optional

returns

unknown

the JSON Schema, or null for a null or void schema

throws

  • Error - naming `options.source` when the value is no schema, when the schema can't be written, or when nothing of it can

SchemaFieldBaseType
#

http/schema_helpers.ts view source

SchemaFieldBaseType

type "string" | "number" | "bigint" | "boolean" | "object" | "one_of" | "nullable" | "unknown" | "pipe" | "integer" | "min_value" | "max_value" | "literal" | "optional" | "with_default" | "with_default_fn" | "lazy" | "record" | "array" | "union" | "variant" | "min_length" | "max_length" | "safe_integer" | "regex" | "uuid" | "iso_datetime" | "to_lower_case" | "check" | "transform" | "null" | "void"

import type {SchemaFieldBaseType} from '@fuzdev/fuz_app/http/schema_helpers.js';

The kind the testing generators switch on: the innermost layer's @fuzdev/fuz_schema kind, with an object schema of any strictness as 'object', and a literal(null) and a literal(undefined) (s.void) as 'null' and 'void'.

SchemaFieldInfo
#

http/schema_helpers.ts view source

SchemaFieldInfo import type {SchemaFieldInfo} from '@fuzdev/fuz_app/http/schema_helpers.js';

What the testing generators read of one field of an object schema (to_object_schema_view, testing/schema_generators.ts).

name

The field's key.

type string

base_type

The innermost layer's kind, SchemaFieldBaseType.

type SchemaFieldBaseType

required

Whether the key must be present: no optional layer.

type boolean

has_default

Whether the parse fills the key in when it is missing.

type boolean

nullable

Whether a wrapping layer accepts null.

type boolean

SchemaToSurfaceOptions
#

http/schema_helpers.ts view source

SchemaToSurfaceOptions import type {SchemaToSurfaceOptions} from '@fuzdev/fuz_app/http/schema_helpers.js';

Options of schema_to_surface.

io?

Which side of a parse to describe: 'input', what it accepts (a request body, params, query), or 'output', what it returns (a response, an error body, a pushed event).

type "input" | "output"

default 'output'

source?

Names the schema in a thrown error, e.g. POST /api/thing input.

type string

on_unrepresentable?

Called with the emitter's message when part of the schema has no JSON Schema form and is left out or written as {} there.

type (message: string): void

message

type string
returns void

to_input_params
#

http/schema_helpers.ts view source

(input: AnySchema, raw_params: unknown): unknown import {to_input_params} from '@fuzdev/fuz_app/http/schema_helpers.js';

Normalize the raw params of an RPC call for input to validate: for a void input (is_void_schema), to_void_params; for any other input, an absent or null params reads as {}, so an all-optional object input accepts a call that omits them. Every receiving end — the server dispatch (perform_action) and a peer receiving a request or notification (ActionDispatcher) — reads params through this, so a peer is never stricter than the server.

input

the action's input schema

type AnySchema

raw_params

the call's params as parsed off the wire

type unknown

returns

unknown

the value the input parse (parse) reads

to_void_params
#

http/schema_helpers.ts view source

(raw_params: unknown): unknown import {to_void_params} from '@fuzdev/fuz_app/http/schema_helpers.js';

Normalize the raw params of a void input (is_void_schema): an absent params, an empty object, and the null a GET ?params=null parses to are all the no-arg call (undefined); anything else passes through for the void schema to refuse.

JSON-RPC 2.0 lets a parameterless call omit params, and an empty by-name structure carries no parameters, so refusing {} would only break clients that send it for a parameterless call. Arrays — [] included — are not normalized: RPC params are by-name only. The Rust spine's require_void_params accepts the same shapes. Receiving ends reach it through to_input_params.

raw_params

the request's params as parsed off the wire

type unknown

returns

unknown

undefined for an empty shape, else raw_params unchanged

UNKNOWN_KEY_PASSES_MAX
#

Depends on
#

Imported by
#