0.1.6 • Published 8 years ago

react-state-validation-stlk v0.1.6

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

React tiny state validation

This package has been renamed to react-tiny-state-validation.

React State Validation validates state using defined validation functions.

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 = ....

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: ''
    }
  }

  _handleSubmit = (e) => {
    e.preventDefault();
    this.validate();
    const { errors } = this.state;
    if(!Object.keys(errors).every((k) => !errors[k])) {
      return null;
    }
    // Validations passed you can now submit the data
  }

  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 you're not using decorators, you can just wrap the component with the stateValidation method.

export stateValidation(App)

Contributing

Plz do it! oh and run npm test. I use standard for code style/linting, and ava for testing.

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago