bee-travels/openapi-comment-parser

Integrating with input validation

mderazon opened this issue · 3 comments

I know this is out of the scope of this library, but I am asking here to see if someone have already given a thought about this and solved it somehow within the openapi ecosystem.

A lot of boilerplate in any api service has to do with validating user's input. There are all kind of json schema validators and if you're using Typescript, you can use something like class-validator to decorate your classes and validate the user's input according to the requirements.

Ideally, we should write the openapi definitions one time in our code base with openapi-comment-parser syntax and then somehow use it to validate the requests according to the spec. I guess this will require "compiling" the comments into a openapi spec file and then using one of the validators to verify the user's input.

Has anyone tried anything like this before and can suggest an approach that worked for them ?

Thank you for this library

Have you tried something like express openapi validator?
https://www.npmjs.com/package/express-openapi-validator

You should be able to do something like this:

const openapi = require('openapi-comment-parser');
const validator = require('express-openapi-validator');

const spec = openapi();

app.use(validator.middleware({ apiSpec: spec }));

Thanks, will try that and see how it works 👍