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

fix: validator throws 422 with message #191

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 0 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ const requireApi = function (path) {

const register = async function (server, options, next) {

//Validator needs to be explicitly declared for Hapi v19.*
server.validator(require('joi'));

const validation = optionsSchema.validate(options);

Hoek.assert(!validation.error, validation.error);
Expand Down
11 changes: 6 additions & 5 deletions lib/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Enjoi = require('enjoi');
const Joi = require('joi');
const Boom = require('@hapi/boom');

const extensions = [
{ type: 'int64', base: Joi.string().regex(/^\d+$/) },
Expand Down Expand Up @@ -83,7 +84,7 @@ const create = function (options = {}) {

if (openapi) {
template = { ...template, ...parameter.schema };
}
}
else {
template.schema = parameter.schema;
}
Expand Down Expand Up @@ -127,7 +128,7 @@ const create = function (options = {}) {
detail.path = [parameter.name];
});

throw result.error;
throw Boom.boomify(result.error, { statusCode: 422 });
}

return result.value;
Expand All @@ -149,7 +150,7 @@ const create = function (options = {}) {
break;
}
}
}
}
else {
schemaDesc = response.schema;
}
Expand Down Expand Up @@ -298,7 +299,7 @@ const coercion = function (parameter, consumes) {
if (parameter.format === 'int64') {
return data;
}

return Number(data);
};

Expand All @@ -321,7 +322,7 @@ const coercion = function (parameter, consumes) {
in: parameter.in
};
};

break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@
"api",
"soa"
]
}
}
12 changes: 6 additions & 6 deletions test/test-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const OpenAPI = require('../lib');
const Hapi = require('@hapi/hapi');


Test('form data', function (t) {
Test('form data', (t) => {

t.test('upload', async function (t) {
t.test('upload', async (t) => {
t.plan(1);

try {
Expand All @@ -21,7 +21,7 @@ Test('form data', function (t) {
handlers: {
upload: {
post: function (req, h) {
return {
return {
upload: req.payload.toString()
};
}
Expand All @@ -48,7 +48,7 @@ Test('form data', function (t) {

});

t.test('bad content type', async function (t) {
t.test('bad content type', async (t) => {
t.plan(1);

try {
Expand Down Expand Up @@ -83,7 +83,7 @@ Test('form data', function (t) {
});


t.test('invalid payload', async function (t) {
t.test('invalid payload', async (t) => {
t.plan(1);

try {
Expand Down Expand Up @@ -112,7 +112,7 @@ Test('form data', function (t) {
payload: 'name=thing&upload='
});

t.strictEqual(response.statusCode, 400, `${response.request.path} validation error.`);
t.strictEqual(response.statusCode, 422, `${response.request.path} validation error.`);
}
catch (error) {
t.fail(error.message);
Expand Down
Loading