From e8057f1eb10c2cb3ea4637f8676085782e71a77f Mon Sep 17 00:00:00 2001 From: c0d0g3n Date: Fri, 9 Jun 2017 21:45:44 +0200 Subject: [PATCH 1/3] schema type boolean: strictBool option added --- lib/schema/boolean.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/schema/boolean.js b/lib/schema/boolean.js index 5951dd0513d..c4b437b40fa 100644 --- a/lib/schema/boolean.js +++ b/lib/schema/boolean.js @@ -58,16 +58,29 @@ SchemaBoolean.prototype.cast = function(value) { if (value === null) { return value; } - if (value === '0') { - return false; - } - if (value === 'true') { - return true; - } - if (value === 'false') { - return false; + + if (!this.options.strictBool) { + // legacy mode + if (value === '0') { + return false; + } + if (value === 'true') { + return true; + } + if (value === 'false') { + 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); } - return !!value; }; SchemaBoolean.$conditionalHandlers = From 0fb09d98694b9a58231e8a87befb8134e8e26c6b Mon Sep 17 00:00:00 2001 From: c0d0g3n Date: Fri, 9 Jun 2017 21:57:14 +0200 Subject: [PATCH 2/3] schema type boolean: strictBool test added --- test/schema.boolean.test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/schema.boolean.test.js b/test/schema.boolean.test.js index 37b93db9ba4..6b96a28142d 100644 --- a/test/schema.boolean.test.js +++ b/test/schema.boolean.test.js @@ -29,5 +29,21 @@ describe('schematype', function() { assert.strictEqual(true, m3.b); done(); }); + it('strictBool option (gh-5211)', function() { + console.log('chekc'); + var db = start(), + s1 = new Schema({b: {type: Boolean, strictBool: true}}), + M1 = db.model('StrictBoolTrue', s1); + db.close(); + + var m1 = new M1; + var strictValues = [true, false, 'true', 'false', 0, 1, '0', '1']; + var validatePromises = strictValues.map(function(value) { + m1.b = value; + return m1.validate(); + }); + + return global.Promise.all(validatePromises); + }); }); }); From aa6d05af5c1d907ba4181be993e10baf612ec755 Mon Sep 17 00:00:00 2001 From: c0d0g3n Date: Sun, 18 Jun 2017 20:01:54 +0200 Subject: [PATCH 3/3] strictBool test rewritten to support Node < 4 --- test/schema.boolean.test.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/test/schema.boolean.test.js b/test/schema.boolean.test.js index 6b96a28142d..acf05f5bb8b 100644 --- a/test/schema.boolean.test.js +++ b/test/schema.boolean.test.js @@ -29,21 +29,29 @@ describe('schematype', function() { assert.strictEqual(true, m3.b); done(); }); - it('strictBool option (gh-5211)', function() { - console.log('chekc'); + it('strictBool option (gh-5211)', function(done) { var db = start(), s1 = new Schema({b: {type: Boolean, strictBool: true}}), M1 = db.model('StrictBoolTrue', s1); db.close(); - var m1 = new M1; var strictValues = [true, false, 'true', 'false', 0, 1, '0', '1']; - var validatePromises = strictValues.map(function(value) { - m1.b = value; - return m1.validate(); + + var testsRemaining = strictValues.length; + 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) + } + if (!--testsRemaining) { + return done() + } + }); }); - return global.Promise.all(validatePromises); }); }); });