Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix forceId to reject empty strings #1576

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/model-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
}

if (forceId) {
ModelClass.validatesAbsenceOf(idName, {if: 'isNewRecord'});
ModelClass.validatesAbsenceOf(idName, {if: 'isNewRecord', allowBlank: false, allowNull: false});
}

ModelClass.definition.properties[idName].updateOnly = !!forceId;
Expand Down
4 changes: 4 additions & 0 deletions lib/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ Validatable.validatesPresenceOf = getConfigurator('presence');
* @property {String} message Error message to use instead of default.
* @property {String} if Validate only if `if` exists.
* @property {String} unless Validate only if `unless` exists.
* @property {Boolean} allowBlank Allow property to be blank.
* @property {Boolean} allowNull Allow property to be null.
*/
Validatable.validatesAbsenceOf = getConfigurator('absence');

Expand Down Expand Up @@ -296,6 +298,8 @@ function validatePresence(attr, conf, err, options) {
* Absence validator
*/
function validateAbsence(attr, conf, err, options) {
if (nullCheck.call(this, attr, Object.assign({allowBlank: true, allowNull: true}, conf), err)) return;

if (!blank(this[attr])) {
err();
}
Expand Down
15 changes: 15 additions & 0 deletions test/optional-validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ describe('optional-validation', function() {
});
});
});
context('create', function() {
it('should fail with a null id', function(done) {
ModelWithForceId.create({id: null}, function(err, created) {
should.exist(err);
done();
});
});

it('should fail with an empty id', function(done) {
ModelWithForceId.create({id: ''}, function(err, created) {
should.exist(err);
done();
});
});
});
});

describe('no model setting', function() {
Expand Down
24 changes: 24 additions & 0 deletions test/validations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,30 @@ describe('validations', function() {
u.isValid().should.be.true();
});

it('should validate absence with allowBlank set to true', function() {
User.validatesAbsenceOf('foo', {allowBlank: true});
var u = new User({foo: ''});
u.isValid().should.be.true();
});

it('should validate absence with allowBlank set to false', function() {
User.validatesAbsenceOf('foo', {allowBlank: false});
var u = new User({foo: ''});
u.isValid().should.be.false();
});

it('should validate absence with allowNull set to true', function() {
User.validatesAbsenceOf('foo', {allowNull: true});
var u = new User({foo: null});
u.isValid().should.be.true();
});

it('should validate absence with allowNull set to true', function() {
User.validatesAbsenceOf('foo', {allowNull: false});
var u = new User({foo: null});
u.isValid().should.be.false();
});

describe('validate absence on update', function() {
before(function(done) {
Employee.destroyAll(function(err) {
Expand Down