Skip to content

t-tonchim/redux-payload-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

redux-payload-validator

Redux middleware for fsa payload validation.

This liblary no supported redux-thunk

NPM

Table of Contents

Getting Started

Installation

$ npm install --save redux-payload-validator

or

$ yarn add redux-payload-validator

Usage

// validator/index.js

const validator = {
  INCREMENT: count => {  // It will be mappped same action type.
    if(typeof count !== 'number'){
      throw new Error('Error! Argument must be number.') // throw some Error.
    }
  }
}

export default validator
// store
/* ...import others ... */

import payloadValidator from 'redux-payload-validator'
import validator from './validator'

export default createStore(
  rootReducer,
  initialState,
  applyMiddleware(
    payloadValidator(validator)
    /* ... */
  )
)

If throw Error, then middleware add '{error: true}' and change payload to '{ payload: errorObject }'.

// reducer

import {handleActions} from 'redux-actions'
export const initialState = {
  result: ''
}

export default handleAction('INCREMENT', {
    next(state, action){
      return { result: 'valid!' }
    },
    throw(state, action){
      return { result: action.payload.message }
    }
}, initialState)