Redux middleware for fsa payload validation.
This liblary no supported redux-thunk
$ npm install --save redux-payload-validator
or
$ yarn add redux-payload-validator
// 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)