@known-as-bmf/store-middleware-validate
v4.0.6
Published
Validation middleware for @known-as-bmf/store.
Downloads
11
Maintainers
Readme
@known-as-bmf/store-middleware-validate
Installation
npm install --save @known-as-bmf/store @known-as-bmf/store-middleware-validator
Description
Middleware to be used with @known-as-bmf/store
. Allows you to validate / prevent state updates that don't pass validation.
Usage
Pass the middleware to of
:
import { of } from '@known-as-bmf/store';
import { validatorMiddleware } from '@known-as-bmf/store-middleware-validator';
const store = of(
{
preferences: { theme: 'dark', lang: 'fr' },
lastOnline: '2020-02-21T18:22:33.343Z',
someArray: [],
},
validatorMiddleware([(s) => ['dark', 'light'].includes(s.preferences.theme)])
);
If the initial state do not pass validation, the call to of
will throw an error.
When you update the state with swap
or set
, if the new state do not pass validation, the call will also throm an error.
API
validatorMiddleware
/**
* Create a new validation middleware.
* @param validators A list of validators that will be checked against every
* time the state is being changed.
*/
const validatorMiddleware = <S>(validators: Validator<S>[]): Middleware<S>;
type Validator<S> = (state: S) => boolean;