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

Can not put validation rules on integer attributes #474

Closed
fracz opened this issue Jan 5, 2018 · 2 comments · Fixed by #488
Closed

Can not put validation rules on integer attributes #474

fracz opened this issue Jan 5, 2018 · 2 comments · Fixed by #488

Comments

@fracz
Copy link

fracz commented Jan 5, 2018

I'm submitting a feature request or bug report, really don't know

  • Library Version:
    1.1.2

I want to validate the following object:

let myObject  = {
  2: '',
  5: ''
};

The identifiers of properties are dynamic based on my data model.

The 1st try

ValidationRules.ensure(2).required().on(myObject);

results in the following error

aurelia-task-queue.js:59 Uncaught Error: Unable to parse accessor function: 2

The 2nd try

ValidationRules.ensure(obj => obj[2]).required().on(myObject);

Uncaught Error: Unable to parse accessor function: function (obj) { return obj[2]; }

OT: In fact, I have noticed that .ensure(obj => obj.someProp) works but .ensure(obj => obj['someProp']) does not. See this gist for reference.

The 3rd try

ValidationRules.ensure('2').required().on(myObject);

builds the validation rules indeed, but they are not applied when used with validate binding behavior. I have debugged it down to this line which compares '2' !== 2 and skips the validation.

The 4rd try (ugly hack aka workaround)
This works, but is far from pretty.

const v = ValidationRules.ensure('2').required();
v.rules.forEach(rules => rules.forEach(rule => rule.property.name = parseInt(rule.property.name)));
v.on(myObject);
@jdanyow
Copy link
Contributor

jdanyow commented Jan 5, 2018

If you change:

if (!validateAllProperties && rule.property.name !== propertyName) {

To:

if (!validateAllProperties && rule.property.name !== propertyName.toString()) { 

does it work?

@fracz
Copy link
Author

fracz commented Jan 5, 2018

Adding the toString() there causes the validation to be applied on given field as expected. However, the error is not being rendered in the GUI (I am using "standard" BootstrapValidationRenderer from examples). It works, when using my hack so I guess there is another strict comparison somewhere.

jdanyow pushed a commit that referenced this issue Apr 29, 2018
* chore(vscode): disable formatOnSave and set tslint to local path

* fix(property-access): make number and string property keys work the same way

* fix(validation-messages): assign the parser in the constructor

* fix(property-accessor-parser): handle numeric property keys

fixes #474
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants