Created
January 13, 2016 21:21
-
-
Save qoobaa/caeed78ab3fec003b64f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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