Skip to content

Commit

Permalink
feat: surfacing all schema validation errors (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored Oct 17, 2022
1 parent cff718e commit 22b2082
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/validators/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function validateSchema(api, options) {
*/
function initializeAjv(draft04 = true) {
const opts = {
allErrors: false,
allErrors: true,
strict: false,
validateFormats: false,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
openapi: 3.0.0
info:
description: 'This is a sample server Petstore server.'
version: 1.0.0
title: Swagger Petstore
servers:
- urll: http://petstore.swagger.io/v2
tags:
- name: pet
description: Everything about your Pets
externalDocs:
description: Find out more
url: http://swagger.io
paths:
'/pet/findByStatus':
get:
tagss:
- pet
summary: Finds Pets by status
description: Multiple status values can be provided with comma separated strings
operationId: findPetsByStatus
parameters:
- name: status
in: query
description: Status values that need to be considered for filter
required: true
explode: true
schema:
type: array
items:
type: string
enum:
- available
- pending
- sold
default: available
responses:
'200':
description: OK
21 changes: 19 additions & 2 deletions test/specs/validate-schema/validate-schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { expect } = require('chai');
const OpenAPIParser = require('../../..');
const path = require('../../utils/path');

describe('Invalid APIs (Swagger 2.0 schema validation)', () => {
describe('Invalid APIs (Swagger 2.0 and OpenAPI 3.x schema validation)', () => {
const tests = [
{
name: 'invalid response code',
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('Invalid APIs (Swagger 2.0 schema validation)', () => {
file: 'oneof.yaml',
},
{
name: 'invalid security sceheme for OpenAPI 3.0',
name: 'invalid security scheme for OpenAPI 3.0',
valid: false,
file: 'invalid-security-scheme.yaml',
openapi: true,
Expand All @@ -120,6 +120,23 @@ describe('Invalid APIs (Swagger 2.0 schema validation)', () => {
expect(api).to.be.an('object');
});

it('should return all errors', async () => {
try {
await OpenAPIParser.validate(path.rel('specs/validate-schema/invalid/multiple-invalid-properties.yaml'));
throw new Error('Validation should have failed, but it succeeded!');
} catch (err) {
expect(err).to.be.an.instanceOf(SyntaxError);
expect(err.message).to.match(/^OpenAPI schema validation failed.\n(.*)+/);

expect(err.details).to.be.an('array').to.have.length(3);

expect(err.message).to.contain("REQUIRED must have required property 'url'");
expect(err.message).to.contain('url is missing here');
expect(err.message).to.contain('ADDITIONAL PROPERTY must NOT have additional properties');
expect(err.message).to.contain('tagss is not expected to be here');
}
});

for (const test of tests) {
if (test.valid) {
it(test.name, async () => {
Expand Down

0 comments on commit 22b2082

Please sign in to comment.