Skip to content

v9.5

Choose a tag to compare

@jamesgpearce jamesgpearce released this 15 Aug 10:26
· 58 commits to main since this release

Schema Enums

CellSchema and ValueSchema can now use an enum property instead of type to allow only specific primitive values (as requested in issue #38). Enums must be non-empty, can mix strings, finite numbers, and booleans, and continue to use allowNull when null is also valid.

import {createStore} from 'tinybase';

const enumStore = createStore().setValuesSchema({
  status: {enum: ['available', 'adopted'], default: 'available'},
  rating: {enum: ['good', 5, true], allowNull: true},
});

enumStore.setValues({status: 'adopted', rating: true});
enumStore.setValue('status', 'missing');
console.log(enumStore.getValues());
// -> {status: 'available', rating: true}

Each schema entry must use exactly one of type or enum. Defaults are used only when they are enum members, or are null when null is allowed. The schema-aware Store APIs infer exact unions from enum members, and the Zod, Valibot, ArkType, Effect Schema, TypeBox, and Yup schematizers now preserve supported primitive enum and literal constraints in the schemas they produce.

Schema Type Unions

CellSchema and ValueSchema can now accept a non-empty array in the type property to list one or more allowed broad types (as requested in issue #223):

const unionStore = createStore().setValuesSchema({
  reference: {type: ['string', 'number'], default: 'unknown'},
  response: {type: ['boolean', 'object'], allowNull: true},
});

unionStore.setValues({reference: 42, response: {accepted: true}});
unionStore.setValue('reference', false);
console.log(unionStore.getValues());
// -> {reference: 'unknown', response: {accepted: true}}

A type array can contain string, number, boolean, object, and array. Multiple type names form a union, and repeated names have no additional effect. null continues to be represented by allowNull: true, and defaults are used only when they match one of the listed types. Each schema entry still uses exactly one of type or enum: a type array allows every value of its listed types, while an enum allows only its listed values.

The schema-aware Store APIs infer the corresponding TypeScript unions. The ArkType, Effect Schema, TypeBox, Valibot, and Zod schematizers also preserve supported broad type unions, while continuing to preserve literal-only unions as exact enums.

Schema-Aware Type Fixes

This release also corrects a number of declarations on the /with-schemas type surface. These are type-level fixes only, and require no changes to your runtime code: