From 601638b72422562a45b4605aa916f68dba67b2d4 Mon Sep 17 00:00:00 2001 From: Tim Burch Date: Mon, 4 May 2020 21:06:01 -0400 Subject: [PATCH 1/4] Return empty object or array from coercion for body parameters --- lib/validators.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/validators.js b/lib/validators.js index 243f940..076ebed 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -2,7 +2,6 @@ const Enjoi = require('enjoi'); const Joi = require('@hapi/joi'); -const Util = require('util'); const types = { int64: Joi.string().regex(/^\d+$/), @@ -274,12 +273,7 @@ const coercion = function (parameter, consumes) { } if (!fn && parameter.schema) { - fn = function (data) { - if (Util.isObject(data) && !Object.keys(data).length) { - return undefined; - } - return data; - }; + fn = (data) => data; } return fn; From 48a5652fd905e0802d5aa41a92e17e8215868c9a Mon Sep 17 00:00:00 2001 From: Tim Burch Date: Mon, 4 May 2020 21:30:09 -0400 Subject: [PATCH 2/4] Remove await from non-async calls to "validate" --- lib/validators.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/validators.js b/lib/validators.js index 076ebed..67eca08 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -96,9 +96,9 @@ const create = function (options = {}) { request[p][parameter.name] = coerce && request[p][parameter.name] && coerce(request[p][parameter.name]); return h.continue; }, - validate: async function (value) { + validate: function (value) { const data = coerce && value && coerce(value); - const result = await schema.validate(data); + const result = schema.validate(data); if (result.error) { @@ -140,8 +140,8 @@ const create = function (options = {}) { const formValidators = {}; let headers = {}; - const formValidator = async function (value) { - const result = await this.validate(value); + const formValidator = function (value) { + const result = this.validate(value); if (result.error) { throw result.error; From f273056d7463da49d7f24f05e7281a7aff5c3ce5 Mon Sep 17 00:00:00 2001 From: Tim Burch Date: Mon, 4 May 2020 21:57:01 -0400 Subject: [PATCH 3/4] add "undefined" check for result of "validate" --- lib/validators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validators.js b/lib/validators.js index 67eca08..2ad1958 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -100,7 +100,7 @@ const create = function (options = {}) { const data = coerce && value && coerce(value); const result = schema.validate(data); - if (result.error) { + if (result && result.error) { result.error.message = result.error.message.replace('value', parameter.name); From d4bd4c6bf989802b7534f9094f6101fd5f7ed7b7 Mon Sep 17 00:00:00 2001 From: Tim Burch Date: Tue, 5 May 2020 23:23:08 -0400 Subject: [PATCH 4/4] add tests for error messages --- test/test-validators.js | 49 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/test/test-validators.js b/test/test-validators.js index e9abbd8..7e493aa 100644 --- a/test/test-validators.js +++ b/test/test-validators.js @@ -26,6 +26,25 @@ Test('validator special types', function(t) { description: 'default response' } } + }, + post: { + description: '', + parameters: [ + { + name: 'payload', + in: 'body', + required: true, + schema: { + type: 'object', + required: ['requiredProperty'], + properties: { + requiredProperty: { + type: 'string' + } + } + } + } + ] } }, '/test/{foo*}': { @@ -59,7 +78,7 @@ Test('validator special types', function(t) { ); try { - await validate('1995-09-07T10:40:52Z'); + validate('1995-09-07T10:40:52Z'); t.pass('valid date-time'); } catch (error) { t.fail(error.message); @@ -76,7 +95,7 @@ Test('validator special types', function(t) { const timestamp = Date.now(); try { - await validate(timestamp); + validate(timestamp); t.fail(`${timestamp} should be invalid.`); } catch (error) { t.pass(`${timestamp} is invalid.`); @@ -95,4 +114,30 @@ Test('validator special types', function(t) { } t.fail(`${keys.join(', ')} are invalid.`); }); + + t.test('validate missing body parameter', async function(t) { + t.plan(1); + + const { validate } = validator.makeValidator(api.paths['/test'].post.parameters[0]); + + try { + validate(); + t.fail('"undefined" should be invalid'); + } catch (error) { + t.equal(error.message, '"payload" is required', "received expected payload error message"); + } + }); + + t.test('validate empty object with required property', async function(t) { + t.plan(1); + + const { validate } = validator.makeValidator(api.paths['/test'].post.parameters[0]); + + try { + validate({}); + t.fail('"undefined" should be invalid'); + } catch (error) { + t.match(error.message, /"requiredProperty" is required/, "received expected property error message"); + } + }) });