Code Monkey home page Code Monkey logo

Comments (5)

SpencerLawrenceBrown avatar SpencerLawrenceBrown commented on May 8, 2024

Looks like the validator middleware only gets pushed onto the app not the routes:

express-openapi-validator/dist/index.js

        if (this.options.validateRequests)
            use.push(validateMiddleware);
        if (this.options.validateResponses)
            use.push(resOav.validate());
        app.use(use);

I imagine this is the issue.

from express-openapi-validator.

SpencerLawrenceBrown avatar SpencerLawrenceBrown commented on May 8, 2024

ok wow, did more digging and it turns out that the order in which the middlewares are added matters
:(

Validates correctly

const app = express();
new OpenApiValidator({
  apiSpec: './workflow-tracker-openapi.yml',
  validateRequests: true,
  validateResponses: true,
}).install(app);
app.use(bodyParser.json());
app.use(express.json());
app.use('/v1', progressRouter);

Does not validate

const app = express();
app.use('/v1', progressRouter);
new OpenApiValidator({
  apiSpec: './workflow-tracker-openapi.yml',
  validateRequests: true,
  validateResponses: true,
}).install(app);
app.use(bodyParser.json());
app.use(express.json());

from express-openapi-validator.

SpencerLawrenceBrown avatar SpencerLawrenceBrown commented on May 8, 2024

Let me know if you'd like me to open up a pr to talk about this in the documentation

from express-openapi-validator.

cdimascio avatar cdimascio commented on May 8, 2024

I believe the issue is the fact in the second example (Does not validate), the bodyParser middleware is setup after the routes are setup. To parse json, text, etc the parse middleware should come earlier in the chain

Assuming this is the case, perhaps, a note should be added to the documentation.

Note that I can certainly have the validator install the parser middleware automatically, however I feel the choice of parser should be left to the user and not to the library.

from express-openapi-validator.

cdimascio avatar cdimascio commented on May 8, 2024

@SpencerLawrenceBrown i've added some notes to the doc. feel free to provide feedback. thanks!

from express-openapi-validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.