Skip to content

Commit

Permalink
Merge pull request #5856 from ekulabuhov/master
Browse files Browse the repository at this point in the history
Added strictBool option to schema
  • Loading branch information
vkarpov15 committed Dec 2, 2017
2 parents 5c547ad + a3fd08c commit 7dc384c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/schema/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,25 @@ SchemaBoolean.prototype.checkRequired = function(value) {
* Casts to boolean
*
* @param {Object} value
* @param {Object} model - this value is optional
* @api private
*/

SchemaBoolean.prototype.cast = function(value) {
SchemaBoolean.prototype.cast = function(value, model) {
if (value === null) {
return value;
}

if (!this.options.strictBool) {
if (this.options.strictBool || (model && model.schema.options.strictBool && this.options.strictBool !== false)) {
// strict mode (throws if value is not a boolean, instead of converting)
if (value === true || value === 'true' || value === 1 || value === '1') {
return true;
}
if (value === false || value === 'false' || value === 0 || value === '0') {
return false;
}
throw new CastError('boolean', value, this.path);
} else {
// legacy mode
if (value === '0') {
return false;
Expand All @@ -72,15 +82,6 @@ SchemaBoolean.prototype.cast = function(value) {
return false;
}
return !!value;
} else {
// strict mode (throws if value is not a boolean, instead of converting)
if (value === true || value === 'true' || value === 1 || value === '1') {
return true;
}
if (value === false || value === 'false' || value === 0 || value === '0') {
return false;
}
throw new CastError('boolean', value, this.path);
}
};

Expand Down
31 changes: 31 additions & 0 deletions test/schema.boolean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('schematype', function() {
assert.strictEqual(true, m3.b);
done();
});

it('strictBool option (gh-5211)', function(done) {
var db = start(),
s1 = new Schema({b: {type: Boolean, strictBool: true}}),
Expand All @@ -51,7 +52,37 @@ describe('schematype', function() {
}
});
});
});

it('strictBool schema option', function(done) {
var db = start(),
s1 = new Schema({b: {type: Boolean}}, {strictBool: true}),
M1 = db.model('StrictBoolTrue', s1);
db.close();

var strictValues = [true, false, 'true', 'false', 0, 1, '0', '1'];

strictValues.forEach(function(value) {
var doc = new M1;
doc.b = value;
doc.validate(function(error) {
if (error) {
// test fails as soon as one value fails
return done(error);
}
});
});

var doc = new M1;
doc.b = 'Not a boolean';
doc.validate(function(error) {
if (error) {
done();
} else {
done(new Error('ValidationError expected'));
}
});
});

});
});

0 comments on commit 7dc384c

Please sign in to comment.