Skip to content

Instantly share code, notes, and snippets.

@singledigit
Last active October 11, 2016 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singledigit/36c07af90bf34d6be1aedfcf5590b275 to your computer and use it in GitHub Desktop.
Save singledigit/36c07af90bf34d6be1aedfcf5590b275 to your computer and use it in GitHub Desktop.
Dynamically build Aurelia Validation rules.
//This is a rough attempt at dynamically building validation rules based on a JSON document.
let rules = [];
Forms[this.viewing].map(field => {
if(field.required && !field.viewIf){
rules.push(ValidationRules.ensure(field.propName).required().rules[0])
}
if (field.required && field.viewIf) {
rules.push(ValidationRules.ensure(field.propName).required()
.when(val => this.currentDoc[field.viewIf] === field.viewIfValue)
.rules[0])
}
});
{
"fieldDescription": "Dashboard Link",
"fieldType": "link-field",
"propName": "dashboardLink",
"viewIf": "previousProperty",
"viewIfValue": true,
"required": true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment