JoeScho / oas2joi

Create a Joi schema from an Open API Specification
11 stars 2 forks source link

Feature Request: Ability to validate an endpoint #3

Open simontabor opened 5 years ago

simontabor commented 5 years ago

I want to be really lazy and validate an entire endpoint (including parameters, which I don't think are supported at the moment).

Usage could look something like this:

const oas2joi = require('oas2joi');
const validator = oas2joi('./open-api.yml');
const { error } = validator('/my-endpoint', { params, body });
JoeScho commented 5 years ago

Hey @simontabor , thanks for raising this.

I started looking into this, and before long realised that the function would require additional information such as the request method, and potentially content type, going from an OAS such as

/demo:
    post:
      summary: Demo
      description: Demo
      tags:
        - Demo
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/request'

Would it meet your requirements if this functionality were to be added as a middleware, like so:

const validator = require('oas2joi/validator');

// express app
app.use(validator('open-api.yml'));

So in each request, it validates using the request path, method, content-type (if present, default to application/json probably), body/params/query