The content returned by openapi-validator is in HTML format, rather than JSON as expected

Using an operation handler for requests, I have defined the creation of a user in the oas scheme as follows:

/users:
post:
  description: |
    creates a user
  operationId: createUser
  x-eov-operation-handler: controllers/user.controller
  tags:
    - Users
  requestBody:
    description: User to create
    required: true
    content:
      application/json:
        schema:
          properties:
            email:
              type: string
              description: The email of the user
              example: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85e0fde4e8f5e9e0c5e2e8e4ece9abe6eae8">[email protected]</a>
              pattern: ^[\w-\.]+@([\w-]+\.)+[\w-]{2,5}$
            password:
              type: string
              description: The password of  the user
              example: password1234
              minLength: 8
              maxLength: 16
          required:
            - email
            - password
          additionalProperties: false
  responses:
    '201':
      description: User was created
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/User'
    '400':
      description: Bad request
      content:
        application/json:
          schema:
            properties:
              code:
                description: Http status code
                type: number
                example: 400
              message:
                description: The message of the response
                type: string
                example: Bad request
            required:
              - code
              - message
            additionalProperties: false

... etc

Here are my validator settings:

const openApiValidator = OpenApiValidator.middleware({
  apiSpec,
  operationHandlers: path.join(__dirname)
})

However, when attempting to create a user with an incorrect email pattern like examplegmail.com, instead of receiving something like: {400,'Bad request'}, I am presented with an HTML page.

<!DOCTYPE html>
<html lang="en>
<head>
<meta charset="utf-8">
<title>Error</title></head>
<body>
<pre>Error details here...
</pre>
</body>
</html>

Is there a way to change this behavior or is it intended to be this way?

Edit: I also have an error handler defined after openApiValidator

app.use(openApiValidator);
app.use(errorHandler);

function errorHandler(err: any, req: Request, res: Response) {
  res.status(err.status || 500).json({
    code: err.status,
    message: err.message,
  });
}

Edit 2: Issue resolved After realizing that my error handler was missing the next parameter, I included it and the problem was fixed.

Answer №1

After realizing my error, I discovered that my error handler was missing the next parameter. Initially, I thought I could omit it because I wasn't using it, but fixing this issue solved the problem.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The server node proxy is failing to trigger the API call

update 1: After modifying the api path, I am now able to initiate the api call. However, I encountered the following error: (node:13480) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): RangeError: Invalid status code: res ...

What is the procedure for changing the status of an incident in BMC Remedy using the API?

For our project, we successfully integrated BMC Remedy API to handle incidents by creating them with comments and attachments, filtering incidents, and retrieving incident details. Our current task is to update the status of a created incident to closed u ...

When using Express and torrent-stream to write a file from a stream and send it to a client with a URL, the client is unable to locate

Currently, I am working on a personal project that involves taking a magnet link, initiating the file download, and then displaying the video within the torrent directly in the browser. To accomplish this, I am utilizing an npm module called torrent-stream ...

Problem with Installing Mongoose

During my attempt to install mongoose, I have encountered a persistent error. Even after expanding the available space, the issue remains unsolved. The error message is as follows: https://i.sstatic.net/7bYmS.png ...

Dynamic typing depending on the individual elements within an array

I am working with some extensions: type ExtensionOptions = { name: string } type BoldOptions = ExtensionOptions & { boldShortcut?: string } type ItalicOptions = ExtensionOptions & { italicShortcut?: string } type LinkOptions = ExtensionOptions &am ...

Leveraging .tsx components within nested .tsx components in React Native

Currently, I am delving into the world of building apps using TypeScript in React Native. Coming from a background as a Swift developer, adjusting to JavaScript and TypeScript has been an interesting journey. An observation that stood out to me is the cha ...

The compatibility issue arises when trying to utilize Axios for API calls in Ionic 6 React with react-query on a real Android device in production. While it works seamlessly on the emulator and browser

My form utilizes react-hook-form to submit data to a server. Here is the code: <FormProvider {...methods}> <form onSubmit={handleSubmit(onIndividualSignup)}> <Swiper onSwiper={(swiper) => setSlidesRef(s ...

The message indicates that "items" has not been defined

Below is the code snippet: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const userSchema = new Schema({ name: { type: String, required: true }, email: { type: String, require ...

What is the proper way to utilize a service within a parent component?

I need assistance with setting up inheritance between Child and Parent components. I am looking to utilize a service in the Parent component, but I have encountered an issue. When attempting to input the service in the Parent constructor like this: expor ...

Compiler unable to determine Generic type if not explicitly specified

Here is a simple code snippet that I am working with: class Model { prop1: number; } class A<TModel> { constructor(p: (model: TModel) => any) {} bar = (): A<TModel> => { return this; } } function foo<T>(p: ...

Traverse the elements of a BehaviorSubject named Layer_Template

I am currently facing an issue with displaying data from my BehaviorSubject. I have come across a way to iterate through a BehaviorSubject using asyncpipe which subscribes to the Observable SERVICE todo.service.ts @Injectable() export class TodoService ...

What is the best way to send data back in a post request using express.js?

I have a function in my React frontend called handleDrop that handles file uploads. Here is the code: handleDrop(files) { var data = new FormData(); alert((files[0]) instanceof File); files.forEach((file, index) => { data.append(&a ...

The TypeScript type for a versatile onChange handler in a form

Let's skip the function declaration and dive into writing the current types for state, and the state itself. type BookFormState = { hasError: boolean; } BookForm<BookFormState> { ... state = { hasError: false }; Next, inside the class ...

Discover the power of sorting outcomes in a Node.js-MongoDB query by utilizing a dynamic function call

Currently, I am working on a web application in Node.js that is connected to MongoDB using the Mongo native connector. In one of my JavaScript files, I have implemented a generic method to perform "find" or "findOne" operations to fetch data from a collec ...

Sanity.io's selection of schema field types for efficient and convenient

Hey there, guys! I recently started using Sanity.io and I'm curious whether there's a way to enhance my code efficiency and reuse certain fields across different schemas. I had an idea that goes something like this: cars.ts: export default { ...

The pagination in React using React Query will only trigger a re-render when the window is in

Currently, I am utilizing React-Query with React and have encountered an issue with pagination. The component only renders when the window gains focus. This behavior is demonstrated in the video link below, https://i.sstatic.net/hIkFp.gif The video showc ...

Avoid sending an empty object to a mongoose schema

I am struggling with checking if all values of an object are empty before saving it to the database to prevent blank items. When I edit supplier brand information, it always adds empty supplier data to the mongoose schema. Can someone please help me figure ...

Struggling to effectively transfer a callback function within a series of functions

I am currently utilizing the codebird library to make requests to the Twitter API. The responses from these requests are functioning as expected, but I am looking to pass that response along to my route. Below is a segment of my route.js: router.get(&apos ...

What is the process of creating Excel-like data bars using Angular?

I am seeking a way to incorporate databars behind the values in a specific column within my grid, similar to the Data Bars Conditional Formatting feature in Excel as shown here. I came across an answer that demonstrates how to achieve this using jQuery da ...

Having trouble getting the express router to function properly in your Node.js TypeScript project?

One of the components in this application is registerClass, where all routes are added. The source code is in the dist directory since this node app is using TypeScript. However, when calling the http://localhost:9001/user endpoint, it seems that it is not ...