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

New option "log" in enforce_extra to just log extra fields #620

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions lib/thinky.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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".
*/
Expand Down
8 changes: 8 additions & 0 deletions lib/type/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]])) {
Expand Down
25 changes: 25 additions & 0 deletions test/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down