Skip to content

Commit

Permalink
feat: examples' name should not contain space (#332)
Browse files Browse the repository at this point in the history
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
Andras-Csanyi committed Sep 29, 2021
1 parent f3280e8 commit 053d9da
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/spectral/rulesets/.defaultsForSpectral.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@ rules:
- $.servers[*][variables][*][default]
then:
function: truthy
# ensure that examples name does not contain space
examples-name-contains-space:
description: 'Examples name should not contain space'
message: "{{description}}"
severity: warn
resolved: false
formats: ["oas3"]
given:
- $.paths[*][*].responses[*][*][*].examples[*]~
then:
function: pattern
functionOptions:
notMatch: '^(.*\s+.*)+$'
# ensure summary field does not have trailing comma
prohibit-summary-sentence-style:
description: 'Summary should not have a trailing period'
Expand Down
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'

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'

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 test/test-utils/get-message-and-path-from-captured-text.js
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;
}
1 change: 1 addition & 0 deletions test/test-utils/index.js
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;

0 comments on commit 053d9da

Please sign in to comment.