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

Commit

Permalink
Add properties mapping of rules
Browse files Browse the repository at this point in the history
  • Loading branch information
fastner committed May 12, 2016
1 parent 64e91b0 commit 673f127
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/BaseValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default class BaseValidator {
constructor(module = null)
{
this._ruleMap = []
this._propertiesMap = {}
this.module = module
}

Expand All @@ -50,8 +51,19 @@ export default class BaseValidator {
this._store = store
}

getProperties()
{
return Object.keys(this._propertiesMap)
}

getRulesByProperty(property)
{
return this._propertiesMap[property]
}

/*
* Add rule to validator. name {string} is the rule's name to find it in debugging mode.
* properties {array(string)} is a list of properties that are validated with this rule.
* validatorFunction {function(state)} is the validator function given global state as
* first parameter.
*
Expand All @@ -66,12 +78,25 @@ export default class BaseValidator {
* The validatorFunction is called in context of ValidatorAssertions, so every function in there
* is callable via this.functionName().
*/
rule(name, validatorFunction)
rule(name, properties, validatorFunction)
{
this._ruleMap.push({
name,
properties,
validatorFunction
})

const propertiesMap = this._propertiesMap
properties.forEach((prop) =>
{
if (!propertiesMap[prop])
propertiesMap[prop] = []

propertiesMap[prop] = {
name,
validatorFunction
}
})
}

isValid()
Expand Down

0 comments on commit 673f127

Please sign in to comment.