Schemas

How to describe a document's shape with primitives, and what the server validates.

Every collection has a schema, and every document in it follows that schema. You write a schema once, in TypeScript, with the Primitive builders of @voidhash/mimic-core. The same definition gives you the schema the server enforces, the TypeScript types of the document, and a typed way to edit it. This page lists the primitives and explains validation.

Defining a schema

import { Primitive } from "@voidhash/mimic-core";

export const Note = Primitive.Struct({
  title: Primitive.String().min(1).max(120).required(),
  body: Primitive.String().default(""),
  pinned: Primitive.Boolean().default(false),
  tags: Primitive.Array(Primitive.String()).default([]),
}).required();

A document's root is usually a Struct or a Tree. Primitives are immutable: every modifier such as .required() returns a new primitive.

Primitives

PrimitiveHoldsModifiers besides required and default
Primitive.String()A stringmin, max, length, regex, email, url
Primitive.Number()A finite numbermin, max, positive, negative, int
Primitive.Boolean()true or false
Primitive.Literal(value)Exactly one string, number or boolean
Primitive.Struct(fields)An object with the named fieldsextend, partial
Primitive.Array(element)An ordered list of elements, each with a stable idminLength, maxLength
Primitive.Either(a, b, …)A value matching one of several primitives, tried in order
Primitive.Union(variants)One of several structs, told apart by a discriminator field
Primitive.Tree({ root })A tree of typed nodes
Primitive.TreeNode(type, config)One node type of a tree: its data and the child types it allows

Arrays and trees have their own page: Lists and trees.

Required, optional and default

A field is optional unless you say otherwise:

  • Optional (no modifier): the field may be absent. It reads as undefined.
  • .required(): the field must be present. A document without it is invalid.
  • .default(value): when the field is absent, it takes the default. The field always reads as a value, never undefined.

The TypeScript types follow: in a snapshot, a required or defaulted field has its type, and only an optional field without a default is T | undefined.

Validators

String and number validators are part of the schema, so the server enforces them too.

const Profile = Primitive.Struct({
  email: Primitive.String().email().required(),
  website: Primitive.String().url(),
  handle: Primitive.String().regex(/^[a-z0-9_]{3,20}$/),
  age: Primitive.Number().int().min(0),
});

regex accepts a safe subset of regular expressions that can be matched in linear time: patterns of at most 256 characters with the flags i, m, s and u, without backreferences, lookarounds or named groups, and without a quantifier on a group that itself contains a quantifier or |. A pattern outside that subset throws a PrimitiveError when the schema is built.

Structs

A struct is an object with a fixed set of fields. extend(fields) returns a struct with more fields, and partial() one with every field optional. A field cannot be named get, set or update, because the struct's editing proxy has methods with those names.

Either and Union

Either is for alternatives of simple values:

const Fill = Primitive.Either(Primitive.Literal("none"), Primitive.String());

At most one of its variants may hold structured data (a struct, union, array or tree). For alternatives of structured shapes, use Union, which adds a discriminator field (type by default) naming the variant:

const Block = Primitive.Union({
  text: Primitive.Struct({ value: Primitive.String().required() }),
  image: Primitive.Struct({ url: Primitive.String().required() }),
});

// A value of this union: { type: "text", value: "hello" }

Pass { discriminator: "kind" } as the second argument to use another field name.

Types from a schema

@voidhash/mimic-core derives three types from a primitive:

type NoteSnapshot = Primitive.InferSnapshot<typeof Note>; // what you read
type NoteInput = Primitive.InferInput<typeof Note>; // what you pass when creating or setting
type NoteProxy = Primitive.InferProxy<typeof Note>; // what you edit through

The snapshot is the document as plain, immutable data. The input is the same shape with defaulted and optional fields left out as you like. The proxy is the object a transaction receives; see Reading and writing.

What validation does

The server validates a document when it is created and after every transaction, and clients validate what they receive. Validation checks the value against the schema:

  • A missing required field, a value of the wrong kind or a failed validator makes the document invalid. A transaction that would leave the document invalid is rejected as a whole, validation_failed.
  • The server accepts a value only in the schema's canonical form: a field the schema does not declare, or an absent field that has a default, makes it invalid too. The client and the engine SDK put what they send in that form, so this matters only to values you build by hand.

Because defaults are part of the schema, the server and every client fill in a missing default identically.

Document values are nested at most 64 levels deep, and a tree at most 256 levels.

Changing a schema

A collection's schema changes only through a migration, which is checked against every existing document before it takes effect. A client can tell the server which schema it was built with, so that an outdated client is not allowed to write; see Schema versions.