Joi: How to represent a 204 No Content using Joi?

Created on 2 Mar 2017  路  7Comments  路  Source: sideway/joi

Context

  • node version:
  • joi version: 10.1.0
  • environment: node
  • used with: hapi
  • any other relevant information:

What are you trying to achieve or the steps to reproduce ?

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

Which result you had ?

This results in an empty object on the /documentation page generated by hapi-swagger.
joi-hapi-swagger

What did you expect ?

support

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

All 7 comments

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:
screen shot 2017-03-22 at 15 52 49

What's wrong with false ?

What's wrong with false ?

@Marsup it's rendered differently using https://github.com/glennjones/hapi-swagger

Using false
delete-response-2

Using joi.string().empty('')
delete-response-1

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mohamadresaaa picture mohamadresaaa  路  3Comments

Dreamystify picture Dreamystify  路  4Comments

kevbook picture kevbook  路  4Comments

neroaugustus1 picture neroaugustus1  路  4Comments

chrisegner picture chrisegner  路  4Comments