@known-as-bmf/store-middleware-validate
TypeScript icon, indicating that this package has built-in type declarations

4.0.6 • Public • Published

@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;

Package Sidebar

Install

npm i @known-as-bmf/store-middleware-validate

Weekly Downloads

1

Version

4.0.6

License

MIT

Unpacked Size

20.9 kB

Total Files

23

Last publish

Collaborators

  • known-as-bmf