openapi-comment-parser icon indicating copy to clipboard operation
openapi-comment-parser copied to clipboard

Integrating with input validation

Open mderazon opened this issue 5 years ago • 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

mderazon avatar Sep 29 '20 08:09 mderazon

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

bourdakos1 avatar Sep 29 '20 13:09 bourdakos1

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 }));

bourdakos1 avatar Sep 29 '20 13:09 bourdakos1

Thanks, will try that and see how it works :+1:

mderazon avatar Sep 30 '20 07:09 mderazon