Skip to content
Permalink
Browse files

Provide entire model to validator

Closes #6491
  • Loading branch information...
jaswilli committed Feb 17, 2016
1 parent 2166c37 commit b10da0569a31099e981d8b2dffa7ad701689c3a9
Showing with 37 additions and 2 deletions.
  1. +9 −2 core/server/models/user.js
  2. +28 −0 core/test/integration/model/model_users_spec.js
@@ -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
@@ -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;

0 comments on commit b10da05

Please sign in to comment.
You can’t perform that action at this time.