A Ruby on Rails inspired model validation framework that is completely and utterly computed property based.
No observers were used nor harmed while developing and testing this addon.
- Lazily computed validations
- Ruby on rails inspired validators
- Support for both Ember Data Models and Objects
- Synchronous and asynchronous support for both validators and validations
- Dirty tracking
- Support for nested models via
belongs-to
andhasMany
relationships - No observers. Seriously... there are none. Like absolutely zero....
Integrated with Ember Data's DS.Errors APIwaiting on #3707 to be resolved- Meta data based cycle tracking to detect cycles within your model relationships which could break the CP chain
- Custom validators
- Ember CLI generator to create custom validators with a unit test
ember install ember-cp-validations
Changelog can be found here
Detailed documentation can be found here
A live demo can be found here
If it is a bug please open an issue on GitHub.
The first thing we need to do it build our validation rules. This will then generate a Mixin that you will be able to incorporate into your model or object.
// models/user.js
import Ember from 'ember';
import DS from 'ember-data';
import {
validator, buildValidations
}
from 'ember-cp-validations';
var Validations = buildValidations({
username: validator('presence', true),
password: [
validator('presence', true),
validator('length', {
min: 4,
max: 8
})
],
email: [
validator('presence', true),
validator('format', { type: 'email' })
],
emailConfirmation: [
validator('presence', true),
validator('confirmation', {
on: 'email',
message: 'do not match',
attributeDescription: 'Email addresses'
})
]
});
Once our rules are created and our Mixin is generated, all we have to do is add it to our model.
// models/user.js
export default DS.Model.extend(Validations, {
'username': attr('string'),
'password': attr('string'),
'email': attr('string')
});
You can also use the generated Validations
mixin on any Ember.Object
or child
of Ember.Object
, like Ember.Component
. For example:
// components/x-foo.js
import Ember from 'ember';
import {
validator, buildValidations
}
from 'ember-cp-validations';
var Validations = buildValidations({
bar: validator('presence', true)
});
export default Ember.Component.extend(Validations, {
bar: null
});