Skip to content

Commit

Permalink
fix: add operation-operationId-valid-in-url (#86)
Browse files Browse the repository at this point in the history
to check for valid url characters in operationId
  • Loading branch information
Marc MacLeod authored and tbarn committed Feb 1, 2019
1 parent 3f7af7d commit dd46d06
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`operation-operationId-valid-in-url return errors if operationId contains invalid characters 1`] = `
Array [
Object {
"message": "must match the pattern '^[A-Za-z0-9-._~:/?#\\\\[\\\\]@!\\\\$&'()*+,;=]*$'",
"name": "operation-operationId-valid-in-url",
"path": Array [
"paths",
"/todos",
"get",
"operationId",
],
"severity": 40,
"severityLabel": "warn",
"summary": "operationId may only use characters that are valid when used in a URL.",
},
]
`;
41 changes: 41 additions & 0 deletions src/rulesets/oas/__tests__/operation-operationId-valid-in-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Spectral } from '../../../spectral';
import { commonOasRules } from '../index';

const ruleset = { rules: commonOasRules() };

describe('operation-operationId-valid-in-url', () => {
const s = new Spectral();
s.addRules({
'operation-operationId-valid-in-url': Object.assign(ruleset.rules['operation-operationId-valid-in-url'], {
enabled: true,
}),
});

test('validate a correct object', () => {
const results = s.run({
swagger: '2.0',
paths: {
'/todos': {
get: {
operationId: "A-Za-z0-9-._~:/?#[]@!$&'()*+,;=",
},
},
},
});
expect(results.results.length).toEqual(0);
});

test('return errors if operationId contains invalid characters', () => {
const results = s.run({
swagger: '2.0',
paths: {
'/todos': {
get: {
operationId: 'foo-^^',
},
},
},
});
expect(results.results).toMatchSnapshot();
});
});
13 changes: 13 additions & 0 deletions src/rulesets/oas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,19 @@ export const commonOasRules = (): RuleCollection => ({
},
tags: ['operation'],
},
'operation-operationId-valid-in-url': {
summary: 'operationId may only use characters that are valid when used in a URL.',
type: RuleType.VALIDATION,
given: operationPath,
then: {
field: 'operationId',
function: RuleFunction.PATTERN,
functionOptions: {
match: `^[A-Za-z0-9-._~:/?#\\[\\]@!\\$&'()*+,;=]*$`,
},
},
tags: ['operation'],
},
'operation-singular-tag': {
enabled: false,
summary: 'Operation may only have one tag.',
Expand Down

0 comments on commit dd46d06

Please sign in to comment.