-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom Spectral rule to ensure content objects contain schema
Purpose: - Establish a pattern in the validator for adding custom spectral rules and custom spectral functions - Ensure content objects contain a schema Changes: - Add custom spectral rule to ensure that content objects contain a schema (defaults to warning) Tests: - Add tests for each use case outlined in the spectral rule
- Loading branch information
Barrett Schonefeld
committed
Mar 10, 2021
1 parent
25cf7c6
commit 0b09c2a
Showing
4 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
test/spectral/tests/custom-rules/content-entry-contains-schema.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
const inCodeValidator = require('../../../../src/lib'); | ||
|
||
describe('spectral - test validation that schema provided in content object', function() { | ||
it('should not error when the content object contains a schema', async () => { | ||
const spec = { | ||
Openapi: '3.0.0', | ||
paths: {}, | ||
components: { | ||
schemas: { | ||
GenericSchema: { | ||
content: { | ||
'application/json': { | ||
// schema provided | ||
schema: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const res = await inCodeValidator(spec, true); | ||
const expectedWarnings = res.warnings.filter( | ||
warn => | ||
warn.message === | ||
'Content entries in request and response bodies must specify a schema' | ||
); | ||
expect(expectedWarnings.length).toBe(0); | ||
}); | ||
|
||
it('should error when a content object in a requestBody reference does not contain a schema', async () => { | ||
const spec = { | ||
Openapi: '3.0.0', | ||
paths: { | ||
path1: { | ||
post: { | ||
requestBody: { | ||
$ref: '#/components/requestBodies/GenericRequestBody' | ||
} | ||
} | ||
} | ||
}, | ||
components: { | ||
requestBodies: { | ||
GenericRequestBody: { | ||
content: { | ||
'application/json': { | ||
// schema not provided | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const res = await inCodeValidator(spec, true); | ||
const expectedWarnings = res.warnings.filter( | ||
warn => | ||
warn.message === | ||
'Content entries in request and response bodies must specify a schema' | ||
); | ||
expect(expectedWarnings.length).toBe(1); | ||
}); | ||
|
||
it('should error when a content object in a response reference does not contain a schema', async () => { | ||
const spec = { | ||
Openapi: '3.0.0', | ||
paths: { | ||
path1: { | ||
post: { | ||
responses: { | ||
'200': { | ||
$ref: '#/components/responses/GenericResponse' | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
components: { | ||
responses: { | ||
GenericResponse: { | ||
content: { | ||
'application/json': { | ||
// schema not provided | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const res = await inCodeValidator(spec, true); | ||
const expectedWarnings = res.warnings.filter( | ||
warn => | ||
warn.message === | ||
'Content entries in request and response bodies must specify a schema' | ||
); | ||
expect(expectedWarnings.length).toBe(1); | ||
}); | ||
|
||
it('should error when the content object does not contain a schema in a response', async () => { | ||
const spec = { | ||
Openapi: '3.0.0', | ||
paths: { | ||
'pets/{petId}': { | ||
get: { | ||
operationId: 'getPetsById', | ||
responses: { | ||
200: { | ||
content: { | ||
'*/*': { | ||
// schema not provided | ||
} | ||
} | ||
}, | ||
default: { | ||
content: { | ||
'text/html': { | ||
// schema not provided | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const res = await inCodeValidator(spec, true); | ||
const expectedWarnings = res.warnings.filter( | ||
warn => | ||
warn.message === | ||
'Content entries in request and response bodies must specify a schema' | ||
); | ||
expect(expectedWarnings.length).toBe(2); | ||
}); | ||
|
||
it('should error when the content object does not contain a schema in a request body', async () => { | ||
const spec = { | ||
Openapi: '3.0.0', | ||
paths: { | ||
createPet: { | ||
post: { | ||
operationId: 'addPet', | ||
requestBody: { | ||
content: { | ||
'application/json': { | ||
// no schema provided | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const res = await inCodeValidator(spec, true); | ||
const expectedWarnings = res.warnings.filter( | ||
warn => | ||
warn.message === | ||
'Content entries in request and response bodies must specify a schema' | ||
); | ||
expect(expectedWarnings.length).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters