Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

Commit

Permalink
Add errors array to validation assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
fastner committed May 17, 2016
1 parent a023f36 commit f82d1fd
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/BaseValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -101,7 +128,7 @@ export default class BaseValidator {

propertiesMap[prop].push({
name,
validatorFunction: validatorFunction.bind(validatorAssertions)
validatorFunction: boundValidatorFunction
})
})
}
Expand All @@ -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 {
Expand Down

0 comments on commit f82d1fd

Please sign in to comment.