2017-08-22 10:53:26 +03:00
|
|
|
import EmberObject from '@ember/object';
|
2016-06-11 19:52:36 +03:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
/**
|
|
|
|
* Base validator that all validators should extend
|
|
|
|
* Handles checking of individual properties or the entire model
|
|
|
|
*/
|
2016-06-11 19:52:36 +03:00
|
|
|
export default EmberObject.extend({
|
2015-06-07 06:19:19 +03:00
|
|
|
passed: false,
|
|
|
|
|
2017-11-24 21:53:19 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.properties = this.properties || [];
|
|
|
|
},
|
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
/**
|
|
|
|
* When passed a model and (optionally) a property name,
|
|
|
|
* checks it against a list of validation functions
|
|
|
|
* @param {Ember.Object} model Model to validate
|
|
|
|
* @param {string} prop Property name to check
|
|
|
|
* @return {boolean} True if the model passed all (or one) validation(s),
|
|
|
|
* false if not
|
|
|
|
*/
|
2015-10-28 14:36:45 +03:00
|
|
|
check(model, prop) {
|
2015-06-07 06:19:19 +03:00
|
|
|
this.set('passed', true);
|
|
|
|
|
|
|
|
if (prop && this[prop]) {
|
|
|
|
this[prop](model);
|
|
|
|
} else {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.properties.forEach((property) => {
|
2015-10-28 14:36:45 +03:00
|
|
|
if (this[property]) {
|
|
|
|
this[property](model);
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.passed;
|
2015-06-07 06:19:19 +03:00
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
invalidate() {
|
2015-06-07 06:19:19 +03:00
|
|
|
this.set('passed', false);
|
|
|
|
}
|
|
|
|
});
|