5

I am building out a new endpoint in my application which uses express-openapi-validator as validator middleware.

/* index.ts */

import * as OpenApiValidator from 'express-openapi-validator';

const whitelistedPaths = [/* regex tested paths */];

app.use(
    OpenApiValidator.middleware({
      apiSpec: './schema/api.json',
      validateResponses: true,
      ignorePaths: whitelistedPaths,
      validateSecurity: true,
    }),
  );

/* ... */

app.post(
  '/users/:email/validateToken',
  bodyParser.json(),
  (req) => validateToken(req.params.email, req.body.resetToken),
);

In my configuration (api.json) file I've defined the schema for my endpoint as:

    "/users/{email}/validateToken": {
      "post": {
        "tags": ["users"],
        "summary": "Validate user token",
        "operationId": "validateToken",
        "responses": {
          "200": {
            "description": "Ok",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          }
        },
        "parameters": [
          {
            "name": "email",
            "in": "path",
            "description": "User email",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["resetToken"],
                "properties": {
                  "resetToken": {
                    "type": "string"
                  }
                }
              }
            }
          }
        }
      }
    },

I've tested with Postman with the following JSON body:

{
  "resetToken": "randomd9320ru9"
}

but receive the following error message:

{
    "message": "request should have required property 'body'",
    "errors": [
        {
            "path": ".body",
            "message": "should have required property 'body'",
            "errorCode": "required.openapi.validation"
        }
    ]
}

I'm not sure why it's complaining about the body. I tried putting "required": true under the requestBody config in api.json but that didn't change anything. I just want to make sure that the body includes the required field resetToken.

milan
  • 235
  • 2
  • 13
  • Likely the same issue, or related: https://stackoverflow.com/questions/66420890/open-api-error-request-should-have-required-property-headers-docker – Oscar Jul 15 '22 at 22:20

1 Answers1

5

I suppose you need to use bodyParser.json() before using OpenApiValidator.middleware:

app.use(bodyParser.json());
app.use(
    OpenApiValidator.middleware({
      apiSpec: './schema/api.json',
      validateRequests: true,
      validateResponses: true,
      ignorePaths: whitelistedPaths,
      validateSecurity: true,
    }),
  );
...
app.post(
  '/users/:email/validateToken',
  (req) => validateToken(req.params.email, req.body.resetToken),
);
Anatoly
  • 20,799
  • 3
  • 28
  • 42