From f82d1fddd152d4a0c5d253bb02f2c82f18a02ae3 Mon Sep 17 00:00:00 2001 From: Sebastian Fastner Date: Tue, 17 May 2016 11:21:09 +0200 Subject: [PATCH] Add errors array to validation assertions --- src/BaseValidator.js | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/BaseValidator.js b/src/BaseValidator.js index e1eadc9..9270838 100644 --- a/src/BaseValidator.js +++ b/src/BaseValidator.js @@ -3,13 +3,34 @@ import isFinite from "lodash-es/isFinite" import isString from "lodash-es/isString" class ValidatorAssertions { + constructor() + { + this._errors = [] + } + + clearErrors() + { + this._errors = [] + } + + errors() + { + const errors = this._errors + this.clearErrors() + return errors + } + invalid(fields, error) { - return { + const err = { valid: false, fields, error } + + this._errors.push(err) + + return err } isNumber(value) @@ -87,10 +108,16 @@ export default class BaseValidator { */ rule(name, properties, validatorFunction) { + function boundValidatorFunction(...rest) + { + validatorAssertions.clearErrors() + return validatorFunction.apply(validatorAssertions, rest) + } + this._ruleMap.push({ name, properties, - validatorFunction + validatorFunction: boundValidatorFunction }) const propertiesMap = this._propertiesMap @@ -101,7 +128,7 @@ export default class BaseValidator { propertiesMap[prop].push({ name, - validatorFunction: validatorFunction.bind(validatorAssertions) + validatorFunction: boundValidatorFunction }) }) } @@ -114,7 +141,7 @@ export default class BaseValidator { this._ruleMap.map( (rule) => { - const valid = rule.validatorFunction.call(validatorAssertions, state) + const valid = rule.validatorFunction(state) if (valid && valid.valid === false) return {