My API is returning 204 No Content. I'm using https://github.com/glennjones/hapi-swagger to generate a Swagger schema from a Joi schema in Hapi. I'm also validating the response output and since my endpoint is returning a 204 the body needs to be empty. How can I represent this using Joi?
My current workaround is to set validation: false in Hapi.
server.route({
method: 'DELETE',
path: '/endpoint',
handler,
config: {
response: {
status: {
204: false,
404: schema.responseNotFound
}
},
}
});
This results in an empty object on the /documentation page generated by hapi-swagger.

The response will be whatever you replied, in a normal scenario you would need something like Joi.only(null), but considering hapi removes the responses from 204s you shouldn't have to care, so your false is fine, unless you want to prevent programmer errors.
Hi @danihodovic!
I have a similar configuration as you and here is my solution, which is pretty empty actually :)
{
method: 'DELETE',
path: '/v1/datasourcestypes/{id}',
config: {
description: 'Delete a single data source type.',
notes: 'Deletes a single object',
tags: ['api', 'v1'],
response: {
status: {
204: joi.string().empty('')
}
},
validate: {
params: {
id: joi.string()
.guid({ version: ['uuidv4'] })
.required()
.description('The UUID V4 of the data source.')
}
},
handler: handlers.delete
}
}
The documentation output is like:

What's wrong with false ?
What's wrong with false ?
@Marsup it's rendered differently using https://github.com/glennjones/hapi-swagger
Using false

Using joi.string().empty('')

Thanks @ricmalta
Then that's a bug in hapi-swagger, hapi can't return anything on a 204, that's simply impossible.
@glennjones what's your take on this ?
Well my interpretation is that the documentation is correct but complete.
It should not send an example of an empty object because it's a false state, in other hand it should referer that there is no body.
So @Marsup I agree with you when you say that it's a bug or a non implemented feature of hapi-swagger
Most helpful comment
Well my interpretation is that the documentation is correct but complete.
It should not send an example of an empty object because it's a false state, in other hand it should referer that there is no body.
So @Marsup I agree with you when you say that it's a bug or a non implemented feature of hapi-swagger