0.1.0 • Published 8 years ago

react-state-validation v0.1.0

Weekly downloads
2
License
ISC
Repository
github
Last release
8 years ago

React State Validation

Build Status

React State Validation uses the propTypes validation pattern to validate states.

Usage

The stateValidation method can be used a decorator.

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.

export stateValidation(App)
0.1.0

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago