2020-01-16 20:01:12 +03:00
|
|
|
// TODO: remove usage of Ember Data's private `Errors` class when refactoring validations
|
|
|
|
// eslint-disable-next-line
|
2020-01-30 18:35:36 +03:00
|
|
|
import CustomViewValidator from 'ghost-admin/validators/custom-view';
|
|
|
|
import DS from 'ember-data'; // eslint-disable-line
|
2018-10-18 02:18:29 +03:00
|
|
|
import IntegrationValidator from 'ghost-admin/validators/integration';
|
2016-05-24 15:06:59 +03:00
|
|
|
import InviteUserValidator from 'ghost-admin/validators/invite-user';
|
2020-02-14 12:34:01 +03:00
|
|
|
import LabelValidator from 'ghost-admin/validators/label';
|
2019-11-28 14:30:21 +03:00
|
|
|
import MemberValidator from 'ghost-admin/validators/member';
|
2017-08-22 10:53:26 +03:00
|
|
|
import Mixin from '@ember/object/mixin';
|
2020-01-16 20:01:12 +03:00
|
|
|
import Model from '@ember-data/model';
|
2016-05-24 15:06:59 +03:00
|
|
|
import NavItemValidator from 'ghost-admin/validators/nav-item';
|
2022-04-04 21:26:49 +03:00
|
|
|
import NewsletterValidator from 'ghost-admin/validators/newsletter';
|
2021-10-06 17:29:15 +03:00
|
|
|
import OfferValidator from 'ghost-admin/validators/offer';
|
2016-05-24 15:06:59 +03:00
|
|
|
import PostValidator from 'ghost-admin/validators/post';
|
2017-05-29 21:50:03 +03:00
|
|
|
import RSVP from 'rsvp';
|
2016-05-24 15:06:59 +03:00
|
|
|
import ResetValidator from 'ghost-admin/validators/reset';
|
|
|
|
import SettingValidator from 'ghost-admin/validators/setting';
|
|
|
|
import SetupValidator from 'ghost-admin/validators/setup';
|
|
|
|
import SigninValidator from 'ghost-admin/validators/signin';
|
|
|
|
import SignupValidator from 'ghost-admin/validators/signup';
|
2020-10-15 20:03:18 +03:00
|
|
|
import SnippetValidator from 'ghost-admin/validators/snippet';
|
2016-05-24 15:06:59 +03:00
|
|
|
import TagSettingsValidator from 'ghost-admin/validators/tag-settings';
|
2022-05-11 20:11:54 +03:00
|
|
|
import TierBenefitItemValidator from 'ghost-admin/validators/tier-benefit-item';
|
|
|
|
import TierValidator from 'ghost-admin/validators/tier';
|
2016-05-24 15:06:59 +03:00
|
|
|
import UserValidator from 'ghost-admin/validators/user';
|
2018-10-18 02:18:29 +03:00
|
|
|
import WebhookValidator from 'ghost-admin/validators/webhook';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {A as emberA, isArray as isEmberArray} from '@ember/array';
|
2014-06-21 01:36:44 +04:00
|
|
|
|
2016-01-12 22:23:01 +03:00
|
|
|
const {Errors} = DS;
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2014-06-30 07:43:25 +04:00
|
|
|
/**
|
|
|
|
* The class that gets this mixin will receive these properties and functions.
|
|
|
|
* It will be able to validate any properties on itself (or the model it passes to validate())
|
|
|
|
* with the use of a declared validator.
|
|
|
|
*/
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Mixin.create({
|
2014-06-30 07:43:25 +04:00
|
|
|
// these validators can be passed a model to validate when the class that
|
|
|
|
// mixes in the ValidationEngine declares a validationType equal to a key on this object.
|
|
|
|
// the model is either passed in via `this.validate({ model: object })`
|
|
|
|
// or by calling `this.validate()` without the model property.
|
|
|
|
// in that case the model will be the class that the ValidationEngine
|
|
|
|
// was mixed into, i.e. the controller or Ember Data model.
|
2021-07-15 17:27:29 +03:00
|
|
|
validators: null,
|
2014-06-21 01:36:44 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
// This adds the Errors object to the validation engine, and shouldn't affect
|
|
|
|
// ember-data models because they essentially use the same thing
|
2016-04-25 12:54:36 +03:00
|
|
|
errors: null,
|
2015-06-07 06:19:19 +03:00
|
|
|
|
2015-08-27 23:28:41 +03:00
|
|
|
// Store whether a property has been validated yet, so that we know whether or not
|
|
|
|
// to show error / success validation for a field
|
2016-04-25 12:54:36 +03:00
|
|
|
hasValidated: null,
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.set('errors', Errors.create());
|
|
|
|
this.set('hasValidated', emberA());
|
2021-07-15 17:27:29 +03:00
|
|
|
|
|
|
|
this.validators = {
|
|
|
|
customView: CustomViewValidator,
|
|
|
|
inviteUser: InviteUserValidator,
|
|
|
|
navItem: NavItemValidator,
|
2022-05-11 20:11:54 +03:00
|
|
|
tierBenefitItem: TierBenefitItemValidator,
|
2021-07-15 17:27:29 +03:00
|
|
|
post: PostValidator,
|
|
|
|
reset: ResetValidator,
|
|
|
|
setting: SettingValidator,
|
|
|
|
setup: SetupValidator,
|
|
|
|
signin: SigninValidator,
|
|
|
|
signup: SignupValidator,
|
|
|
|
tag: TagSettingsValidator,
|
|
|
|
user: UserValidator,
|
|
|
|
member: MemberValidator,
|
|
|
|
integration: IntegrationValidator,
|
|
|
|
webhook: WebhookValidator,
|
|
|
|
label: LabelValidator,
|
|
|
|
snippet: SnippetValidator,
|
2022-05-11 20:11:54 +03:00
|
|
|
tier: TierValidator,
|
2022-04-04 21:26:49 +03:00
|
|
|
offer: OfferValidator,
|
|
|
|
newsletter: NewsletterValidator
|
2021-07-15 17:27:29 +03:00
|
|
|
};
|
2016-04-25 12:54:36 +03:00
|
|
|
},
|
2015-08-27 23:28:41 +03:00
|
|
|
|
2014-06-30 07:43:25 +04:00
|
|
|
/**
|
2015-03-15 00:50:00 +03:00
|
|
|
* Passes the model to the validator specified by validationType.
|
2014-06-30 07:43:25 +04:00
|
|
|
* Returns a promise that will resolve if validation succeeds, and reject if not.
|
|
|
|
* Some options can be specified:
|
|
|
|
*
|
|
|
|
* `model: Object` - you can specify the model to be validated, rather than pass the default value of `this`,
|
|
|
|
* the class that mixes in this mixin.
|
2015-06-07 06:19:19 +03:00
|
|
|
*
|
|
|
|
* `property: String` - you can specify a specific property to validate. If
|
|
|
|
* no property is specified, the entire model will be
|
|
|
|
* validated
|
2014-06-30 07:43:25 +04:00
|
|
|
*/
|
2015-10-28 14:36:45 +03:00
|
|
|
validate(opts) {
|
|
|
|
let model = this;
|
|
|
|
let hasValidated,
|
2015-01-12 06:15:13 +03:00
|
|
|
type,
|
2015-10-28 14:36:45 +03:00
|
|
|
validator;
|
|
|
|
|
|
|
|
opts = opts || {};
|
2015-01-12 06:15:13 +03:00
|
|
|
|
|
|
|
if (opts.model) {
|
|
|
|
model = opts.model;
|
2015-10-28 14:36:45 +03:00
|
|
|
} else if (this instanceof Model) {
|
2015-01-12 06:15:13 +03:00
|
|
|
model = this;
|
2019-03-06 16:53:54 +03:00
|
|
|
} else if (this.model) {
|
|
|
|
model = this.model;
|
2015-01-12 06:15:13 +03:00
|
|
|
}
|
|
|
|
|
2022-10-07 22:13:42 +03:00
|
|
|
type = this.validationType || model.validationType;
|
|
|
|
validator = this.get(`validators.${type}`) || model.validators[type];
|
2019-03-06 16:53:54 +03:00
|
|
|
hasValidated = this.hasValidated;
|
2015-01-12 06:15:13 +03:00
|
|
|
|
2014-06-30 07:43:25 +04:00
|
|
|
opts.validationType = type;
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return new RSVP.Promise((resolve, reject) => {
|
|
|
|
let passed;
|
2014-06-30 07:43:25 +04:00
|
|
|
|
2014-06-21 01:36:44 +04:00
|
|
|
if (!type || !validator) {
|
2015-10-28 14:36:45 +03:00
|
|
|
return reject([`The validator specified, "${type}", did not exist!`]);
|
2014-06-21 01:36:44 +04:00
|
|
|
}
|
|
|
|
|
2015-07-11 20:04:19 +03:00
|
|
|
if (opts.property) {
|
2015-08-27 23:28:41 +03:00
|
|
|
// If property isn't in `hasValidated`, add it to mark that this field can show a validation result
|
|
|
|
hasValidated.addObject(opts.property);
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.remove(opts.property);
|
2015-07-11 20:04:19 +03:00
|
|
|
} else {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.clear();
|
2015-07-11 20:04:19 +03:00
|
|
|
}
|
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
passed = validator.check(model, opts.property);
|
2014-06-21 01:36:44 +04:00
|
|
|
|
2015-07-11 20:04:19 +03:00
|
|
|
return (passed) ? resolve() : reject();
|
2014-06-21 01:36:44 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-06-30 07:43:25 +04:00
|
|
|
/**
|
|
|
|
* The primary goal of this method is to override the `save` method on Ember Data models.
|
|
|
|
* This allows us to run validation before actually trying to save the model to the server.
|
|
|
|
* You can supply options to be passed into the `validate` method, since the ED `save` method takes no options.
|
|
|
|
*/
|
2015-10-28 14:36:45 +03:00
|
|
|
save(options) {
|
2015-11-30 21:23:47 +03:00
|
|
|
let {_super} = this;
|
2014-06-21 01:36:44 +04:00
|
|
|
|
2014-07-06 00:44:19 +04:00
|
|
|
options = options || {};
|
|
|
|
options.wasSave = true;
|
2014-06-30 07:43:25 +04:00
|
|
|
|
2014-06-22 01:59:12 +04:00
|
|
|
// model.destroyRecord() calls model.save() behind the scenes.
|
2014-06-30 07:43:25 +04:00
|
|
|
// in that case, we don't need validation checks or error propagation,
|
|
|
|
// because the model itself is being destroyed.
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.isDeleted) {
|
2015-10-28 14:36:45 +03:00
|
|
|
return this._super(...arguments);
|
2014-06-22 01:59:12 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 01:36:44 +04:00
|
|
|
// If validation fails, reject with validation errors.
|
|
|
|
// If save to the server fails, reject with server response.
|
2015-10-28 14:36:45 +03:00
|
|
|
return this.validate(options).then(() => {
|
2017-04-11 16:39:45 +03:00
|
|
|
if (typeof this.beforeSave === 'function') {
|
|
|
|
this.beforeSave();
|
|
|
|
}
|
2015-10-28 14:36:45 +03:00
|
|
|
return _super.call(this, options);
|
|
|
|
}).catch((result) => {
|
2015-07-07 20:14:23 +03:00
|
|
|
// server save failed or validator type doesn't exist
|
2016-06-30 13:21:47 +03:00
|
|
|
if (result && !isEmberArray(result)) {
|
2016-06-30 17:45:02 +03:00
|
|
|
throw result;
|
2014-06-21 01:36:44 +04:00
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return RSVP.reject(result);
|
2014-06-21 01:36:44 +04:00
|
|
|
});
|
2015-06-07 06:19:19 +03:00
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
validate(property) {
|
|
|
|
this.validate({property});
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
2014-06-21 01:36:44 +04:00
|
|
|
}
|
|
|
|
});
|