Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jcblw/react-state-validation
Validate react components state
https://github.com/jcblw/react-state-validation
Last synced: 16 days ago
JSON representation
Validate react components state
- Host: GitHub
- URL: https://github.com/jcblw/react-state-validation
- Owner: jcblw
- Created: 2015-12-19T06:27:24.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-12T14:22:16.000Z (over 7 years ago)
- Last Synced: 2024-10-11T14:38:42.138Z (about 1 month ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# React State Validation
[![Build Status](https://travis-ci.org/jcblw/react-state-validation.svg)](https://travis-ci.org/jcblw/react-state-validation)
React State Validation uses the propTypes validation pattern to validate states.
## Usage
The `stateValidation` method can be used a decorator.
```javascript
import React, {Component} from 'react'
import {stateValidation} from 'react-state-validation'
const stateValidations = {
customState: function(state, stateName, componentName) {
if (!/matchme/.test(state[stateName])) {
return new Error('Validation failed!');
}
}
}@stateValidation
class App extends Component {
constructor () {
super()
this.state = {
customState: ''
}
}render () {
// the stateValidation method will set the errors key will the current state errors
const {errors} = this.state
// 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!']
//...
}
}App.stateValidations = stateValidations
```or if your not using decorators you can just wrap the component with the stateValidation method.
```javascript
export stateValidation(App)
```## Contributing
Plz do it! oh and run `npm test`. I use [standard](http://standardjs.com/) for code style/linting, and [ava](https://github.com/sindresorhus/ava) for testing.
## Cool modules to use with this
- [react-validator-prop-types](https://www.npmjs.com/package/react-validator-prop-types)