From a5d055fd4dd19ed2f9ab6db6a3b9da315246a426 Mon Sep 17 00:00:00 2001 From: Sergio Date: Fri, 19 May 2017 21:52:31 +1000 Subject: [PATCH] =?UTF-8?q?Adds=20an=20additional=20option=20=E2=80=9Clog?= =?UTF-8?q?=E2=80=9D=20to=20enforce=5Fextra,=20which=20will=20not=20fail?= =?UTF-8?q?=20but=20will=20log=20all=20extra=20fields.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/model.js | 2 +- lib/thinky.js | 4 ++-- lib/type/object.js | 8 ++++++++ lib/util.js | 9 +++++++++ test/schema.js | 25 +++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/model.js b/lib/model.js index 33c8d9a4..7ae9de69 100644 --- a/lib/model.js +++ b/lib/model.js @@ -377,7 +377,7 @@ Model.prototype._createIndex = function(name, fn, opts) { * options can be: * - init: Boolean (create an index or not) * - timeFormat: 'raw'/'native' - * - enforce_extra: 'strict'/'remove'/'none' + * - enforce_extra: 'strict'/'remove'/'log'/'none' * - enforce_missing: Boolean * - enforce_type: 'strict'/'loose'/'none' * - validate: 'oncreate'/'onsave' diff --git a/lib/thinky.js b/lib/thinky.js index b7b088ed..21a31244 100644 --- a/lib/thinky.js +++ b/lib/thinky.js @@ -15,7 +15,7 @@ var Errors = require(__dirname+'/errors.js'); * - `timeoutError` {number} The wait time before reconnecting in case of an error (in ms), default 1000 * - `timeoutGb` {number} How long the pool keep a connection that hasn't been used (in ms), default 60*60*1000 * - `enforce_missing` {boolean}, default `false` - * - `enforce_extra` {"strict"|"remove"|"none"}, default `"none"` + * - `enforce_extra` {"strict"|"remove"|"log"|"none"}, default `"none"` * - `enforce_type` {"strict"|"loose"|"none"}, default `"loose"` * - `timeFormat` {"raw"|"native"} * - `createDatabase` {boolean} Whether thinky should create the database or not. @@ -111,7 +111,7 @@ Thinky.prototype.getOptions = function() { * other use cases. * - `timeFormat` {"raw"|"native"} Format of ReQL dates. * - `enforce_missing` {boolean}, default `false`. - * - `enforce_extra` {"strict"|"remove"|"none"}, default `"none"`. + * - `enforce_extra` {"strict"|"remove"|"log"|"none"}, default `"none"`. * - `enforce_type` {"strict"|"loose"|"none"}, default `"loose"`. * - `validate` {"oncreate"|"onsave"}, default "onsave". */ diff --git a/lib/type/object.js b/lib/type/object.js index b55beb39..deea14a9 100644 --- a/lib/type/object.js +++ b/lib/type/object.js @@ -154,6 +154,14 @@ TypeObject.prototype.validate = function(object, prefix, options) { } }); } + else if (localOptions.enforce_extra === "log") { + util.loopKeys(object, function(object, key) { + if ((self._model === undefined || self._model._joins.hasOwnProperty(key) === false) + && (self._schema[key] === undefined)) { + util.logExtraField(prefix, key); + } + }); + } } } diff --git a/lib/util.js b/lib/util.js index f89be908..78f840fd 100644 --- a/lib/util.js +++ b/lib/util.js @@ -283,6 +283,15 @@ function extraField(prefix, key) { } util.extraField = extraField; +function logExtraField(prefix, key) { + if (prefix === '') { + console.log("Extra field `"+key+"` not allowed."); + return; + } + console.log("Extra field `"+key+"` in "+prefix+" not allowed.") +} +util.logExtraField = logExtraField; + function looseType(prefix, expected) { if ((expected.length > 0) && (vowels[expected[0]])) { diff --git a/test/schema.js b/test/schema.js index e297de17..4206f875 100644 --- a/test/schema.js +++ b/test/schema.js @@ -207,6 +207,7 @@ describe('Chainable types', function(){ return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be a string.") }); }); + it('String - basic - null and strict', function(){ var name = util.s8(); var Model = thinky.createModel(name, @@ -3087,6 +3088,30 @@ describe('validate', function(){ }); + it('Extra field - enforce_extra:"log" - global option', function(){ + var name = util.s8(); + var str = util.s8(); + + var Model = thinky.createModel(name, { + id: String, + foo: {fizz: String}, + }, {init: false, enforce_extra: 'log'}) + + doc = new Model({ + id: str, + foo: {fizz: "Hello", buzz: "OMIT"}, + bar: "OMIT" + }) + doc.validate(); + + assert.equal(true, doc.foo.hasOwnProperty('buzz')); + assert.deepEqual(doc, { + id: str, + foo: { fizz: 'Hello', buzz: "OMIT" }, + bar: "OMIT" + }); + }); + it('Test option validate="oncreate"', function(){ var name = util.s8(); var str = util.s8();