Skip to content

Instantly share code, notes, and snippets.

@qoobaa
Created January 13, 2016 21:21
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 qoobaa/caeed78ab3fec003b64f to your computer and use it in GitHub Desktop.
Save qoobaa/caeed78ab3fec003b64f to your computer and use it in GitHub Desktop.
import Ember from "ember";
const Proxy = Ember.Object.extend({
init() {
this.set("errors", []);
},
isEmpty() {
const value = this.get("value");
const result = value === null && value === undefined && value === "";
if (!result) {
this.errors.pushObject("empty");
}
return result;
},
isFilled() {
const value = this.get("value");
const result = value !== null && value !== undefined && value !== "";
if (!result) {
this.errors.pushObject("filled");
}
return result;
},
isInteger() {
const value = this.get("value");
const result = Number.isInteger(result);
if (!result) {
this.errors.pushObject("integer");
}
return result;
}
});
export default Ember.Object.extend({
companyName: "",
validate() {
let isValid = true;
const validations = this.get("validations");
this.set("errors", Ember.Object.create());
Object.keys(validations).forEach((attributeName) => {
const validator = validations[attributeName];
const value = this.get(attributeName);
const proxy = Proxy.create({ value });
if (!validator.apply(this, [proxy])) {
isValid = false;
this.set(`errors.${attributeName}`, proxy.get("errors"));
}
});
return isValid;
},
validations: {
companyName: (companyName) => companyName.isFilled()
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment