npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-validator

v0.2.3

Published

Action parameter validator middleware for redux

Downloads

7,904

Readme

redux-validator Build Status Coverage Status

Action parameter validator middleware for redux

Installation

npm i redux-validator --save

How to use

Add middleware to your store

import Validator from 'redux-validator';

const validator = Validator();
const createStoreWithMiddleware = applyMiddleware(validator)(createStore);
const store = createStoreWithMiddleware(reducer);

If you use other middleware, apply redux-validator first to prevent unexpected errors.

import promise from 'redux-promise';
import Validator from 'redux-validator';

const validator = Validator();
const createStoreWithMiddleware = applyMiddleware(validator, promise)(createStore);
const store = createStoreWithMiddleware(reducer);

Add validators to your actions

const validAction = {
    type: ADD_TODO,
    payload: {
        text: 'Sample todo',
        complete: false
    },
    meta: {
        validator: {
            text: {
                func: (text, state, payload) => 0 <= text.length
                msg: 'Cannot add an empty todo'
            },
            complete: {
                func: (complete, state, payload) => typeof(complete) === "boolean"
                msg: 'Complete must be true or false'
            }
        }
    }
}

const result = dispatch(validAction); // validation success and dispatch completes

redux-validator is Flux Standard Action compatible.

The properties in action.payload are validated against the functions provided in the action.meta.validator map. If validation of any property fails, the dispatch is aborted and an object containing error information is returned.

const invalidAction = {
    type: ADD_TODO,
    payload: {
        text: ''
    },
    meta: {
        validator: {
            text: {
                func: (text, state, payload) => 0 <= text.length
                msg: 'Cannot add an empty todo'
            }
        }
    }
}

const result = dispatch(invalidAction); // dispatch aborted and error returned:
// result = {
//     err: 'validator',
//     msg: 'Cannot add an empty todo',
//     param: 'text',
//     id: 0
// }

Multiple validations may be defined for a single property. The property will be checked against all validators in the array in the order they are declared. The dispatch is aborted and the error returned on the first failing validation.

const action = {
    type: ADD_TODO,
    payload: {
        text: 'Write an awesome react app'
    },
    meta: {
        validator: {
            text: [
                {
                    func: (text, state, payload) => 0 <= text.length
                    msg: 'Cannot add an empty todo'
                },
                {
                    func: (text, state, payload) => text.length < 500
                    msg: 'Todo too long'
                }
            ]
        }
    }
}

Other Examples

Usage with redux-actions

It's very easy to integrate redux-validator with createAction from redux-actions:

import {createAction} from 'redux-actions';

const actionCreator = createAction('action2', payload => payload, () => ({
    validator: {
        payload: [ // if action.payload is not a map, use payload key to validate action.payload itself
            {
                func: (payload, state) => payload >= 0,
                msg: 'payload is less than 0'
            },
            {
                func: (payload, state) => payload < 100
            }
        ]
    }
}));

const result1 = dispath(actionCreator(-1));
const result2 = dispath(actionCreator(200));
// dispatchs would abort!
// result1 = {
//     err: 'validator',
//     msg: 'payload is less than 0',
//     param: 'payload',
//     id: 0
// }
// result2 = {
//     err: 'validator',
//     msg: '',
//     param: 'payload',
//     id: 1
// }

Async actions (thunk support)

import {createAction} from 'redux-actions';

const actionCreator = createAction('action2', payload => (
    {
        payload, // if you want a param to be validated, then return this param
        thunk: dispatch => {} // thunk would not be validated, and would be dispatched if validator all succeed
    }
), () => ({
    validator: {
        payload: [
            {
                func: (payload, state) => payload >= 0,
                msg: 'payload is less than 0'
            },
            {
                func: (payload, state) => payload < 100
            }
        ]
    }
}));

const result1 = dispath(actionCreator(-1));
const result2 = dispath(actionCreator(10));
// result1 would abort!
// result1 = {
//     err: 'validator',
//     msg: 'payload is less than 0',
//     param: 'payload',
//     id: 0
// }
// result2 would not abort, and it would continue to pass through thunk middleware
// result2 = thunk

API

Middleware Options

validatorKey

Override the default location for the validators in action objects (default is 'meta').

import Validator from 'redux-validator';

const validator = Validator({
    validatorKey: 'val' // validator should be put inside action.val
});

paramKey

Override the default location for the parmas in action objects (default is payload).

import Validator from 'redux-validator';

const validator = Validator({
    paramKey: 'val' // validator should be put inside action.val
});