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';
|
|
|
|
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';
|
|
|
|
import SlackIntegrationValidator from 'ghost-admin/validators/slack-integration';
|
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';
|
|
|
|
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.
|
2014-06-21 01:36:44 +04:00
|
|
|
validators: {
|
2020-01-30 18:35:36 +03:00
|
|
|
customView: CustomViewValidator,
|
2016-04-15 17:45:50 +03:00
|
|
|
inviteUser: InviteUserValidator,
|
|
|
|
navItem: NavItemValidator,
|
2014-06-24 04:47:51 +04:00
|
|
|
post: PostValidator,
|
2016-04-15 17:45:50 +03:00
|
|
|
reset: ResetValidator,
|
|
|
|
setting: SettingValidator,
|
2014-06-25 16:12:48 +04:00
|
|
|
setup: SetupValidator,
|
2014-06-24 04:47:51 +04:00
|
|
|
signin: SigninValidator,
|
2016-04-15 17:45:50 +03:00
|
|
|
signup: SignupValidator,
|
|
|
|
slackIntegration: SlackIntegrationValidator,
|
2015-09-16 20:02:06 +03:00
|
|
|
tag: TagSettingsValidator,
|
2018-10-18 02:18:29 +03:00
|
|
|
user: UserValidator,
|
2019-11-28 14:30:21 +03:00
|
|
|
member: MemberValidator,
|
2018-10-18 02:18:29 +03:00
|
|
|
integration: IntegrationValidator,
|
2020-02-14 12:34:01 +03:00
|
|
|
webhook: WebhookValidator,
|
2020-10-15 20:03:18 +03:00
|
|
|
label: LabelValidator,
|
|
|
|
snippet: SnippetValidator
|
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());
|
|
|
|
},
|
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
|
|
|
}
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
type = this.validationType || model.get('validationType');
|
2015-10-28 14:36:45 +03:00
|
|
|
validator = this.get(`validators.${type}`) || model.get(`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);
|
2015-07-11 20:04:19 +03:00
|
|
|
model.get('errors').remove(opts.property);
|
|
|
|
} else {
|
|
|
|
model.get('errors').clear();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|