npm

Need private packages and team management tools?Check out npm Orgs. »

react-state-validation

0.1.0 • Public • Published

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)

install

npm i react-state-validation

Downloadsweekly downloads

0

version

0.1.0

license

ISC

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability