Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/ruleset/src/functions/major-version-in-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ function checkMajorVersion(apiDef) {
return [];
}

if (!verionIsFirstInPaths(urls) && versionIsInPath(urls)) {
logger.debug(`${ruleId}: first segment of path isn't the major version`);
return [
{
message: "First segment of path isn't the major version of the API",
Comment thread
diatrcz marked this conversation as resolved.
Outdated
path: ['paths'],
},
];
}

Comment thread
diatrcz marked this conversation as resolved.
Outdated
const versions = urls.map(url => getVersion(url));

if (versions.length > 1 && !versions.every(v => v === versions[0])) {
Expand Down Expand Up @@ -197,3 +207,15 @@ function getDefaultUrl(server) {

return urlString;
}

function verionIsFirstInPaths(paths) {
Comment thread
diatrcz marked this conversation as resolved.
Outdated
const versionRegex = /^\/v\d+\//;

return paths.every(path => versionRegex.test(path));
}

function versionIsInPath(paths) {
const versionRegex = /\/v\d+\//;

return paths.every(path => versionRegex.test(path));
}
17 changes: 17 additions & 0 deletions packages/ruleset/test/rules/major-version-in-path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,21 @@ describe(`Spectral rule: ${ruleId}`, () => {
expect(validation.path).toStrictEqual([]);
expect(validation.severity).toBe(severityCodes.warning);
});

it('should error when paths start with different versions', async () => {
const testDocument = makeCopy(rootDocument);
testDocument.paths['metadata/v1/some_path'] = {};

const results = await testRule(ruleId, rule, testDocument);

expect(results).toHaveLength(1);

const validation = results[0];
expect(validation.code).toBe(ruleId);
expect(validation.message).toBe(
"First segment of path isn't the major version of the API"
);
expect(validation.path).toStrictEqual(['paths']);
expect(validation.severity).toBe(severityCodes.warning);
});
});