ember-cp-validations
Version:
Ember computed property based validation library
101 lines (95 loc) • 2.46 kB
JavaScript
import Base from 'ember-cp-validations/validators/base';
import { isPromise } from 'ember-cp-validations/utils/utils';
/**
* <i class="fa fa-hand-o-right" aria-hidden="true"></i> [See All Options](#method_validate)
*
* Identifies a `belongs-to` relationship in an Ember Data Model or EmberObject.
* This is used to create a link to the validations object of the child model.
*
* _**Note:** Validations must exist on **both** models/objects_
*
* ### Ember Model
*
* ```javascript
* // model/users.js
*
* const Validations = buildValidations({
* details: validator('belongs-to')
* });
*
* export default Model.extend(Validations, {
* 'details': belongsTo('user-detail')
* });
* ```
*
* ```javascript
* // model/user-details.js
*
* const Validations = buildValidations({
* firstName: validator('presence', true),
* lastName: validator('presence', true)
* });
*
* export default Model.extend(Validations, {
* "firstName": attr('string'),
* "lastName": attr('string'),
* });
* ```
*
* ### Ember Object
*
* ```javascript
* // model/users.js
*
* import { getOwner } from '@ember/application';
* import EmberObject from '@ember/object';
* import UserDetails from '../user-details';
*
* const Validations = buildValidations({
* details: validator('belongs-to')
* });
*
* export default EmberObject.extend(Validations, {
* details: null,
*
* init() {
* this._super(...arguments);
* let owner = getOwner(this);
* this.set('details', UserDetails.create(owner.ownerInjection()));
* }
* });
* ```
*
* From our `user` model, we can now check any validation property on the `user-details` model.
*
* ```javascript
* get(model, 'validations.attrs.details.isValid')
* get(model, 'validations.attrs.details.messages')
* ```
*
* @class Belongs To
* @module Validators
* @extends Base
*/
const BelongsTo = Base.extend({
validate(value, ...args) {
if (value) {
if (isPromise(value)) {
return value.then((model) => this.validate(model, ...args));
}
return value.validations;
}
return true;
},
});
BelongsTo.reopenClass({
getDependentsFor(attribute) {
return [
`model.${attribute}.isDeleted`,
`model.${attribute}.content.isDeleted`,
`model.${attribute}.validations`,
`model.${attribute}.content.validations`,
];
},
});
export default BelongsTo;