React tiny state validation
Validates state using defined validation functions.
Instalation
npm install react-tiny-state-validation
Usage
The stateValidation
method can be used as a decorator. State is not validated until this.validate()
function is called.
You can define stateValidations
as a static variable or dynamically in constructor using this.stateValidations = ...
.
const stateValidations = { if !/matchme/ return 'Validation failed!'; } @stateValidation { super thisstate = customState: '' } { e; this; const errors = thisstate; if!Object return null; // Validations passed you can now submit the data } { // the stateValidation method will set the errors key will the current state errors const errors = thisstate // the key of the state will be the same key in the errors object const customState = errors // if there is errors it will be an array of strings // eg. ['Validation failed!'] //... } AppstateValidations = stateValidations
or if you're not using decorators, you can just wrap the component with the stateValidation
method.
Contributing
Plz do it! oh and run npm test
. I use standard for code style/linting, and ava for testing.