This is now deprecated and does not work with ember >2.11
- Recommend to use ember-changeset and ember-changeset-validations instead.
ember-cli-ohmyval
A small validation mixin script I am using that work (even with Ember Data).
Motivation
There is a few validation packages around but non of them works well with Ember Data. Mainly because it is using 'isValid' which Ember data uses. So I made this for the few Ember projects I am working on.
Installation
npm install ember-cli-ohmyval --save-dev
Then import where you need it.
import VlidationMixin from 'ohmyval/mixins/ohmyval'
Validations
Presence:
{ var userObj = EmberObject; var objInst = userObj; var validationResult = objInst; objInst; //false validationResult // array of {failed: true, message: 'This field cannot be blank', field: 'name'} }}
Email:
{ var userObj = EmberObject; var objInst = userObj; var validationResult = objInst; objInst; //false validationResult // array of {failed: true, message: 'This is not a valid email', field: 'email'} }
Numericality
{ var userObj = EmberObject; var objInst = userObj; var validationResult = objInst; objInst; //false validationResult // array of {failed: true, message: 'This only allow numbers', field: 'phoneNumber'} }
Numericality (greater than)
{ var basketObj = EmberObject; var objInst = basketObj; var validationResult = objInst; objInst; //false validationResult // array of {failed: true, message: 'Cannot be larger then 100', field: 'discount'} }
Numericality (less than)
{ var basketObj = EmberObject; var objInst = basketObj; var validationResult = objInst; objInst; //false validationResult // array of {failed: true, message: 'Cannot be less then 50', field: 'discount'} }
Numericality (range)
{ var basketObj = EmberObject; var objInst = basketObj; var validationResult = objInst; objInst; //false validationResult // array of {failed: true, message: 'Cannot be less then 50', field: 'discount'} objInst; validationResult = objInst; objInst; //false validationResult // array of {failed: true, message: 'Cannot be greater then 100', field: 'discount'} }
Inclusion (simple list)
var simpleList = 'UK' 'US' 'HK'; { var userObj = EmberObject; var objInst = userObj; var validationResult = objInst; objInst; //false validationResult // array of {failed: true, message: 'is not included in the list', field: 'country'} }
Inclusion (complex list)
var complexList = Em; complexList; complexList; complexList; { var userObj = EmberObject; var objInst = userObj; var validationResult = objInst; objInst; //false validationResult // array of {failed: true, message: 'is not included in the list', field: 'email'} }
Custom Error Message
var userObj = EmberObject;
Next Steps
- More tests.
- Add more validation
- Make it easily extendable with custom validation