Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The validation doesn't work at all #870

Open
razb-viola opened this issue Oct 21, 2023 · 2 comments
Open

The validation doesn't work at all #870

razb-viola opened this issue Oct 21, 2023 · 2 comments

Comments

@razb-viola
Copy link

razb-viola commented Oct 21, 2023

Describe the bug
I am trying to implement the express-openapi-validator in a sample node.js app.
I followed the instructions in the docs and configured it properly.
However, protected routes, which receive user input that must be validated are open for all - one can access them even without providing any input or do provide input which is invalid.

To Reproduce
Send an HTTP Request to the backend, with any input you like and it will work.
I protected in my specification the POST /persons route.

Actual behavior
Protected routes are never validating user inputs.

Expected behavior
There should be an error with a very detailed description that will be catch by the express error middleware.

Examples and context
This is the backend app code:

const express = require('express')
const OpenApiValidator = require('express-openapi-validator')
const path = require('path')

const app = express()
app.use(express.json())
app.use(express.text())
app.use(express.urlencoded({ extended: false }))
const apiSpec = path.join(__dirname, 'swagger.yaml')

app.use(OpenApiValidator.middleware({
  apiSpec,
  validateResponses: true,
}))

app.post('/persons', (req, res) => {
  console.log(`validated!`)
  res.json({ date: new Date().toISOString() })
})

app.use((err, req, res, next) => {
  console.log(err)
  res.json({ error: 'error' })
})

app.listen(5959, () => console.log(`listen on 5959`))

In the Swagger UI it seems that the POST /persons route is requiring firstName and lastName with type of string and min 2 and max 20 length, each.

By the way, if anyone post an answer or a fix for this, it can be great if you provide a a TypeScript sample node app which supports ESM, I used commonjs here since __dirname causes issues with TypeScript.

@chaselmann
Copy link

Your setup seems to be fine. Could you provide your apiSpec?

@Ragnis
Copy link

Ragnis commented Jan 3, 2024

Do you have any URLs set under the servers property in your swagger.yaml? In my case, having a URL with a path name under there caused a similar problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants