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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*.yaml
*.yml
*.html
!/tools/spectral/ipa/**/*.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-102-path-alternate-resource-name-path-param', [
{
name: 'valid paths - api/atlas/v2',
document: {
paths: {
'/api/atlas/v2/resourceName': {},
'/api/atlas/v2/resourceName/{pathParam}': {},
'/api/atlas/v2/resourceName1/{pathParam}/resourceName2': {},
'/api/atlas/v2/resourceName1/{pathParam1p}/resourceName2/{pathParam2}': {},
'/api/atlas/v2/resourceName/{pathParam}:method': {},
'/api/atlas/v2/custom:method': {},
'/api/atlas/v2': {},
},
},
errors: [],
},
{
name: 'valid paths - api/atlas/v2/unauth',
document: {
paths: {
'/api/atlas/v2/unauth/resourceName': {},
'/api/atlas/v2/unauth/resourceName/{pathParam}': {},
'/api/atlas/v2/unauth/resourceName1/{pathParam}/resourceName2': {},
'/api/atlas/v2/unauth/resourceName1/{pathParam1p}/resourceName2/{pathParam2}': {},
'/api/atlas/v2/unauth/resourceName/{pathParam}:method': {},
'/api/atlas/v2/unauth/custom:method': {},
'/api/atlas/v2/unauth': {},
},
},
errors: [],
},
{
name: 'invalid paths - api/atlas/v2',
document: {
paths: {
'/api/atlas/v2/resourceName1/resourceName2': {},
'/api/atlas/v2/resourceName/{pathParam1}/{pathParam2}': {},
'/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/resourceName3': {},
'/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}': {},
'/api/atlas/v2/{pathParam}': {},
'/api/atlas/v2/{pathParam1}/{pathParam2}': {},
},
},
errors: [
{
code: 'xgen-IPA-102-path-alternate-resource-name-path-param',
message: 'API paths must alternate between resource name and path params. http://go/ipa/102',
path: ['paths', '/api/atlas/v2/resourceName1/resourceName2'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-path-alternate-resource-name-path-param',
message: 'API paths must alternate between resource name and path params. http://go/ipa/102',
path: ['paths', '/api/atlas/v2/resourceName/{pathParam1}/{pathParam2}'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-path-alternate-resource-name-path-param',
message: 'API paths must alternate between resource name and path params. http://go/ipa/102',
path: ['paths', '/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/resourceName3'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-path-alternate-resource-name-path-param',
message: 'API paths must alternate between resource name and path params. http://go/ipa/102',
path: ['paths', '/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-path-alternate-resource-name-path-param',
message: 'API paths must alternate between resource name and path params. http://go/ipa/102',
path: ['paths', '/api/atlas/v2/{pathParam}'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-path-alternate-resource-name-path-param',
message: 'API paths must alternate between resource name and path params. http://go/ipa/102',
path: ['paths', '/api/atlas/v2/{pathParam1}/{pathParam2}'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid paths - api/atlas/v2/unauth',
document: {
paths: {
'/api/atlas/v2/unauth/resourceName1/resourceName2': {},
'/api/atlas/v2/unauth/resourceName/{pathParam1}/{pathParam2}': {},
'/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/resourceName3': {},
'/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}': {},
'/api/atlas/v2/unauth/{pathParam}': {},
'/api/atlas/v2/unauth/{pathParam1}/{pathParam2}': {},
},
},
errors: [
{
code: 'xgen-IPA-102-path-alternate-resource-name-path-param',
message: 'API paths must alternate between resource name and path params. http://go/ipa/102',
path: ['paths', '/api/atlas/v2/unauth/resourceName1/resourceName2'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-path-alternate-resource-name-path-param',
message: 'API paths must alternate between resource name and path params. http://go/ipa/102',
path: ['paths', '/api/atlas/v2/unauth/resourceName/{pathParam1}/{pathParam2}'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-path-alternate-resource-name-path-param',
message: 'API paths must alternate between resource name and path params. http://go/ipa/102',
path: ['paths', '/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/resourceName3'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-path-alternate-resource-name-path-param',
message: 'API paths must alternate between resource name and path params. http://go/ipa/102',
path: ['paths', '/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-path-alternate-resource-name-path-param',
message: 'API paths must alternate between resource name and path params. http://go/ipa/102',
path: ['paths', '/api/atlas/v2/unauth/{pathParam}'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-path-alternate-resource-name-path-param',
message: 'API paths must alternate between resource name and path params. http://go/ipa/102',
path: ['paths', '/api/atlas/v2/unauth/{pathParam1}/{pathParam2}'],
severity: DiagnosticSeverity.Warning,
},
],
},
]);
10 changes: 5 additions & 5 deletions tools/spectral/ipa/__tests__/eachResourceHasGetMethod.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,31 +95,31 @@ testRule('xgen-IPA-104-resource-has-GET', [
errors: [
{
code: 'xgen-IPA-104-resource-has-GET',
message: 'APIs must provide a get method for resources. http://go/ipa/117',
message: 'APIs must provide a get method for resources. http://go/ipa/104',
path: ['paths', '/standard'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-104-resource-has-GET',
message: 'APIs must provide a get method for resources. http://go/ipa/117',
message: 'APIs must provide a get method for resources. http://go/ipa/104',
path: ['paths', '/standard/{exampleId}/nested'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-104-resource-has-GET',
message: 'APIs must provide a get method for resources. http://go/ipa/117',
message: 'APIs must provide a get method for resources. http://go/ipa/104',
path: ['paths', '/standard/{exampleId}/nestedSingleton'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-104-resource-has-GET',
message: 'APIs must provide a get method for resources. http://go/ipa/117',
message: 'APIs must provide a get method for resources. http://go/ipa/104',
path: ['paths', '/custom'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-104-resource-has-GET',
message: 'APIs must provide a get method for resources. http://go/ipa/117',
message: 'APIs must provide a get method for resources. http://go/ipa/104',
path: ['paths', '/singleton'],
severity: DiagnosticSeverity.Warning,
},
Expand Down
1 change: 1 addition & 0 deletions tools/spectral/ipa/ipa-spectral.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
extends:
- ./rulesets/IPA-102.yaml
- ./rulesets/IPA-104.yaml
15 changes: 15 additions & 0 deletions tools/spectral/ipa/rulesets/IPA-102.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# IPA-102: Resource Identifiers
# http://go/ipa/102

functions:
- eachPathAlternatesBetweenResourceNameAndPathParam

rules:
xgen-IPA-102-path-alternate-resource-name-path-param:
description: 'Paths should alternate between resource names and path params. http://go/ipa/102'
message: '{{error}} http://go/ipa/102'
severity: warn
given: '$.paths'
then:
field: '@key'
function: 'eachPathAlternatesBetweenResourceNameAndPathParam'
10 changes: 5 additions & 5 deletions tools/spectral/ipa/rulesets/IPA-104.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ functions:

rules:
xgen-IPA-104-resource-has-GET:
description: "APIs must provide a get method for resources. http://go/ipa/104"
message: "{{error}} http://go/ipa/117"
description: 'APIs must provide a get method for resources. http://go/ipa/104'
message: '{{error}} http://go/ipa/104'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

severity: warn
given: "$.paths"
given: '$.paths'
then:
field: "@key"
function: "eachResourceHasGetMethod"
field: '@key'
function: 'eachResourceHasGetMethod'
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { isPathParam } from './utils/pathUtils';

const ERROR_MESSAGE = 'API paths must alternate between resource name and path params.';
const ERROR_RESULT = [{ message: ERROR_MESSAGE }];
const AUTH_PREFIX = '/api/atlas/v2';
const UNAUTH_PREFIX = '/api/atlas/v2/unauth';

const getPrefix = (path) => {
if (path.includes(UNAUTH_PREFIX)) return UNAUTH_PREFIX;
if (path.includes(AUTH_PREFIX)) return AUTH_PREFIX;
return null;
};

const validatePathStructure = (elements) => {
return elements.every((element, index) => {
const isEvenIndex = index % 2 === 0;
return isEvenIndex ? !isPathParam(element) : isPathParam(element);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return isEvenIndex ? !isPathParam(element) : isPathParam(element);
return isEvenIndex && !isPathParam(element);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the index is odd, it will return false this way. Am I missing anything?

});
};

export default (input) => {
const prefix = getPrefix(input);
if (!prefix) return;

let suffixWithLeadingSlash = input.slice(prefix.length);
if (suffixWithLeadingSlash.length === 0) {
return;
}

let suffix = suffixWithLeadingSlash.slice(1);
let elements = suffix.split('/');
if (!validatePathStructure(elements)) {
return ERROR_RESULT;
}
};
14 changes: 14 additions & 0 deletions tools/spectral/ipa/rulesets/functions/utils/pathUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Checks if a string belongs to a path parameter or a path parameter with a custom method.
*
* A path parameter has the format: `{paramName}`
* A path parameter with a custom method has the format: `{paramName}:customMethod`
*
* @param {string} str - A string extracted from a path split by slashes.
* @returns {boolean} True if the string matches the expected formats, false otherwise.
*/
export function isPathParam(str) {
const pathParamRegEx = new RegExp(`^{[a-z][a-zA-Z0-9]*}$`);
const pathParamWithCustomMethodRegEx = new RegExp(`^{[a-z][a-zA-Z0-9]*}:[a-z][a-zA-Z0-9]*$`);
return pathParamRegEx.test(str) || pathParamWithCustomMethodRegEx.test(str);
}
Loading