From 3bf7ec99b8f6b2fa15f829a88241db0e134570d7 Mon Sep 17 00:00:00 2001 From: Eric Chaves Date: Sun, 23 Dec 2012 12:41:16 -0200 Subject: [PATCH 1/4] adding default values int object when not present --- lib/revalidator.js | 20 +++++++++++++++++--- test/validator-test.js | 23 ++++++++++++++++++++++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/lib/revalidator.js b/lib/revalidator.js index 1faa25f..9bc1a15 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -26,7 +26,7 @@ // ({}) in this case. // function validate(object, schema, options) { - options = mixin({}, options, validate.defaults); + options = mixin({}, validate.defaults, options); var errors = []; validateObject(object, schema, options, errors); @@ -76,7 +76,16 @@ * Default: true *

*/ - validateFormatExtensions: true + validateFormatExtensions: true, + /** + *

+ * When {@link #addMissingDefaults} is true, + * if property is missing and it has a default value it will be added to the object. + *

+ * Default: false + *

+ */ + addMissingDefaults: false }; /** @@ -230,7 +239,12 @@ } if (value === undefined) { - if (schema.required && schema.type !== 'any') { + if(schema.default !== undefined && options.addMissingDefaults){ + if (typeof schema.default === 'function') + object[property] = value = schema.default(); + else + object[property] = value = schema.default; + }else if (schema.required && schema.type !== 'any') { return error('required', property, undefined, schema, errors); } else { return; diff --git a/test/validator-test.js b/test/validator-test.js index 0d2590d..dcbfece 100644 --- a/test/validator-test.js +++ b/test/validator-test.js @@ -231,7 +231,10 @@ vows.describe('revalidator', { category: { type: 'string' }, palindrome: {type: 'string', conform: function(val) { return val == val.split("").reverse().join(""); } - } + }, + printed_at: { type: 'string', default: Date}, + printed_copies: { type: 'number', default: 100}, + shipped_copies: { type: 'number', default: function(){return 50}}, }, patternProperties: { '^_': { @@ -273,6 +276,24 @@ vows.describe('revalidator', { "and an error concerning the 'required' attribute": assertHasError('required'), "and the error message defined": assertHasErrorMsg('required', "is essential for survival") }, + "and if it has a missing default property": { + topic: function (object, schema) { + var duble = clone(object); + delete duble.printed_copies; + delete duble.printed_at; + delete duble.shipped_copies; + return { + valid: revalidator.validate(duble, schema, { addMissingDefaults: true }).valid, + target: duble + } + }, + "add missing properties to object with default values": function(res){ + assert.ok(res.valid); + assert.equal(res.target.printed_copies, 100); + //assert.ok(typeof res.target.printed_at === string); + assert.equal(res.target.shipped_copies, 50); + } + }, "and if it has a missing non-required property": { topic: function (object, schema) { object = clone(object); From e7cfe6c0acb4bf22fffcabafc36c243ece9100f4 Mon Sep 17 00:00:00 2001 From: Eric Chaves Date: Tue, 25 Dec 2012 12:42:26 -0200 Subject: [PATCH 2/4] added deleteUnknowProperties to options --- lib/revalidator.js | 17 ++++++++++++++++- test/validator-test.js | 14 +++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/revalidator.js b/lib/revalidator.js index 9bc1a15..7487e55 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -85,7 +85,16 @@ * Default: false *

*/ - addMissingDefaults: false + addMissingDefaults: false, + /** + *

+ * When {@link #deleteUnknowProperties} is true, + * if property is not declared in schema it is deleted from object. + *

+ * Default: false + *

+ */ + deleteUnknowProperties: false }; /** @@ -210,6 +219,12 @@ return -1 === visitedProps.indexOf(k); }); + // deleteUnknowProperties + if(options.deleteUnknowProperties) + unvisitedProps.forEach(function(k){ + delete object[k]; + }); + // Prevent additional properties; each unvisited property is therefore an error if (schema.additionalProperties === false && unvisitedProps.length > 0) { for (i = 0, l = unvisitedProps.length; i < l; i++) { diff --git a/test/validator-test.js b/test/validator-test.js index dcbfece..6d654c5 100644 --- a/test/validator-test.js +++ b/test/validator-test.js @@ -290,10 +290,22 @@ vows.describe('revalidator', { "add missing properties to object with default values": function(res){ assert.ok(res.valid); assert.equal(res.target.printed_copies, 100); - //assert.ok(typeof res.target.printed_at === string); assert.equal(res.target.shipped_copies, 50); } }, + "and if it has properties not declared in schema":{ + topic: function(object, schema){ + var duble = clone(object); + duble.not_here = 'ohno'; + return { + valid: revalidator.validate(duble, schema, { deleteUnknowProperties: true }).valid, + target: duble + } + }, + "it should be removed from object when using strictSchema true": function(res){ + assert.ok(res.target['not_here'], undefined); + } + }, "and if it has a missing non-required property": { topic: function (object, schema) { object = clone(object); From 950831cefc738403ee056de18a9149cd9b26af4e Mon Sep 17 00:00:00 2001 From: Eric Chaves Date: Thu, 27 Dec 2012 18:32:57 -0200 Subject: [PATCH 3/4] fixed deleteUnknowProperties bug --- lib/revalidator.js | 15 ++++++++++----- test/validator-test.js | 3 ++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/revalidator.js b/lib/revalidator.js index 7487e55..b33a4b0 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -27,6 +27,7 @@ // function validate(object, schema, options) { options = mixin({}, validate.defaults, options); + var errors = []; validateObject(object, schema, options, errors); @@ -211,6 +212,15 @@ } } + // deleteUnknowProperties + if(options.deleteUnknowProperties){ + props = Object.keys(schema.patternProperties).concat(Object.keys(schema.properties)); + for (var p in object){ + if (props.indexOf(p) === -1) + delete object[p]; + } + }; + // see 5.4 if (undefined !== schema.additionalProperties) { var i, l; @@ -219,11 +229,6 @@ return -1 === visitedProps.indexOf(k); }); - // deleteUnknowProperties - if(options.deleteUnknowProperties) - unvisitedProps.forEach(function(k){ - delete object[k]; - }); // Prevent additional properties; each unvisited property is therefore an error if (schema.additionalProperties === false && unvisitedProps.length > 0) { diff --git a/test/validator-test.js b/test/validator-test.js index 6d654c5..e6e28eb 100644 --- a/test/validator-test.js +++ b/test/validator-test.js @@ -303,7 +303,8 @@ vows.describe('revalidator', { } }, "it should be removed from object when using strictSchema true": function(res){ - assert.ok(res.target['not_here'], undefined); + assert.equal(res.target['not_here'], null); + assert.ok(res.valid); } }, "and if it has a missing non-required property": { From 768947e0459baa05f2af4096c50ff6e9ed8454f7 Mon Sep 17 00:00:00 2001 From: Eric Chaves Date: Thu, 27 Dec 2012 18:41:40 -0200 Subject: [PATCH 4/4] fixed deleteUnknowProperties bug --- lib/revalidator.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/revalidator.js b/lib/revalidator.js index b33a4b0..08e3f69 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -214,7 +214,8 @@ // deleteUnknowProperties if(options.deleteUnknowProperties){ - props = Object.keys(schema.patternProperties).concat(Object.keys(schema.properties)); + props = schema.properties ? Object.keys(schema.properties) : []; + props = props.concat(schema.patternProperties ? Object.keys(schema.patternProperties) : []); for (var p in object){ if (props.indexOf(p) === -1) delete object[p];