-
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: examples' name should not contain space (#332)
This PR brings the capability to validate whether examples' names contain a space or not. Specification allows to put space in the examples' name, but the generator fails. If this case occurs the validator shows a warning.
- Loading branch information
1 parent
f3280e8
commit 053d9da
Showing
6 changed files
with
151 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
29 changes: 29 additions & 0 deletions
29
test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case.yaml
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,29 @@ | ||
openapi: 3.0.3 | ||
info: | ||
title: Examples name should not contain space | ||
description: Examples name should not contain space | ||
version: 1.0.0 | ||
servers: | ||
- url: 'https://example.com' | ||
paths: | ||
/v1/users: | ||
get: | ||
operationId: get_users | ||
summary: returns user list | ||
responses: | ||
200: | ||
description: returns list of users | ||
content: | ||
'application/json': | ||
schema: | ||
properties: | ||
id: | ||
type: string | ||
examples: | ||
success example: | ||
summary: 'successful example' | ||
value: 'success value' | ||
failed example: | ||
summary: 'failed request' | ||
value: 'failed request value' | ||
|
29 changes: 29 additions & 0 deletions
29
test/spectral/mockFiles/examples-name-should-not-contain-space/positive-case.yaml
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,29 @@ | ||
openapi: 3.0.3 | ||
info: | ||
title: Examples name should not contain space | ||
description: Examples name should not contain space | ||
version: 1.0.0 | ||
servers: | ||
- url: 'https://example.com' | ||
paths: | ||
/v1/users: | ||
get: | ||
operationId: get_users | ||
summary: returns user list | ||
responses: | ||
200: | ||
description: returns list of users | ||
content: | ||
'application/json': | ||
schema: | ||
properties: | ||
id: | ||
type: string | ||
examples: | ||
success: | ||
summary: 'successful example' | ||
value: 'success value' | ||
fail: | ||
summary: 'failed request' | ||
value: 'failed request value' | ||
|
54 changes: 54 additions & 0 deletions
54
test/spectral/tests/custom-rules/examples-name-contains-space.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,54 @@ | ||
const commandLineValidator = require('../../../../src/cli-validator/runValidator'); | ||
const { getCapturedText } = require('../../../test-utils'); | ||
const { getMessageAndPathFromCapturedText } = require('../../../test-utils'); | ||
|
||
describe('spectral - examples name should not contain space', () => { | ||
it('should not display error when examples name does not contain space', async () => { | ||
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
const program = {}; | ||
program.args = [ | ||
'./test/spectral/mockFiles/examples-name-should-not-contain-space/positive-case.yaml' | ||
]; | ||
program.default_mode = true; | ||
program.print_validator_modules = true; | ||
|
||
const exitCode = await commandLineValidator(program); | ||
const capturedText = getCapturedText(consoleSpy.mock.calls); | ||
|
||
const messages = getMessageAndPathFromCapturedText( | ||
'Examples name should not contain space', | ||
capturedText | ||
); | ||
|
||
expect(messages.length).toEqual(0); | ||
|
||
expect(exitCode).toEqual(0); | ||
}); | ||
|
||
it('should display error when multiple examples names contain space', async () => { | ||
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
const program = {}; | ||
program.args = [ | ||
'./test/spectral/mockFiles/examples-name-should-not-contain-space/negative-case.yaml' | ||
]; | ||
program.default_mode = true; | ||
program.print_validator_modules = true; | ||
|
||
const exitCode = await commandLineValidator(program); | ||
const capturedText = getCapturedText(consoleSpy.mock.calls); | ||
|
||
const messages = getMessageAndPathFromCapturedText( | ||
'Examples name should not contain space', | ||
capturedText | ||
); | ||
|
||
expect(messages[0][1].get('Path')).toEqual( | ||
'paths./v1/users.get.responses.200.content.application/json.examples.success example' | ||
); | ||
expect(messages[1][1].get('Path')).toEqual( | ||
'paths./v1/users.get.responses.200.content.application/json.examples.failed example' | ||
); | ||
|
||
expect(exitCode).toEqual(0); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
test/test-utils/get-message-and-path-from-captured-text.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,25 @@ | ||
module.exports.getMessageAndPathFromCapturedText = getMessageAndPathFromCapturedText; | ||
|
||
function getMessageAndPathFromCapturedText(pattern, capturedText) { | ||
const messages = []; | ||
for (let i = 0; i < capturedText.length; i++) { | ||
if (capturedText[i].includes(pattern)) { | ||
const aMessage = []; | ||
|
||
const messageMap = new Map(); | ||
const messageSplit = capturedText[i].split(':'); | ||
messageMap.set(messageSplit[0].trim(), messageSplit[1].trim()); | ||
aMessage.push(messageMap); | ||
|
||
const pathMap = new Map(); | ||
const pathSplit = capturedText[i + 1].split(':'); | ||
pathMap.set(pathSplit[0].trim(), pathSplit[1].trim()); | ||
aMessage.push(pathMap); | ||
|
||
messages.push(aMessage); | ||
// we jump the index by one due to that i+1 entry is already processed | ||
i++; | ||
} | ||
} | ||
return messages; | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
module.exports.getCapturedText = require('./get-captured-text').getCapturedText; | ||
module.exports.getCapturedTextWithColor = require('./get-captured-text').getCapturedTextWithColor; | ||
module.exports.getMessageAndPathFromCapturedText = require('./get-message-and-path-from-captured-text').getMessageAndPathFromCapturedText; |