Primate LogoPrimate

Collections and JSON

Arrays

p.array(item) validates every member and returns ArrayType.

const Tags = p.array(p.string).unique().max(10);
Tags.parse(["pema", "validation"]);
Method Requirement
unique() Primitive members are unique
uniqueBy(select) Selected key is unique
min(n) At least n items
max(n) At most n items
length(from, to) Inclusive item-count range

Cardinality bounds must be finite, nonnegative integers. Item errors use their numeric JSON Pointer path, such as /2. Arrays also support optional() and default().

A one-member array shorthand passed to callable p means the same thing:

const Tags = p([p.string]);

Tuples

p.tuple(...items) validates a fixed position and length for each member:

const Coordinate = p.tuple(p.number, p.number);
Coordinate.parse([13.4, 52.5]);

TupleType reports positional paths and supports optional(). A schema shorthand array with zero or more than one member is normalized as a tuple.

Records and dictionaries

p.record(key, value) validates a dynamic record. Keys may use p.string, p.number or p.symbol; every value is parsed with the supplied schema.

const Scores = p.record(p.string, p.u16);
const ById = p.record(p.number, p({ name: p.string }));

RecordType rejects keys of the wrong category and reports string/number value errors at the corresponding property path. Symbol-key values are also supported.

p.dict(value?) is the string-key convenience form. Its value defaults to p.string:

p.dict();          // Record<string, string>
p.dict(p.boolean); // Record<string, boolean>

Records and dictionaries are validation-only; use JSON when the complete value should be one persistent field.

JSON values

p.json() accepts JSON-compatible primitives, arrays and objects and is Storable with datatype json. It removes object properties whose value is undefined, but rejects undefined array members and non-JSON values such as bigint, Date and URL.

Pass an inner schema for typed validation while retaining one JSON database field:

const Cart = p.json(p({
  items: p.array({
    sku: p.string,
    quantity: p.u16,
  }),
}));

JSONType delegates parsing to the inner schema when present. Ensure its parsed output is genuinely JSON-compatible; the inner schema supplies validation, not automatic conversion of arbitrary runtime objects.

See Pema and Stores for persistence guidance.

Previous
Objects
Next
Composition