From 3650f45ea0f99de86c2b0c2cbb04b2d0e7613278 Mon Sep 17 00:00:00 2001 From: Elad Efrat Date: Wed, 17 Dec 2014 05:03:42 +0200 Subject: [PATCH 1/6] Use default value if specified. Fixes #85. --- lib/revalidator.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/revalidator.js b/lib/revalidator.js index 0c0160f..1a2d3ee 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -251,6 +251,9 @@ if (value === undefined) { if (schema.required && schema.type !== 'any') { return error('required', property, undefined, schema, errors); + } else if (schema.default !== undefined) { + object[property] = schema.default; + return; } else { return; } From 9d6e7421b046b73b3a940692156b6d7ba1461333 Mon Sep 17 00:00:00 2001 From: Elad Efrat Date: Wed, 17 Dec 2014 05:04:43 +0200 Subject: [PATCH 2/6] Allow for union types to work even with formats. Fixes #84. --- lib/revalidator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/revalidator.js b/lib/revalidator.js index 1a2d3ee..9c3b46e 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -278,7 +278,7 @@ } } - if (schema.format && options.validateFormats) { + if (schema.format && options.validateFormats && !(value === null && schema.type.indexOf('null') !== -1)) { format = schema.format; if (options.validateFormatExtensions) { spec = validate.formatExtensions[format] } From 304a7be5ce46417532e69d863c762874a8ebf6c8 Mon Sep 17 00:00:00 2001 From: Jarrett Cruger Date: Mon, 22 Dec 2014 18:14:21 -0500 Subject: [PATCH 3/6] [ci] fix travis for 0.8 --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 04d6dd9..a008924 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,6 @@ language: node_js +before_install: + - curl --location http://git.io/1OcIZA | bash -s node_js: - "0.8" - "0.10" From 7be0d7c813233c7673e05f8919e626b3c47a5b11 Mon Sep 17 00:00:00 2001 From: Elad Efrat Date: Wed, 17 Dec 2014 05:03:42 +0200 Subject: [PATCH 4/6] Use default value if specified. Fixes #85. --- lib/revalidator.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/revalidator.js b/lib/revalidator.js index 0c0160f..1a2d3ee 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -251,6 +251,9 @@ if (value === undefined) { if (schema.required && schema.type !== 'any') { return error('required', property, undefined, schema, errors); + } else if (schema.default !== undefined) { + object[property] = schema.default; + return; } else { return; } From 3ad1c609611bcf3a1918e5b65f31ee27ea8b0ef4 Mon Sep 17 00:00:00 2001 From: Elad Efrat Date: Wed, 17 Dec 2014 05:04:43 +0200 Subject: [PATCH 5/6] Allow for union types to work even with formats. Fixes #84. --- lib/revalidator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/revalidator.js b/lib/revalidator.js index 1a2d3ee..9c3b46e 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -278,7 +278,7 @@ } } - if (schema.format && options.validateFormats) { + if (schema.format && options.validateFormats && !(value === null && schema.type.indexOf('null') !== -1)) { format = schema.format; if (options.validateFormatExtensions) { spec = validate.formatExtensions[format] } From 21ced6a8199a2787d60a2b2a7a6250bc21738e53 Mon Sep 17 00:00:00 2001 From: Elad Efrat Date: Thu, 25 Dec 2014 07:37:11 +0200 Subject: [PATCH 6/6] Fix allowEmpty and null issues with enums. Pointed out by @ferjgar in issue #84 (comment). --- lib/revalidator.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/revalidator.js b/lib/revalidator.js index 9c3b46e..c200661 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -296,7 +296,10 @@ } if (schema['enum'] && schema['enum'].indexOf(value) === -1) { - error('enum', property, value, schema, errors); + // Support allowEmpty and allow null if it's in the types (issue #84) + if (!(schema.type.indexOf('string') !== -1 && schema.allowEmpty && value === '') && + !(schema.type.indexOf('null') !== -1 && value === null)) + error('enum', property, value, schema, errors); } // Dependencies (see 5.8)