diff --git a/lib/validators.js b/lib/validators.js index 243f940..2ad1958 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+$/), @@ -97,11 +96,11 @@ 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) { + if (result && result.error) { result.error.message = result.error.message.replace('value', parameter.name); @@ -141,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; @@ -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; 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"); + } + }) });