-
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 non-object request bodies (#288)
- Loading branch information
1 parent
028c57d
commit ba00a8d
Showing
3 changed files
with
121 additions
and
0 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
81 changes: 81 additions & 0 deletions
81
test/spectral/tests/custom-rules/request-body-object.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,81 @@ | ||
const inCodeValidator = require('../../../../src/lib'); | ||
|
||
describe('spectral - test that request body schema is an object', function() { | ||
it('should error only when request body is not an object', async () => { | ||
const spec = { | ||
openapi: '3.0.0', | ||
paths: { | ||
'/path1': { | ||
post: { | ||
operationId: 'addPet', | ||
requestBody: { | ||
content: { | ||
'application/json': { | ||
schema: { | ||
// no error | ||
type: 'object', | ||
properties: { | ||
prop1: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
put: { | ||
operationId: 'updatePet', | ||
requestBody: { | ||
content: { | ||
'application/json': { | ||
schema: { | ||
// error 1 | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
patch: { | ||
operationId: 'patchPet', | ||
requestBody: { | ||
content: { | ||
'application/json': { | ||
schema: { | ||
// no error, properties provided | ||
properties: { | ||
prop1: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const res = await inCodeValidator(spec, true); | ||
const expectedErrors = res.errors.filter( | ||
err => | ||
err.message === 'All request bodies MUST be structured as an object' | ||
); | ||
expect(expectedErrors.length).toBe(1); | ||
expect(expectedErrors[0].path).toEqual([ | ||
'paths', | ||
'/path1', | ||
'put', | ||
'requestBody', | ||
'content', | ||
'application/json', | ||
'schema', | ||
'type' | ||
]); | ||
}); | ||
}); |