Aurelia Validation Rules: Unable to parse accessor function
Asked Answered
M

1

6

It seems there have been various problems elsewhere in regards to the aurelia-validation module, but I haven't seen anything that has addressed the specific problem I've been running into.

I have a model class with the definition and validation rules as below:

my-model.js

my-model = {
    "name":
        {
        "full": "",
        "short": "",
        }
    };

...

ValidationRules
    .ensure(model => model.name.full).required().minLength(5).maxLength(50)
    .on(this.my-model);

When I try it in the browser, however, I get the error:

...
Inner Error:
Message: Unable to parse accessor function:
function (model) {
                        return model.name.full;
                    }
...

This question was the closest I was able to see to my problem, and another here seems to be having the same issue.

I am running aurelia-framework@^1.0.2 and aurelia-validation@^1.0.0-beta.1.0.1 which I believe are just the defaults from regular updates (but also the reason for it suddenly not working). Is it possible I'm still running incompatible versions of some modules? Or is there somewhere elsewhere in my code that I need to fix?

Moten answered 11/1, 2017 at 19:59 Comment(1)
I still don't get why your ensure didn't work. What was the problem?Serapis
S
3

What if you target the property instead of the object? Does that work?

myModel = {
  "name": {
    "full": "",
    "short": "",
  }
};

ValidationRules
  .ensure(model => model.full)
    .required()
    .minLength(5)
    .maxLength(50)
  .on(this.myModel.name); //<--- see
Sarpedon answered 11/1, 2017 at 20:15 Comment(1)
So I misread that and tried doing model.full without adjusting .on(this.myModel) and it worked even with that. I've updated it to it fully as you describe and it is working now. Thank you very much.Moten

© 2022 - 2024 — McMap. All rights reserved.