Skip to content

Exception is thrown when running a rule with the validationController #514

@gregoryagu

Description

@gregoryagu

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;
            });
        };

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions