4.0.6 • Published 3 years ago

@known-as-bmf/store-middleware-validate v4.0.6

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

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

3 years ago

4.0.5

3 years ago

4.0.4

4 years ago

4.0.3

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.0.0

4 years ago