calidation
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -96,3 +96,3 @@ 'use strict'; | ||
}, _this.validate = function (fields, config) { | ||
return Object.entries(config).reduce(function (all, _ref6) { | ||
return Object.entries(config).reduce(function (allErrors, _ref6) { | ||
var _ref7 = _slicedToArray(_ref6, 2), | ||
@@ -102,5 +102,5 @@ name = _ref7[0], | ||
return _extends({}, all, _defineProperty({}, name, _this.validateField(fieldConfig, name, fields))); | ||
return _extends({}, allErrors, _defineProperty({}, name, _this.validateField(fieldConfig, name, fields, allErrors))); | ||
}, {}); | ||
}, _this.validateField = function (fieldValidators, name, allFields) { | ||
}, _this.validateField = function (fieldValidators, name, allFields, allErrors) { | ||
return Object.entries(fieldValidators).reduce(function (error, _ref8) { | ||
@@ -113,2 +113,14 @@ var _ref9 = _slicedToArray(_ref8, 2), | ||
var validator = _this.props.validators[validatorName]; | ||
(0, _invariant2.default)(validator, "You specified a validator that doesn't exist. You " + ('specified ' + validatorName + '. Available validators: \n\n') + Object.keys(_this.props.validators).join(',\n')); | ||
var context = { | ||
fields: allFields, | ||
errors: _extends({}, _this.state.errors, allErrors) | ||
}; | ||
if (typeof validatorConfig === 'function') { | ||
validatorConfig = validatorConfig(context); | ||
} | ||
if (typeof validatorConfig === 'string') { | ||
@@ -118,4 +130,7 @@ validatorConfig = { message: validatorConfig }; | ||
var validator = _this.props.validators[validatorName]; | ||
(0, _invariant2.default)(validator, "You specified a validator that doesn't exist. You " + ('specified ' + validatorName + '. Available validators: \n\n') + Object.keys(_this.props.validators).join(',\n')); | ||
if (typeof validatorConfig.validateIf === 'function' && !validatorConfig.validateIf(context)) { | ||
return null; | ||
} else if (typeof validatorConfig.validateIf === 'boolean' && !validatorConfig.validateIf) { | ||
return null; | ||
} | ||
@@ -122,0 +137,0 @@ return validator(validatorConfig)(allFields[name]); |
{ | ||
"name": "calidation", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "A red hot validation library for React", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -5,3 +5,3 @@ # calidation | ||
[![Build Status](https://travis-ci.org/selbekk/calidation.svg?branch=master)](https://travis-ci.org/selbekk/calidation) [![codecov](https://codecov.io/gh/selbekk/calidation/branch/master/graph/badge.svg)](https://codecov.io/gh/selbekk/calidation) [![npm version](https://badge.fury.io/js/calidators.svg)](https://badge.fury.io/js/calidators) | ||
[![Build Status](https://travis-ci.org/selbekk/calidation.svg?branch=master)](https://travis-ci.org/selbekk/calidation) [![codecov](https://codecov.io/gh/selbekk/calidation/branch/master/graph/badge.svg)](https://codecov.io/gh/selbekk/calidation) [![npm version](https://badge.fury.io/js/calidation.svg)](https://badge.fury.io/js/calidation) | ||
@@ -180,3 +180,3 @@ ``` | ||
message: 'You need to answer this question', | ||
validateIf: fields => fields.someOtherField === 'foo', | ||
validateIf: ({ errors, fields }) => fields.someOtherField === 'foo', | ||
}, | ||
@@ -186,2 +186,19 @@ }, | ||
If you want to do cross validation, or just need access to the other field inputs and / or errors, each validator also | ||
accepts a function that receives all fields and the current error state. | ||
```js | ||
password: { | ||
isRequired: 'You need a password', | ||
}, | ||
repeatPassword: { | ||
isRequired: 'Please fill out the password a second time', | ||
isEqual: ({ fields }) => { | ||
message: 'The two password must match', | ||
value: fields.password, | ||
validateIf: fields.password.length > 0, // this can be a boolean too! | ||
}, | ||
} | ||
``` | ||
Finally, please note that validation happens from top to bottom. The validation | ||
@@ -188,0 +205,0 @@ will quit once it encounters an error, so you can have multiple levels of |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38341
402
506