Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gregoryagu opened this issue Dec 25, 2018 · 0 comments
Open

Comments

@gregoryagu
Copy link

gregoryagu commented Dec 25, 2018

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;
            });
        };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant