mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-20 09:22:49 +03:00
af47f88648
closes #2893, issue #2850, issue #2856 - this is a stable, but quick and dirty validations layer for the time constraints - this could be replaced with a unified server/client layer later. the infrastructure is there. - create a validation engine mixin to match validators with models - override the save method in the mixin to perform validations first - create a post validator - fixup calls to .save() to make sure they catch errors properly
70 lines
2.2 KiB
JavaScript
70 lines
2.2 KiB
JavaScript
import { getRequestErrorMessage } from 'ghost/utils/ajax';
|
|
|
|
import ValidatorExtensions from 'ghost/utils/validator-extensions';
|
|
import PostValidator from 'ghost/validators/post';
|
|
|
|
ValidatorExtensions.init();
|
|
|
|
var ValidationEngine = Ember.Mixin.create({
|
|
validators: {
|
|
post: PostValidator
|
|
},
|
|
|
|
validate: function () {
|
|
var self = this,
|
|
type = this.get('validationType'),
|
|
validator = this.get('validators.' + type);
|
|
|
|
return new Ember.RSVP.Promise(function (resolve, reject) {
|
|
if (!type || !validator) {
|
|
return reject('The validator specified, "' + type + '", did not exist!');
|
|
}
|
|
|
|
var validationErrors = validator.validate(self);
|
|
|
|
if (Ember.isEmpty(validationErrors)) {
|
|
return resolve();
|
|
}
|
|
|
|
return reject(validationErrors);
|
|
});
|
|
},
|
|
|
|
// override save to do validation first
|
|
save: function () {
|
|
var self = this,
|
|
// this is a hack, but needed for async _super calls.
|
|
// ref: https://github.com/emberjs/ember.js/pull/4301
|
|
_super = this.__nextSuper;
|
|
|
|
// If validation fails, reject with validation errors.
|
|
// If save to the server fails, reject with server response.
|
|
return this.validate().then(function () {
|
|
return _super.call(self);
|
|
}).catch(function (result) {
|
|
var message = 'There was an error saving this ' + self.get('validationType');
|
|
|
|
if (Ember.isArray(result)) {
|
|
// get validation error messages
|
|
message += ': ' + result.mapBy('message').join(' ');
|
|
} else if (typeof result === 'object') {
|
|
// Get messages from server response
|
|
message += ': ' + getRequestErrorMessage(result);
|
|
} else if (typeof result === 'string') {
|
|
message += ': ' + result;
|
|
} else {
|
|
message += '.';
|
|
}
|
|
|
|
// set format for notifications.showErrors
|
|
message = [{ message: message }];
|
|
|
|
return new Ember.RSVP.Promise(function (resolve, reject) {
|
|
reject(message);
|
|
});
|
|
});
|
|
}
|
|
});
|
|
|
|
export default ValidationEngine;
|