Awesome
:exclamation: Deprecated in favor of NeoForm :exclamation:
Valya is just a tiny Higher-Order Component for validation in React that can be used as an ES7 decorator.
Install
npm i -S valya
Example
Creating Validator
import React from 'react';
import Valya from 'valya';
@Valya
class Validator extends React.Component {
static displayName = 'Validator';
_renderError() {
if (!this.props.enabled || this.props.isValid) {
return null;
}
return (
<span className="validator__error" key="error">
{this.props.validationErrorMessage}
</span>
);
}
render () {
return (
<span className="validator">
<span className="validator__target" key="target">
{this.props.children}
</span>
{this._renderError()}
</span>
);
}
}
Your Validator
will receive the following "special" props:
enabled
initialValidation
isValidating
isValid
validationErrorMessage
Usage
_onInputChange(e) {
this.setState({
value: e.target.value
});
}
render () {
return (
<Validator
value={this.state.value}
onStart={() => {
console.log('Validation start');
}}
onEnd={(isValid, message) => {
console.log('validation end:', isValid, message);
}}
validators={[
{
validator(value, params) {
if (value) {
return Promise.resolve();
}
return Promise.reject(params.message);
},
params: {
message: 'Field is required'
}
}
]}>
<div>
<input type="text" value={this.state.value} onChange={::this._onInputChange} />
</div>
</Validator>
);
}
You can pass the following props:
enabled <boolean>
– control whether Valya should be enabled (true
by default)initialValidation <boolean>
– control whether Valya should validate first right in constructor (false
by default)value <any>
– current target's value to validateonStart <Function>
– "on validation start" callbackonEnd <Function>
– "on validation end" callbackvalidators <Array>
– array of "validators": everyvalidator
is just a function that must return apromise
, so Valya doesn't really cares about what is happening inside.