aurelia / aurelia/validation

Exception is thrown when running a rule with the validationController

Open
#514 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
129
Forks
125
PR merge metrics
No merged PRs in 30d

Description

**I'm submitting a bug report**

* **Library Version:**
1.3.2

**Please tell us about your environment:**
* **Operating System:**
Windows 10

* **Browser:**
all

**Current behavior:**

Running this code: `this.controller.validate({ object: this.user, rules: rules });`

Results in this exception:

> Uncaught (in promise) TypeError: Cannot read property 'name' of undefined

See it on Gist.Run :

https://gist.run/?id=a68bdf167a0a2b59f0e868268375692f

**Expected/desired behavior:**
That I can run a custom rule using the Validation Controller

In stepping through the code, the rule loop variable is not getting set properly.

In the below, ruleSequece is an array and so getting the first element results in the `rules` variable being set to the string 'isPopulated' (the name of the rule, not the actual rule).

`var rules = ruleSequence[sequence];`

So later code tries to loop through what it thinks is an arrary, but is actually a string, and the first char of the string is 'i' and it is trying to process it as if it were a rule and error out.

```
StandardValidator.prototype.validateRuleSequence = function (object, propertyName, ruleSequence, sequence, results) {
var _this = this;
// are we validating all properties or a single property?
var validateAllProperties = propertyName === null || propertyName === undefined;
var rules = ruleSequence[sequence];
var allValid = true;
// validate each rule.
var promises = [];
var _loop_1 = function (i) {
var rule = rules[i];
// is the rule related to the property we're validating.
if (!validateAllProperties && rule.property.name !== propertyName) {
return "continue";
}
// is this a conditional rule? is the condition met?
if (rule.when && !rule.when(object)) {
return "continue";
}
// validate.
//Throws error here, on rule.property.name
var value = rule.property.name === null ? object : object[rule.property.name];
var promiseOrBoolean = rule.condition(value, object);
if (!(promiseOrBoolean instanceof Promise)) {
promiseOrBoolean = Promise.resolve(promiseOrBoolean);
}
promises.push(promiseOrBoolean.then(function (valid) {
var message = valid ? null : _this.getMessage(rule, object, value);
results.push(new validate_result_1.ValidateResult(rule, object, rule.property.name, valid, message));
allValid = allValid && valid;
return valid;
}));
};
for (var i = 0; i < rules.length; i++) {
_loop_1(i);
}
return Promise.all(promises)
.then(function () {
sequence++;
if (allValid && sequence < ruleSequence.length) {
return _this.validateRuleSequence(object, propertyName, ruleSequence, sequence, results);
}
return results;
});
};
```

Contributor guide

Open the contributing guide

Research direction

Start at StandardValidator.prototype.validateRuleSequence, using the supplied validationController.validate call and Gist reproduction. Check how ruleSequence is built for the custom isPopulated rule; done means the controller validates the custom rule without treating its name as an iterable rule and the reported TypeError no longer occurs.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.