ValueSchema
The ValueSchema type describes what values are allowed for keyed Values in a Store.
{
type: "string";
default?: string | null;
allowNull?: boolean;
required?: boolean;
enum?: never;
} | {
type: "number";
default?: number | null;
allowNull?: boolean;
required?: boolean;
enum?: never;
} | {
type: "boolean";
default?: boolean | null;
allowNull?: boolean;
required?: boolean;
enum?: never;
} | {
type: "object";
default?: AnyObject | null;
allowNull?: boolean;
required?: boolean;
enum?: never;
} | {
type: "array";
default?: AnyArray | null;
allowNull?: boolean;
required?: boolean;
enum?: never;
} | {
type: SchemaTypeArray;
default?: string | number | boolean | AnyObject | AnyArray | null;
allowNull?: boolean;
required?: boolean;
enum?: never;
} | {
enum: readonly [string | number | boolean, ...(string | number | boolean)[]];
type?: never;
default?: string | number | boolean | null;
allowNull?: boolean;
required?: boolean;
}A ValueSchema specifies either the type of the Value (string, boolean, number, object, or array), a non-empty array of those types, or a non-empty enum of exact primitive values that are allowed. Multiple type names form a union, such as type: ['string', 'number'], and repeated names have no additional effect. The type and enum properties are mutually exclusive, and enum members can be strings, finite numbers, or booleans, including a mixture of those types.
For object and array types, TinyBase automatically serializes values to and from JSON when storing and retrieving them. Their contents should recursively be strings, finite numbers, booleans, null, plain objects, or arrays to ensure they are preserved.
Set allowNull to true to also allow null, whether the schema uses type or enum. A default value is used only when it has the correct type, matches any member of a type union, is an enum member when applicable, or is null when allowed. Literal schemas passed to Store schema setters are checked against these default rules by TypeScript. A valid default means that the Value will always be present in a Store. You can also set required to true to indicate to schema-based typing that the Value should be present even if it does not have a default.
If neither a default value nor required: true is provided, 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 expects a string Value to be present without providing a default value.
import type {ValueSchema} from 'tinybase';
export const requiredString: ValueSchema = {
type: 'string',
required: true,
};
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: [],
};
When applied to a Store, this ValueSchema allows either a string or numeric Value.
import type {ValueSchema} from 'tinybase';
export const referenceValue: ValueSchema = {
type: ['string', 'number'],
};
When applied to a Store, this ValueSchema allows one of three exact primitive values, or null.
import type {ValueSchema} from 'tinybase';
export const ratingValue: ValueSchema = {
enum: ['good', 5, true],
allowNull: true,
default: 'good',
};
Since
v3.0.0