Skip to content
Logo

T.record

A collection: random keys and values, each from its own schema.

T.record(
  T.string.whereby({ length: { min: 4, max: 12 } }),
  T.string.whereby({ length: { max: 40 } }),
).whereby({ size: { max: 6, minTried: 2 } });

Use it for keys you don't know when you write the schema, such as a metadata bag, a translation map, or a lookup by generated id. Keys and values both come from schemas, so you control a key's format like any other value.

size is { max, minTried }, not { max, min }, on purpose. Keys are drawn independently, so two can come out the same and collapse into one entry. max is a real upper bound, but the lower number is only tried, never guaranteed. With a wide key schema, such as a long string, collisions rarely matter. With a narrow one, such as a single digit, you'll often get fewer entries than you asked for. Fabricator never redraws a colliding key. Collapsing is the intended behavior. For a record that redraws colliding keys and holds exactly the size drawn, use T.unique.record.

Adapters state size.max as the record's maximum, and no minimum, since minTried counts attempts rather than entries. .admits({ size }) changes what adapters accept without changing what is generated. See .admits(...).