Show:

Running Manual Validations

Although validations are lazily computed, there are times where we might want to force all or specific validations to happen. For this reason we have exposed two methods:

  • validateSync: Should only be used if all validations are synchronous. It will throw an error if any of the validations are asynchronous
  • validate: Will always return a promise and should be used if asynchronous validations are present

Inspecting Validations

All validations can be accessed via the validations object created on your model/object. Each attribute also has its own validation which has the same properties. An attribute validation can be accessed via validations.attrs.<ATTRIBUTE> which will return a ResultCollection.

Global Validations

Global validations exist on the validations object that resides on the object that is being validated. To see all possible properties, please checkout the docs for ResultCollection.

model.get('validations.isValid');
model.get('validations.errors');
model.get('validations.messages');
// etc...

Attribute Validations

The validations object also contains an attrs object which holds a ResultCollection for each attribute specified in your validation rules.

model.get('validations.attrs.username.isValid');
model.get('validations.attrs.password.errors');
model.get('validations.attrs.email.messages');
// etc...

Methods

buildValidations
(
  • validations
)
Ember.Mixin

Top level method that will ultimately return a mixin with all CP validations

Parameters:

  • validations Object

    Validation rules

Returns:

createAttrsClass
(
  • validatableAttributes
  • validationRules
  • owner
)
Ember.Object
private

Creates the attrs class which holds all the CP logic

model.get('validations.attrs.username');
model.get('validations.attrs.nested.object.attribute');

Parameters:

Returns:

createCPValidationFor
(
  • attribute
  • validations
)
Ember.computed
private

CP generator for the given attribute

Parameters:

  • attribute String
  • validations Array / Object

Returns:

Ember.computed:

A computed property which is a ValidationResultCollection

createTopLevelPropsMixin
(
  • validations
)
private

Create a mixin that will have all the top level CPs under the validations object. These are computed collections on different properties of each attribute validations CP

Parameters:

createValidationsClass
(
  • inheritedValidationsClass
  • validations
  • owner
)
Ember.Object
private

Creates the validations class that will become model.validations.

  • Setup parent validation inheritance
  • Normalize nested keys (i.e. 'details.dob') into objects (i.e { details: { dob: validator() }})
  • Merge normalized validations with parent
  • Create global CPs (i.e. 'isValid', 'messages', etc...)

Parameters:

Returns:

createValidatorsFor
(
  • attribute
  • model
)
Array
private

Create validators for the give attribute and store them in a cache

Parameters:

Returns:

debouncedValidate
(
  • validator
  • value
  • options
  • model
  • attribute
  • resolve
)
private

Debounce handler for running a validation for the specified options

Parameters:

getCPDependentKeysFor
(
  • attribute
  • validations
)
Array
private

CP dependency generator for a give attribute depending on its relationships

Parameters:

  • attribute String
  • validations Array / Object

Returns:

Array:

Unique list of dependencies

getValidatorCacheFor
(
  • attribute
  • model
)
Map
private

Get debounced validation cache for the given attribute. If it doesnt exist, create a new one.

Parameters:

Returns:

Map
getValidatorsFor
(
  • attribute
  • model
)
Array
private

Get validators for the give attribute. If they are not in the cache, then create them.

Parameters:

Returns:

lookupValidator
(
  • owner
  • type
)
Class
private

Lookup a validators of a specific type on the owner

Parameters:

  • owner Ember.Owner
  • type String

Returns:

Class:

Validator class or undefined if not found

normalizeOptions
(
  • validations
)
private

Validation rules can be created with default and global options { description: 'Username', validators: [...] } This method generate the default options pojo, applies it to each validation rule, and flattens the object

Parameters:

Returns:

validate
(
  • options
  • async
)
Promise or Object

Options

  • on (Array): Only validate the given attributes. If empty, will validate over all validatable attribute
  • excludes (Array): Exclude validation on the given attributes
model.validate({ on: ['username', 'email'] }).then(({ m, validations }) => {
  validations.get('isValid'); // true or false
  validations.get('isValidating'); // false

  let usernameValidations = m.get('validations.attrs.username');
  usernameValidations.get('isValid') // true or false
});

Parameters:

  • options Object
  • async Boolean

    If false, will get all validations and will error if an async validations is found. If true, will get all validations and wrap them in a promise hash

Returns:

Promise or Object:

Promise if async is true, object if async is false

validateSync
(
  • options
)
Object

Options

  • on (Array): Only validate the given attributes. If empty, will validate over all validatable attribute
  • excludes (Array): Exclude validation on the given attributes
const { m, validations } = model.validateSync();
validations.get('isValid') // true or false

Parameters:

Returns:

validationReturnValueHandler
(
  • attribute
  • value
  • model
)
ValidationResult
private

A handler used to create ValidationResult object from values returned from a validator

Parameters:

Returns:

ValidationResult