TinyBase logoTinyBase β

ValueSchema

The ValueSchema type describes what values are allowed for keyed Values in a Store.

{
  type: "string";
  default?: string | null;
  allowNull?: boolean;
} | {
  type: "number";
  default?: number | null;
  allowNull?: boolean;
} | {
  type: "boolean";
  default?: boolean | null;
  allowNull?: boolean;
} | {
  type: "object";
  default?: AnyObject;
  allowNull?: boolean;
} | {
  type: "array";
  default?: AnyArray;
  allowNull?: boolean;
}

A ValueSchema specifies the type of the Value (string, boolean, number, null since v7.0, or object or array since v8.0), and what the default value can be when an explicit value is not specified.

For object and array types, TinyBase automatically serializes values to and from JSON when storing and retrieving them.

If a default value is provided (and its type is correct), you can be certain that the Value will always be present in a Store.

If the default value is not provided (or its type is incorrect), the Value may not be present in the Store, but when present you can be guaranteed it is of the correct type.

Examples

When applied to a Store, this ValueSchema ensures a boolean Value is always present, and defaults it to false.

import type {ValueSchema} from 'tinybase';

export const requiredBoolean: ValueSchema = {
  type: 'boolean',
  default: false,
};

When applied to a Store, this ValueSchema allows an array Value containing a list of items, defaulting to an empty array.

import type {ValueSchema} from 'tinybase';

export const cartItems: ValueSchema = {
  type: 'array',
  default: [],
};

Since

v3.0.0