0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-15 03:01:37 -05:00

Provide entire model to validator

Closes #6491
This commit is contained in:
Jason Williams 2016-02-17 17:02:16 -06:00
parent 2166c378ed
commit b10da0569a
2 changed files with 37 additions and 2 deletions

View file

@ -96,11 +96,18 @@ User = ghostBookshelf.Model.extend({
// This is used to bypass validation during the credential check, and must never be done with user-provided data
// Should be removed when #3691 is done
validate: function validate() {
var opts = arguments[1];
var opts = arguments[1],
userData;
if (opts && _.has(opts, 'validate') && opts.validate === false) {
return;
}
return validation.validateSchema(this.tableName, this.toJSON());
// use the base toJSON since this model's overridden toJSON
// removes fields and we want everything to run through the validator.
userData = ghostBookshelf.Model.prototype.toJSON.call(this);
return validation.validateSchema(this.tableName, userData);
},
// Get the user from the options object

View file

@ -356,6 +356,22 @@ describe('User Model', function run() {
}).catch(done);
});
it('can NOT add active user with invalid email address', function (done) {
var userData = _.clone(testUtils.DataGenerator.forModel.users[4]);
userData.email = 'invalidemailaddress';
RoleModel.findOne().then(function (role) {
userData.roles = [role.toJSON()];
return UserModel.add(userData, _.extend({}, context, {include: ['roles']}));
}).then(function () {
done(new Error('User was created with an invalid email address'));
}).catch(function () {
done();
});
});
it('can edit active user', function (done) {
var firstUser = 1;
@ -379,6 +395,18 @@ describe('User Model', function run() {
}).catch(done);
});
it('can NOT set an invalid email address', function (done) {
var firstUser = 1;
UserModel.findOne({id: firstUser}).then(function (user) {
return user.edit({email: 'notanemailaddress'});
}).then(function () {
done(new Error('Invalid email address was accepted'));
}).catch(function () {
done();
});
});
it('can edit invited user', function (done) {
var userData = testUtils.DataGenerator.forModel.users[4],
userId;