Aurelia Validation Rules: Unable to parse accessor

2019-06-21 19:02发布

问题:

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?

回答1:

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