Skip to content

Instantly share code, notes, and snippets.

@bultas
Last active October 4, 2017 14:50
Show Gist options
  • Save bultas/c46c4aac3c885a9a1f37cea1d407b888 to your computer and use it in GitHub Desktop.
Save bultas/c46c4aac3c885a9a1f37cea1d407b888 to your computer and use it in GitHub Desktop.
Redux-Validator-Middleware
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