-
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: error for parameter without schema or content
Purpose: - OAS3 schema requires that either a schema or a content object must be defined for any parameter. Tooling based on OAS3 will expect parameters to have a schema or content. Changes: - Add check that a schema is provided in content objects for parameters. - Check that schema or content object provided for all parameters. Test: - Add test to ensure that error returned for parameter with no schema and no content objects. - Add test to ensure a warning given for a content object in a parameter with no schema object. Docs: - Document the parameter-contains-schema rule.
- Loading branch information
1 parent
6e9d3a9
commit 8b6e8bd
Showing
5 changed files
with
151 additions
and
17 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
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,9 @@ | ||
module.exports = function(obj) { | ||
if (!obj.schema && !obj.content) { | ||
return [ | ||
{ | ||
message: 'Parameter must provide either a schema or content' | ||
} | ||
]; | ||
} | ||
}; |
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
57 changes: 57 additions & 0 deletions
57
test/spectral/tests/custom-rules/parameter-schema-or-content.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,57 @@ | ||
const inCodeValidator = require('../../../../src/lib'); | ||
|
||
describe('spectral - test validation that schema provided in content object', function() { | ||
it('should error only when parameter does not provide a schema or content', async () => { | ||
const spec = { | ||
openapi: '3.0.0', | ||
paths: { | ||
createPet: { | ||
post: { | ||
operationId: 'addPet', | ||
parameters: [ | ||
{ | ||
name: 'exampleParam1', | ||
description: 'example param 1', | ||
in: 'query' | ||
// no schema or content object | ||
} | ||
] | ||
} | ||
}, | ||
createOwner: { | ||
post: { | ||
operationId: 'addOwner', | ||
parameters: [ | ||
{ | ||
name: 'exampleParam1', | ||
description: 'example param 1', | ||
in: 'query', | ||
schema: { | ||
type: 'string' | ||
} | ||
}, | ||
{ | ||
name: 'exampleParam2', | ||
description: 'example param 2', | ||
in: 'query', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const res = await inCodeValidator(spec, true); | ||
const expectedErrors = res.errors.filter( | ||
err => err.message === 'Parameter must provide either a schema or content' | ||
); | ||
expect(expectedErrors.length).toBe(1); | ||
}); | ||
}); |