Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty object coercion #178

Merged
merged 4 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions lib/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const Enjoi = require('enjoi');
const Joi = require('@hapi/joi');
const Util = require('util');

const types = {
int64: Joi.string().regex(/^\d+$/),
Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i imagine this was a left over from earlier version of async joi validate calls.

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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
49 changes: 47 additions & 2 deletions test/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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*}': {
Expand Down Expand Up @@ -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);
Expand All @@ -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.`);
Expand All @@ -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");
}
})
});