Skip to content

ameliadowns/ember-cp-validations

 
 

Repository files navigation

Ember CP Validations

Gitter Build Status npm version Code Climate Test Coverage Dependency Status devDependency Status

A Ruby on Rails inspired model validation framework that is completely and utterly computed property based.

Features

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 and hasMany relationships
  • No observers. Seriously... there are none. Like absolutely zero....
  • Integrated with Ember Data's DS.Errors API waiting 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

Installation

ember install ember-cp-validations

Changelog

Changelog can be found here

Documentation

Detailed documentation can be found here

Live Demo

A live demo can be found here

Looking for help?

If it is a bug please open an issue on GitHub.

Basic Usage

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
});

About

Ember computed property based validations

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HTML 52.2%
  • JavaScript 44.0%
  • CSS 3.8%