primitive_schemas.ts

Reusable validator-schema primitives, in @fuzdev/fuz_schema — bounded_string, Username with UsernameCanonical and UsernameProvided, Email, the branded Uuid with its factory, Timestamp, the hash schemas FactHashSchema and Blake3Hash, and ActingActor. Lives at the top level (not inside auth/ or http/) because these shapes don't depend on or imply any domain — accounts hold a username and email, but invites, password resets, cells, and other surfaces all reach for the same primitives without going through auth. A URL is Url from @fuzdev/fuz_util/url.ts.

Client-safe: nothing here imports @fuzdev/blake3-wasm, so wire schemas that validate a hash don't pull the WASM binary into a bundle.

view source

Declarations
#

26 declarations

ACTING_ACTOR_DESCRIPTION
#

primitive_schemas.ts view source

"Actor on the authenticated account that this request acts as. Omit on single-actor accounts; required on multi-actor." import {ACTING_ACTOR_DESCRIPTION} from '@fuzdev/fuz_app/primitive_schemas.js';

The description of ActingActor.

ActingActor
#

primitive_schemas.ts view source

value + type

Branded<string, "Uuid"> | undefined

type Branded | undefined

import {ActingActor} from '@fuzdev/fuz_app/primitive_schemas.js';

acting field shared by every input that needs the caller's acting actor. Declaring acting: ActingActor on a route or action input signals to the dispatcher's authorization phase to resolve an actor against the authenticated account: it runs resolve_acting_actor, builds the actor-bound RequestContext, and loads role_grants before auth guards fire.

Resolution rules: omitted + 1 actor → use it; omitted + multiple actors → actor_required with the available list; supplied + on the account → use it; supplied + foreign actor → actor_not_on_account.

Account-grain routes — input doesn't declare acting and auth doesn't require role_grants — skip resolution entirely; their RequestContext.actor is null and the audit envelope's actor_id stays null.

Paired with RouteAuth (http/auth_shape.ts) by registry-time invariant 2: auth.actor !== 'none' ⟺ input (or query, on REST GETs) declares acting?: ActingActor. The dispatcher finds it by reference (input_schema_declares_acting), so an input declares this export, not a copy of it.

BLAKE3_HASH_MESSAGE
#

BLAKE3_HASH_REGEX
#

primitive_schemas.ts view source

RegExp import {BLAKE3_HASH_REGEX} from '@fuzdev/fuz_app/primitive_schemas.js';

A BLAKE3 hex hash's grammar: 64 lowercase hex characters (256-bit output).

Blake3Hash
#

bounded_string
#

primitive_schemas.ts view source

(options: BoundedStringOptions): PipeSchema<string, Schema<string, string, "string", undefined>> import {bounded_string} from '@fuzdev/fuz_app/primitive_schemas.js';

A string schema whose length is bounded in UTF-16 code units — what String.length measures, and what the Rust spine counts (encode_utf16().count()), so an astral-plane character costs two on both. fuz_schema's min_length / max_length read String.length. Every length bound on a wire string in the package is built here. Its JSON Schema carries minLength / maxLength; max_message replaces fuz_schema's message on the over-max issue.

options

the bounds, in UTF-16 code units, and the max message

returns

PipeSchema

the bounded string schema

BoundedStringOptions
#

primitive_schemas.ts view source

BoundedStringOptions import type {BoundedStringOptions} from '@fuzdev/fuz_app/primitive_schemas.js';

Length bounds for bounded_string, in UTF-16 code units.

min?

Shortest string admitted. Omit for no lower bound.

type number

max

Longest string admitted.

type number

max_message?

Message for the over-max issue, in place of the schema library's default.

type string

create_uuid
#

primitive_schemas.ts view source

(): Branded<string, "Uuid"> import {create_uuid} from '@fuzdev/fuz_app/primitive_schemas.js';

Returns a new random (v4) Uuid.

returns

Branded

Email
#

primitive_schemas.ts view source

value + type

string import {Email} from '@fuzdev/fuz_app/primitive_schemas.js';

Email validation. Every current consumer pairs it with Username (signup, invites, audit log), so the two live together with the other primitives.

Deliberately permissive — a structural shape check (EMAIL_REGEX plus the EMAIL_LENGTH_MAX byte bound), not RFC 5322 conformance or deliverability (real delivery is proven by a confirmation email). The rule is one explicit regex the Rust spine's is_valid_email mirrors, looser than grammars requiring a 2+ char TLD and no consecutive dots, which reject addresses like a@b.c. The length bound is the UTF-8 byte count (RFC 5321 measures octets), so a multibyte address is bounded identically to the Rust spine's s.len(). No transform: case is preserved and surrounding whitespace is rejected (not trimmed), so storage is verbatim and the case-insensitive lookup rides the DB-side LOWER(email).

The max_length ahead of the grammar refuses a string past the bound in UTF-16 code units, which is past it in bytes too, before the regex reads it, and the check after it the multibyte address within the units but past the bytes, both with EMAIL_LENGTH_MESSAGE. Its JSON Schema carries the bound as maxLength.

EMAIL_LENGTH_MAX
#

primitive_schemas.ts view source

254 import {EMAIL_LENGTH_MAX} from '@fuzdev/fuz_app/primitive_schemas.js';

Maximum email length in bytes — RFC 5321 §4.5.3.1.3 path-length limit (the limit is octets). Email bounds the UTF-8 byte length, matching the Rust spine's s.len() check; for an all-ASCII address bytes == characters.

EMAIL_LENGTH_MESSAGE
#

EMAIL_REGEX
#

primitive_schemas.ts view source

RegExp import {EMAIL_REGEX} from '@fuzdev/fuz_app/primitive_schemas.js';

Loose email shape local@domain.tld: a non-empty local part, exactly one @, a domain that holds a . and neither starts nor ends with one, and no whitespace. Accepts a@b.c (single-char TLDs are fine), a@b..c and user+tag@a.co; rejects notanemail, @x.com, user@, user@host, a@.b.c, a@b.c., and any whitespace. The Rust spine's is_valid_email is the same rule.

The negated class excludes \s plus U+0085 (NEL): JS's \s omits U+0085 but Unicode White_Space (Rust's char::is_whitespace) includes it, so excluding it keeps the whitespace set identical across the twins. (The Rust side correspondingly also rejects U+FEFF, which JS \s treats as whitespace but Unicode White_Space does not — so both spines reject exactly White_Space ∪ {U+FEFF}.)

Linear in the input's length: the @ can match only the first @, and the \. only the domain's first ., since the classes before each exclude it, so the tail after the \. is scanned once. The domain spelled [^\s\u0085@.][^\s\u0085@]*\.[^\s\u0085@]*[^\s\u0085@.], the same language, is quadratic: it tries every . as the split.

FactHashSchema
#

primitive_schemas.ts view source

Schema<Branded<string, "FactHash">, string, "pipe", readonly (SchemaOfKind<"string"> | SchemaOfKind<"number"> | SchemaOfKind<"bigint"> | ... 29 more ... | SchemaOfKind<...>)[]> & { ...; }

type Schema<Branded, string, "pipe", readonly (SchemaOfKind<"string"> | SchemaOfKind<"number"> | SchemaOfKind<"bigint"> | SchemaOfKind<"boolean"> | SchemaOfKind<"object"> | SchemaOfKind<"one_of"> | SchemaOfKind<"nullable"> | SchemaOfKind<"unknown"> | SchemaOfKind<"pipe"> | SchemaOfKind<"integer"> | SchemaOfKind<"min_value"> | SchemaOfKind<"max_value"> | SchemaOfKind<"literal"> | SchemaOfKind<"optional"> | SchemaOfKind<"with_default"> | SchemaOfKind<"with_default_fn"> | SchemaOfKind<"lazy"> | SchemaOfKind<"loose_object"> | SchemaOfKind<"strip_object"> | SchemaOfKind<"object_with_rest"> | SchemaOfKind<"record"> | SchemaOfKind<"array"> | SchemaOfKind<"union"> | SchemaOfKind<"variant"> | SchemaOfKind<"min_length"> | SchemaOfKind<"max_length"> | SchemaOfKind<"safe_integer"> | SchemaOfKind<"regex"> | SchemaOfKind<"uuid"> | SchemaOfKind<"iso_datetime"> | SchemaOfKind<"to_lower_case"> | SchemaOfKind<"check"> | SchemaOfKind<"transform">)[]> & { readonly '~types'?: { readonly input: string; readonly first: Schema<string, string, "string", undefined>; } | undefined; }

import {FactHashSchema} from '@fuzdev/fuz_app/primitive_schemas.js';

A blake3:-prefixed fact hash on the wire: FACT_HASH_EXACT, its output FactHash from @fuzdev/fuz_util/hash_schemas.ts — the type fact_hash_bytes, fact_hash_stream, and FactStore take — so a parsed value passes straight to them. Client-side (cell payloads) and server-side (DB-row hashes) consumers reuse this same schema. Its JSON Schema is the pattern.

is_email_within_length_max
#

primitive_schemas.ts view source

(s: string): boolean import {is_email_within_length_max} from '@fuzdev/fuz_app/primitive_schemas.js';

Whether s is at most EMAIL_LENGTH_MAX bytes in UTF-8, the bound Email checks.

s.length (UTF-16 units) is a cheap upper bound that short-circuits the encode on absurd inputs: a valid (<= 254-byte) address is necessarily <= 254 code units, so it never refuses one — the byte count is the real RFC-5321 octet bound, matching the Rust spine's s.len().

s

the address

type string

returns

boolean

true when the address is within the byte bound

Timestamp
#

primitive_schemas.ts view source

value + type

string import {Timestamp} from '@fuzdev/fuz_app/primitive_schemas.js';

A timestamp on the wire: a date and time of RFC 3339 in UTC, fuz_schema's iso_datetime, which both spines' YYYY-MM-DDTHH:MM:SSZ (iso8601_timestamp_column) and a toISOString() stamp satisfy. Not branded.

Username
#

primitive_schemas.ts view source

value + type

string import {Username} from '@fuzdev/fuz_app/primitive_schemas.js';

Username for account creation — starts with letter, alphanumeric/dash/underscore middle, ends with alphanumeric. No @ or . allowed.

Canonicalized to lowercase at parse time. The regex rejects whitespace outright, so no trim is needed. Storage is canonical across every creation site (bootstrap, signup, admin-create, invite acceptance) because the schema is the single source of truth — eliminates the per-caller trim().toLowerCase() ritual and keeps the LOWER(username) = LOWER($1) lookup contract simple. A string outside the bounds and off the grammar gets the length issue alone.

USERNAME_CANONICAL_REGEX
#

primitive_schemas.ts view source

RegExp import {USERNAME_CANONICAL_REGEX} from '@fuzdev/fuz_app/primitive_schemas.js';

A username's canonical grammar, USERNAME_REGEX in lowercase: a lowercase letter, then lowercase letters, digits, _ or -, ending in a lowercase letter or digit. What Username outputs, and what an account row holds (its account_username_lowercase CHECK).

USERNAME_LENGTH_MAX
#

primitive_schemas.ts view source

39 import {USERNAME_LENGTH_MAX} from '@fuzdev/fuz_app/primitive_schemas.js';

Maximum username length (matches GitHub's limit). The grammar is ASCII, so units and characters agree.

USERNAME_LENGTH_MIN
#

primitive_schemas.ts view source

3 import {USERNAME_LENGTH_MIN} from '@fuzdev/fuz_app/primitive_schemas.js';

Minimum username length (must have start + middle + end characters).

USERNAME_PROVIDED_LENGTH_MAX
#

primitive_schemas.ts view source

255 import {USERNAME_PROVIDED_LENGTH_MAX} from '@fuzdev/fuz_app/primitive_schemas.js';

Maximum length for username input on login/lookup, in UTF-16 code units, measured before trimming — more permissive than USERNAME_LENGTH_MAX for forward-compatibility if the creation limit is raised.

USERNAME_REGEX
#

primitive_schemas.ts view source

RegExp import {USERNAME_REGEX} from '@fuzdev/fuz_app/primitive_schemas.js';

The creation username's grammar: a letter, then letters, digits, _ or -, ending in a letter or digit. ASCII, so it needs at least two characters, and Username bounds the length beside it.

UsernameCanonical
#

UsernameProvided
#

primitive_schemas.ts view source

value + type

string import {UsernameProvided} from '@fuzdev/fuz_app/primitive_schemas.js';

Username submitted for login or lookup — minimal validation, for forward-compatibility if the creation grammar changes: at least one and at most USERNAME_PROVIDED_LENGTH_MAX UTF-16 code units, measured before trimming, holding a character that isn't whitespace.

Canonicalized to .trim().toLowerCase() at parse time, so login's per-account rate-limit key and its lookup see one value whatever the casing or surrounding whitespace, as Username canonicalizes what is stored.

A whitespace-only identifier is refused, not canonicalized to "" and left to fall through to a lookup-miss 401: an empty identifier is malformed input, not a wrong credential, and the Rust spine's account_login refuses it too. JS \s is exactly the set trim removes (ECMA-262's WhiteSpace and LineTerminator), so the regex ahead of the transform refuses what would trim to nothing.

Uuid
#

primitive_schemas.ts view source

value + type

Branded<string, "Uuid">

type Branded

import {Uuid} from '@fuzdev/fuz_app/primitive_schemas.js';

Branded UUID schema: a UUID of RFC 9562 in either case, fuz_schema's uuid (whose grammar is UUID_REGEX), output in lowercase — the form Postgres renders a uuid in and every id the server mints — so a parsed id compares equal to the same id read back from the database. The Rust spine canonicalizes the same way. The brand goes outside the to_lower_case, which outputs a new string.

external types

Infer<typeof Uuid>

UUID_REGEX
#

primitive_schemas.ts view source

RegExp import {UUID_REGEX} from '@fuzdev/fuz_app/primitive_schemas.js';

The UUID grammar of RFC 9562, in either case: five groups of hex digits, 8-4-4-4-12, with a version from 1 to 8 and the RFC's variant, or the nil or the max UUID, read case-insensitively as RFC 9562 §4 reads a UUID. The same source as the pattern fuz_schema's uuid renders to JSON Schema, which a test holds it to, and the grammar of the Rust spine's is_valid_uuid.

UuidWithDefault
#

primitive_schemas.ts view source

value + type

Branded<string, "Uuid">

type Branded

import {UuidWithDefault} from '@fuzdev/fuz_app/primitive_schemas.js';

Uuid that defaults to a fresh create_uuid() when the input is undefined.

external types

Infer<typeof UuidWithDefault>

Imported by
#