Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion docs/ibm-cloud-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -2400,7 +2400,7 @@ components:
<td valign=top><b>Description:</b></td>
<td>Each path defined within the API definition should include a path segment for the API major version,
of the form <code>v&lt;n&gt;</code>, and all paths should have the same API major version segment.
The API major version can appear in either the server URL or in each path entry.
The API major version can appear in either the server URL or in each path entry. In the path entry, the first segment of the API's path must be the major version of the API.
</td>
</tr>
<tr>
Expand Down
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 @@ -127,6 +127,16 @@ function checkMajorVersion(apiDef) {
];
}

if (!versionIsFirstInPaths(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",
path: ['paths'],
},
];
}

if (versions.length >= 1 && versions[0]) {
// Major version present in server URL and all match -- all good
logger.debug(
Expand Down Expand Up @@ -197,3 +207,15 @@ function getDefaultUrl(server) {

return urlString;
}

function versionIsFirstInPaths(paths) {
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);
});
});