node.js – OpenAPI: “request should have required property ‘body'”

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',
      validateRequests: true,
      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 reset token by email",
        "operationId": "validateToken",
        "responses": {
          "200": {
            "description": "Successful operation",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          }
        },
        "parameters": [
          {
            "name": "email",
            "in": "path",
            "description": "User email",
            "required": true,
            "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.

Read more here: Source link