I am using an operation handler for requests. In the oas scheme I defined create user as:
/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: example@gmail.com
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
and these are my validator settings:
const openApiValidator = OpenApiValidator.middleware({
apiSpec,
operationHandlers: path.join(__dirname)
})
but in the response when I try to create a user with wrong email pattern, for example: examplegmail.com, instead of getting something like: {400,'Bad request'}
I get a HTML page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Bad Request: request.body.email should match pattern "^[\w-\.]+@([\w-]+\.)+[\w-]{2,5}$"<br> at Object.POST-/users-application/json (C:\Users\mager\Desktop\netflix-api_\node_modules\express-openapi-validator\src\middlewares\openapi.request.validator.ts:175:35)<br> at RequestValidator.validate (C:\Users\mager\Desktop\netflix-api_\node_modules\express-openapi-validator\src\middlewares\openapi.request.validator.ts:76:37)<br> at C:\Users\mager\Desktop\netflix-api_\node_modules\express-openapi-validator\src\openapi.validator.ts:282:49<br> at C:\Users\mager\Desktop\netflix-api_\node_modules\express-openapi-validator\src\openapi.validator.ts:188:20<br> at processTicksAndRejections (node:internal/process/task_queues:95:5)</pre>
</body>
</html>
Can i change this behaviour or is this how it is supposed to be?
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: I managed to fix it my error handler was missing the next parameter, I thought I could omit that one cuz I wasn't using it.
I managed to fix it my error handler was missing the next parameter, I thought I could omit that one cuz I wasn't using it.