From 9513ba16032180fd40599cefb27d99e411d1f3b7 Mon Sep 17 00:00:00 2001 From: Mehmet Kamil Sulubulut Date: Tue, 29 Dec 2015 16:43:09 +0200 Subject: [PATCH 1/3] enables `default` attribute --- lib/validator.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/validator.js b/lib/validator.js index 2f3ef63..b38bbc9 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -205,6 +205,11 @@ Validator.prototype.validateSchema = function validateSchema (instance, schema, return this.validateSchema(instance, resolved.subschema, options, subctx); } + // Set default value if instance is undefined and default is defined + if(typeof instance === 'undefined') { + result.instance = instance = schema.default; + } + var skipAttributes = options && options.skipAttributes || []; // Validate each schema attribute against the instance for (var key in schema) { From 047d6b68848e85fc27aa80dc12e2ef65fa3cbcb0 Mon Sep 17 00:00:00 2001 From: Mehmet Kamil Sulubulut Date: Tue, 29 Dec 2015 17:05:57 +0200 Subject: [PATCH 2/3] enables `default` attribute --- lib/validator.js | 2 +- test/attributes.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/validator.js b/lib/validator.js index b38bbc9..5aa71b0 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -206,7 +206,7 @@ Validator.prototype.validateSchema = function validateSchema (instance, schema, } // Set default value if instance is undefined and default is defined - if(typeof instance === 'undefined') { + if(typeof instance === 'undefined' && schema.required !== true) { result.instance = instance = schema.default; } diff --git a/test/attributes.js b/test/attributes.js index b29154b..baab455 100644 --- a/test/attributes.js +++ b/test/attributes.js @@ -295,6 +295,23 @@ describe('Attributes', function () { return this.validator.validate({'the_field':'bar'}, {'type': 'object', 'properties':{'the_field': {'enum': ['foo', 'bar', 'baz'], 'required': true}}}).valid.should.be.true; }); }); + + describe('default', function () { + beforeEach(function () { + this.validator = new Validator(); + }); + it('should preserve value if field is defined', function () { + var validation = this.validator.validate({'the_field': 'foo'}, {'type': 'object', 'properties':{'the_field':{'type': 'string', 'default': 'bar'}}}); + validation.valid.should.be.true; + validation.instance.the_field.should.equal('foo'); + }); + + it('should set default value if field is undefined', function () { + var validation = this.validator.validate({}, {'type': 'object', 'properties':{'the_field':{'type': 'string', 'default': 'bar'}}}) + validation.valid.should.be.true; + validation.instance.the_field.should.equal('bar'); + }); + }); describe('description', function () { beforeEach(function () { From cda08e2a3c9a33c6352554b56c29c56cb7c4dff7 Mon Sep 17 00:00:00 2001 From: Mehmet Kamil Sulubulut Date: Thu, 31 Dec 2015 13:39:57 +0200 Subject: [PATCH 3/3] options.setDefaults flag for default option behavior --- lib/validator.js | 2 +- test/attributes.js | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/validator.js b/lib/validator.js index 5aa71b0..9aaa276 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -206,7 +206,7 @@ Validator.prototype.validateSchema = function validateSchema (instance, schema, } // Set default value if instance is undefined and default is defined - if(typeof instance === 'undefined' && schema.required !== true) { + if(options.setDefaults && typeof instance === 'undefined' && schema.required !== true) { result.instance = instance = schema.default; } diff --git a/test/attributes.js b/test/attributes.js index baab455..093395c 100644 --- a/test/attributes.js +++ b/test/attributes.js @@ -300,14 +300,21 @@ describe('Attributes', function () { beforeEach(function () { this.validator = new Validator(); }); + it('should preserve value if field is defined', function () { - var validation = this.validator.validate({'the_field': 'foo'}, {'type': 'object', 'properties':{'the_field':{'type': 'string', 'default': 'bar'}}}); + var validation = this.validator.validate({'the_field': 'foo'}, {'type': 'object', 'properties':{'the_field':{'type': 'string', 'default': 'bar'}}}, {setDefaults: true}); validation.valid.should.be.true; validation.instance.the_field.should.equal('foo'); }); - it('should set default value if field is undefined', function () { - var validation = this.validator.validate({}, {'type': 'object', 'properties':{'the_field':{'type': 'string', 'default': 'bar'}}}) + it('should not set default value even field is undefined if options.setDefaults is not true', function () { + var validation = this.validator.validate({}, {'type': 'object', 'properties':{'the_field':{'type': 'string', 'default': 'bar'}}}); + validation.valid.should.be.true; + validation.instance.should.not.have.property('the_field'); + }); + + it('should set default value if field is undefined and options.setDefaults is true', function () { + var validation = this.validator.validate({}, {'type': 'object', 'properties':{'the_field':{'type': 'string', 'default': 'bar'}}}, {setDefaults: true}) validation.valid.should.be.true; validation.instance.the_field.should.equal('bar'); });