Upgrading Hapi and Joi validation issues

23 Views Asked by At

I'm currently upgrading an API server that uses Hapi for handling requests. When upgrading from Joi 13.7.0 to 17.12.2 and Hapi 18.1.0 to 21.3.3 I'm having an issue with POST requests returning a 400 error with the message "payload" must be of type object and using the content type text/plain;charset=UTF-8 (due to the way applications are calling the route).

The route configuration currently looks like this:

const postRouteA = {
  method: 'POST',
  path: '/post-route-a/{id}/item',
  options: {
    handler: async (request, h) => {
      // ....
      // Handler Code
      // ....
      return h.response(result).code(201);
    },
    description: 'Api description',
    validate: {
      params: Joi.object({
        id: Joi.string()
          .required()
          .description('Unique identified')
          .example('1a'),
      }),
      payload: Joi.object({
        date: Joi.string()
          .required()
          .description('Date/time')
          .example('2018-06-27T01:35:21.595Z'),
        description: Joi.string()
          .required()
          .description('Description')
          .example('Example Description'),
      }).label('payload'),
    },
    response: {
      status: {
        201: Joi.object({
          id: Joi.number().integer()
            .example(1234)
            .description('Unique identified'),
        }).label('Result'),
      },
    },
  },
};

Prior to upgrading the route making a POST request worked with the content-type header set to text/plain;charset=UTF-8. But after upgrading, this route only works when content-type header is set to application/json. In addition to upgrading packages, I have also moved from NodeJS 8 to 18.

Is there any way to get this working without having to update the application that uses this and maintain the current functionality?

0

There are 0 best solutions below