-
Notifications
You must be signed in to change notification settings - Fork 87
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 Docs: - Add a central location for documenting custom Spectral rules - Document the `content-entry-contains-schema` rule
- Loading branch information
Barrett Schonefeld
committed
Mar 16, 2021
1 parent
65a9b3d
commit 41767c8
Showing
6 changed files
with
225 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
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,30 @@ | ||
# Custom Spectral Rules | ||
|
||
This document outlines the custom Spectral rules implemented in `ibm:oas` ruleset. | ||
|
||
## content-entry-contains-schema | ||
|
||
Any request or response body that has `content` should contain a schema. | ||
|
||
**Bad Example** | ||
|
||
```yaml | ||
responses: | ||
200: | ||
content: | ||
application/json: | ||
# schema not provided | ||
``` | ||
|
||
**Good Example** | ||
|
||
```yaml | ||
responses: | ||
200: | ||
content: | ||
application/json: | ||
schema: | ||
type: string | ||
``` | ||
|
||
**Default Severity**: warn |
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
177 changes: 177 additions & 0 deletions
177
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,177 @@ | ||
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: { | ||
path1: { | ||
get: { | ||
responses: { | ||
'200': { | ||
$ref: '#/components/responses/GenericResponse' | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
components: { | ||
responses: { | ||
GenericResponse: { | ||
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