Last active
October 4, 2017 14:50
-
-
Save bultas/c46c4aac3c885a9a1f37cea1d407b888 to your computer and use it in GitHub Desktop.
Redux-Validator-Middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { validator } from 'validator' | |
import { VALIDATOR_SYMBOL } from 'validator-middleware'; | |
const data = { x: 1, y: 2 }; | |
const validator = validator(fn1, fn2); // curry, one data input left | |
const action = { | |
[VALIDATOR_SYMBOL]: { | |
data, | |
validator, | |
types: ['VALID', 'INVALID'] | |
} | |
} | |
// validator-middleware read VALIDATOR ACTION | |
// then do validation with action's validator and data | |
// then dispatch FSA to next middleware according to validate result | |
{ | |
type: 'VALID' || 'INVALID', | |
payload: result | |
} | |
// Then you can for example create custom reducer to save validator result | |
const formReducer = (state, action) => { | |
if (action.type === 'VALID') { | |
return action.payload; | |
} | |
return state; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment