Pema
Pema is Primate's TypeScript schema package. It parses untrusted runtime values, reports structured validation errors and infers the corresponding TypeScript type. It can be used independently or anywhere Primate accepts a schema.
import p from "pema";
const User = p({
name: p.string,
age: p.u8,
});
const user = User.parse({ name: "Ada", age: 37 });
// { name: string; age: number }Pema is the TypeScript package documented in this section. Primate's Go, Python and Ruby backends have separate language-specific validation APIs; their names and capabilities are not guaranteed to match TypeScript Pema.
Validation and persistence
Every Pema schema describes parsing behavior. A smaller set is also Storable: it exposes a database datatype that a Primate Store can map to a column or document field.
schema: {
title: p.string, // Storable string
published: p.boolean.default(false), // Storable wrapper
metadata: p.json(p({ tags: p.array(p.string) })),
}Ordinary objects, arrays, unions and derived schemas are useful validation
schemas, but they are not direct Store fields. Wrap JSON-compatible structured
data in p.json(inner) when it should occupy one persistent JSON field. See
Pema and Stores for the complete compatibility table and Stores for keys,
CRUD, relations and migrations.
Where to go next
| Topic | Page |
|---|---|
| Parsing, inference, defaults and coercion | Schemas |
| Text, URLs and UUIDs | Strings |
| Numeric widths and enums | Numbers and enums |
| Booleans, dates and binary values | Built-in values |
| Object shapes and partial updates | Objects |
| Arrays, tuples, records and JSON | Collections |
| Unions and custom predicates | Composition |
| Issues and JSON Pointer paths | Errors |
| Persistent Store fields | Pema and Stores |
For framework integration, Input validation explains request bodies, query
parameters, headers and route schemas. Pema itself only requires a value and a
schema with parse().