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

openapi-request-validator doesn't validate JSON objects given in headers or query params #854

Open
cguy opened this issue Jan 23, 2023 · 1 comment

Comments

@cguy
Copy link

cguy commented Jan 23, 2023

Hello,

I have an issue regarding the request validator. It doesn't seem to validate objects (content > application/json) given in header or query params.

Stackblitz to reproduce : https://stackblitz.com/edit/node-bfjjnw?embed=1&file=index.js,my-api.yml

---
openapi: 3.0.3
info:
  title: API name
  description: API description
  version: 0.0.1
paths:
  "/test":
    get:
      parameters:
        - in: header
          name: x-my-header
          required: true
          type: string
          pattern: '(test1|test2)'
        - in: query
          name: filter
          required: true
          content:
            application/json:
              schema:
                type: object
                required:
                  - "color"
                properties:
                  type:
                    type: string
                  color:
                    type: string
                    pattern: "(black|white)"
      responses:
        "204":
          description: OK

Calling the validator

// run `node index.js` in the terminal
const SwaggerParser = require('@apidevtools/swagger-parser');
const OpenAPIRequestValidator = require('openapi-request-validator');

console.log(`Hello Node.js v${process.versions.node}!`);

(async () => {
  let api = await SwaggerParser.bundle('./my-api.yml');
  const validator = new OpenAPIRequestValidator.default({
    ...api.paths['/test'].get,
  });

  const validation = validator.validateRequest({
    body: {},
    headers: { 'x-my-header': 'test1' },
    query: { filter: '{"type": "test"}' },
  });
  console.log('\n\nshould contains a validation error about query', validation);
})();

I should have some errors color is required (and have pattern to respect). Is there anything I am doing wrong?

@sshahar1
Copy link

I think I'm experiencing the same issue

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

2 participants