Factory
addon/validations/factory.js:44
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
Top level method that will ultimately return a mixin with all CP validations
Parameters:
-
validations
ObjectValidation rules
Returns:
createAttrsClass
-
validatableAttributes
-
validationRules
-
owner
Creates the attrs
class which holds all the CP logic
model.get('validations.attrs.username');
model.get('validations.attrs.nested.object.attribute');
Returns:
createCPValidationFor
-
attribute
-
validations
CP generator for the given attribute
Parameters:
-
attribute
String -
validations
Array / Object
Returns:
A computed property which is a ValidationResultCollection
createTopLevelPropsMixin
-
validations
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:
-
validations
Object
createValidationsClass
-
inheritedValidationsClass
-
validations
-
owner
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...)
Returns:
createValidatorsFor
-
attribute
-
model
Create validators for the give attribute and store them in a cache
Returns:
debouncedValidate
-
validator
-
value
-
options
-
model
-
attribute
-
resolve
Debounce handler for running a validation for the specified options
getCPDependentKeysFor
-
attribute
-
validations
CP dependency generator for a give attribute depending on its relationships
Parameters:
-
attribute
String -
validations
Array / Object
Returns:
Unique list of dependencies
getValidatorCacheFor
-
attribute
-
model
Get debounced validation cache for the given attribute. If it doesnt exist, create a new one.
Returns:
getValidatorsFor
-
attribute
-
model
Get validators for the give attribute. If they are not in the cache, then create them.
Returns:
lookupValidator
-
owner
-
type
Lookup a validators of a specific type on the owner
Parameters:
-
owner
Ember.Owner -
type
String
Returns:
Validator class or undefined if not found
normalizeOptions
-
validations
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:
-
validations
Object
Returns:
validate
-
options
-
async
Options
on
(Array): Only validate the given attributes. If empty, will validate over all validatable attributeexcludes
(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:
Returns:
Promise if async is true, object if async is false
validateSync
-
options
Options
on
(Array): Only validate the given attributes. If empty, will validate over all validatable attributeexcludes
(Array): Exclude validation on the given attributes
const { m, validations } = model.validateSync();
validations.get('isValid') // true or false
Parameters:
-
options
Object