0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-13 22:41:32 -05:00

Merge pull request #6532 from jaswilli/issue-6491

Provide entire model to validator
This commit is contained in:
Hannah Wolfe 2016-02-18 09:12:06 +00:00
commit cdbec7a8ba
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

@ -355,6 +355,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;
@ -378,6 +394,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;