testing/schema_generators.ts

Schema-driven value generation helpers for testing.

Walks @fuzdev/fuz_schema schemas to generate valid values for route params, request bodies, and URL paths. Used by adversarial input, adversarial 404, and round-trip validation test suites. A value that is no schema throws, and so does an input no body can be generated for, rather than yielding no body.

view source

Declarations
#

15 declarations

detect_format
#

testing/schema_generators.ts view source

(field_schema: AnySchema): string | null import {detect_format} from '@fuzdev/fuz_app/testing/schema_generators.js';

Detect format constraints on a field from its input-side JSON Schema (to_field_json_schema). Returns the format string (e.g. 'uuid', 'email'), 'pattern' for a regex, or null.

field_schema

type AnySchema

returns

string | null

detect_json_schema_format
#

testing/schema_generators.ts view source

(json: Record<string, unknown>): string | null import {detect_json_schema_format} from '@fuzdev/fuz_app/testing/schema_generators.js';

The format constraint a field's input-side JSON Schema carries: its format (e.g. 'uuid', 'email'), 'pattern' for a regex, or null. A union (anyOf of several non-null branches) answers its first branch carrying one — a value violating every branch is what the caller needs, and the generators check the field refuses the candidate.

json

a field's JSON Schema, as to_field_json_schema returns it

type Record<string, unknown>

returns

string | null

the format, 'pattern', or null

generate_valid_body
#

testing/schema_generators.ts view source

(input_schema: AnySchema): Record<string, unknown> | undefined import {generate_valid_body} from '@fuzdev/fuz_app/testing/schema_generators.js';

Generate a valid request body for a route's or action's input schema.

input_schema

the input schema

type AnySchema

returns

Record<string, unknown> | undefined

a generated body that passes the schema, or undefined for a void or null input, which takes no body

throws

  • Error - if the input is no schema, or is neither void, null, nor an object schema, or if the generated body fails the input — catching broken `generate_valid_value` logic early with the parse's issues instead of a confusing 400 in downstream tests

generate_valid_object
#

testing/schema_generators.ts view source

(object_schema: AnySchema, schema: AnySchema): GeneratedObject import {generate_valid_object} from '@fuzdev/fuz_app/testing/schema_generators.js';

Generate a valid object for an object schema: its required and defaulted fields first; when schema still refuses it, each optional field alone on top of those, in declaration order, taking the first that parses; and failing that, the optional fields added one at a time, in declaration order, until it parses. The fill covers a refine over optional fields (at least one of, exactly one of, both of), which the required-only object can't satisfy, and the single-field pass keeps an unrelated optional field declared ahead of the refined ones (an acting, a note) out of the object.

Shared by the top-level body generators and generate_valid_value's nested-object case, so a refined object validates the same way at any depth.

object_schema

the object whose fields are generated: an object schema, possibly wrapped

type AnySchema

schema

the schema the object must satisfy, refinements included (object_schema or a schema wrapping it)

type AnySchema

returns

GeneratedObject

the generated object and its last parse result — the caller decides whether a failure throws

throws

  • Error - when `object_schema` is no object schema, or no schema

generate_valid_object_with_field
#

testing/schema_generators.ts view source

(object_schema: AnySchema, schema: AnySchema, base: Record<string, unknown>, field: SchemaFieldInfo): Record<string, unknown> | null import {generate_valid_object_with_field} from '@fuzdev/fuz_app/testing/schema_generators.js';

A valid object that carries field, built from base, a valid object of object_schema — the body a case breaking field changes, so the case breaks that field and nothing else. base itself when it carries the field. Otherwise the field takes a generated value and joins base, with the optional fields base omits filled as generate_valid_object fills them (a pair rule: adding scope_kind needs scope_id); failing that, it replaces base's optional fields, refilled the same way around it (an exactly-one-of rule: adding slug means dropping id).

object_schema

the object schema base is an instance of, possibly wrapped

type AnySchema

schema

the schema the object must satisfy, refinements included

type AnySchema

base

a valid object, the generated or seeded base

type Record<string, unknown>

field

the field the object must carry

returns

Record<string, unknown> | null

the object, or null when no object the generator can build carries the field and passes schema

throws

  • Error - when `object_schema` is no object schema, or no schema

generate_valid_string_param
#

testing/schema_generators.ts view source

(field: SchemaFieldInfo, field_schema: AnySchema): string | null import {generate_valid_string_param} from '@fuzdev/fuz_app/testing/schema_generators.js';

A string a URL-sourced field accepts — a route param or a query value, which arrive as strings whatever the schema parses them into. The nil UUID for a UUID field; else the first the field accepts of test_value, the generated valid value written as a string, 1, 0, true and false, the field's JSON Schema default or lower bound written as a string, each min_value and max_value bound anywhere in the schema written as an integer (a string parsed into an integer whose range excludes the small candidates, to_numeric_bounds), and a string synthesized from the field's constraints.

field

the field's info, from to_object_schema_view

field_schema

the field's schema

type AnySchema

returns

string | null

the string, or null when no candidate passes

generate_valid_value
#

testing/schema_generators.ts view source

(field: SchemaFieldInfo, field_schema: AnySchema): unknown import {generate_valid_value} from '@fuzdev/fuz_app/testing/schema_generators.js';

Generate a valid-ish value for a field based on its base type.

field

the field's info, from to_object_schema_view

field_schema

the field's schema

type AnySchema

returns

unknown

the value, checked by the caller against the whole schema

GeneratedObject
#

testing/schema_generators.ts view source

GeneratedObject import type {GeneratedObject} from '@fuzdev/fuz_app/testing/schema_generators.js';

What generate_valid_object returns: the object and its last parse.

value

type Record<string, unknown>

result

The last parse of value against the schema — the caller decides whether a failure throws.

type SchemaResult

ObjectSchemaView
#

testing/schema_generators.ts view source

ObjectSchemaView import type {ObjectSchemaView} from '@fuzdev/fuz_app/testing/schema_generators.js';

An object schema's fields and the schema of each.

fields

The fields, in declaration order.

type SchemaFieldInfo[]

field_schema

The schema of the field named name, or undefined when there is none.

type (name: string): AnySchema | undefined

name

type string
returns AnySchema | undefined

resolve_valid_path
#

testing/schema_generators.ts view source

(path: string, params_schema?: AnySchema | undefined, param_values?: Readonly<Record<string, string>> | undefined): string import {resolve_valid_path} from '@fuzdev/fuz_app/testing/schema_generators.js';

Resolve a route path with valid-ish param values so params validation passes. Used when testing input on routes that also have params.

The template is read as Hono reads it (split_route_template, parse_route_param): only a whole param segment — :name or :name{re}, optional or not — is filled, so a : inside a literal segment (/v1/files:upload) stays, and a {re} goes with its param. An optional param is filled too.

A value in param_values is used first, percent-encoded. Otherwise, without a params schema (or for a param the schema doesn't name) each param becomes test_<name> — fine for an unconstrained segment. With one, test_<name> is still preferred wherever the field schema accepts it, because the param name in the URL is what makes a failure message readable. Only a segment that refuses it falls through: a uuid param takes the nil UUID (the fast path — by far the most common shape), and anything else is synthesized by generate_valid_string, so a format-constrained segment (a blake3:… fact hash, a tok_… id) resolves to a value its schema accepts instead of a test_<name> that 400s before the route's real gates run. The synthesized value is percent-encoded, and a value containing / is refused (it would silently add a path segment and route elsewhere) — both fall back to test_<name>.

A {re} param's value, supplied or not, must match its pattern, grouped and anchored as Hono's RegExpRouter reads it — the stricter of its routers' readings, as TrieRouter leaves a top-level alternation ungrouped (compile_route_param_pattern, over the value as Hono decodes it, decode_route_path) — or the request would reach no route; no value is guessed from the pattern. A supplied value the URL would normalize away ('', ., ..) is refused by name.

path

the route's path template

type string

params_schema?

the route's params schema, an object schema

type AnySchema
optional

param_values?

the route's supplied param values by name, its entry of RouteParamValues

type Readonly<Record<string, string>>
optional

returns

string

throws

  • Error - when `params_schema` is no object schema, or no schema; when `param_values` names no param of the path; when a supplied value is `''`, `.` or `..`, or holds a lone surrogate, which can't be encoded; and when a `{re}` param's value is one its pattern refuses, naming the route and the param

RouteParamValues
#

testing/schema_generators.ts view source

Readonly<Record<string, Readonly<Record<string, string>>>>

type Readonly<Record<string, Readonly<Record<string, string>>>>

import type {RouteParamValues} from '@fuzdev/fuz_app/testing/schema_generators.js';

Caller-supplied path param values, keyed by route key (format_route_key, 'METHOD /path') and then by param name — what the suites that resolve a route's path (resolve_valid_path) fill a param with before generating one. A route needs an entry when a param of it is a :name{re} whose pattern refuses every value the generator reaches, as a {.+\.git} refuses test_<name>. Each value is the param as the handler reads it, unencoded. Values are always percent-encoded, so a {re} that needs a literal / can't be met by a supplied value: its / arrives as %2F.

[key: string]

type Readonly<Record<string, string>>

readonly

to_body_input
#

testing/schema_generators.ts view source

(input_schema: AnySchema, label: string): ObjectSchemaView | null import {to_body_input} from '@fuzdev/fuz_app/testing/schema_generators.js';

The input a body is generated for: null for a void or null input (is_void_schema, is_null_schema), which takes no body, else its object view (to_object_schema_view).

input_schema

a route's or action's input schema

type AnySchema

label

names the input in the error

type string

returns

ObjectSchemaView | null

throws

  • Error - naming `label` when the input is no schema, or is neither void, null, nor an object schema — no body can be generated for it, and no body is not the same as one the generator can't build

to_field_json_schema
#

testing/schema_generators.ts view source

(field_schema: AnySchema): Record<string, unknown> import {to_field_json_schema} from '@fuzdev/fuz_app/testing/schema_generators.js';

The JSON Schema of a field's input side — where its format, pattern and bounds live.

Rendered with io: 'input', so a field carrying a transform in a pipe (Username) still yields the constraints its input is checked against; the default output side throws on a transform. A pipe yields its input side only, so constraints of the stage after it are not here. A nullable field renders as anyOf: [{…}, {type: 'null'}], or a bare type as type: [type, 'null']; its one non-null branch or type is returned, so the bounds, pattern and type read the same as on the bare field. A union of two or more non-null branches returns the whole schema — its branches' bounds are no single field's bounds.

It renders with unrepresentable: 'any': a part JSON Schema can't say (a check or a bigint) is left out or {}, and the bounds around it still read, so a pipe(string, max_length(n), check(…)) keeps its maxLength — every case the generators build from it is checked against the field itself before it's used. What the emitter refuses even then (an entry named like a member of Object.prototype, metadata that isn't JSON) throws: a mistake in the schema, not a gap in JSON Schema.

A check is never here: JSON Schema can't express one, so a bound a check enforces (a code-point length, a cross-field rule) generates no boundary case.

field_schema

the field to read

type AnySchema

returns

Record<string, unknown>

the field's input-side JSON Schema

throws

  • Error - when the emitter refuses `field_schema` with `unrepresentable: 'any'`

to_object_schema_view
#

testing/schema_generators.ts view source

(schema: AnySchema, label: string): ObjectSchemaView import {to_object_schema_view} from '@fuzdev/fuz_app/testing/schema_generators.js';

The fields of an object schema and the schema of each, through its wrappers: a schema whose innermost layer is an object schema (get_schema_entries, get_schema_fields).

schema

an object schema, possibly wrapped

type AnySchema

label

names the schema in the error

type string

returns

ObjectSchemaView

throws

  • Error - naming `label` when `schema` is no object schema, or no schema

to_route_content_type
#

testing/schema_generators.ts view source

(route: { raw_body?: boolean | undefined; }): string import {to_route_content_type} from '@fuzdev/fuz_app/testing/schema_generators.js';

The Content-Type a well-formed request to a route sends: application/json, or application/octet-stream for a raw_body route. Every POST route refuses anything else with 415 before its other gates run (see http/simple_request.ts), so a probe of those gates must send it, body or not. Other methods don't check it, so sending it there is harmless.

route

the route spec or surface route (reads raw_body)

type { raw_body?: boolean | undefined; }

returns

string

Depends on
#

Imported by
#