Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
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': {
post: {},
get: {},
},
'/api/atlas/v2/resourceName/{pathParam}': {
get: {},
patch: {},
delete: {},
},
'/api/atlas/v2/resourceName1/{pathParam}/resourceName2': {
post: {},
get: {},
},
'/api/atlas/v2/resourceName1/{pathParam1p}/resourceName2/{pathParam2}': {
get: {},
patch: {},
delete: {},
},
'/api/atlas/v2/resourceName/{pathParam}:method': {
post: {},
},
'/api/atlas/v2/custom:method': {
post: {},
},
'/api/atlas/v2': {
post: {},
},
},
},
errors: [],
},
{
name: 'valid paths - api/atlas/v2/unauth',
document: {
paths: {
'/api/atlas/v2/unauth/resourceName': {
post: {},
get: {},
},
'/api/atlas/v2/unauth/resourceName/{pathParam}': {
get: {},
patch: {},
delete: {},
},
'/api/atlas/v2/unauth/resourceName1/{pathParam}/resourceName2': {
post: {},
get: {},
},
'/api/atlas/v2/unauth/resourceName1/{pathParam1p}/resourceName2/{pathParam2}': {
get: {},
patch: {},
delete: {},
},
'/api/atlas/v2/unauth/resourceName/{pathParam}:method': {
post: {},
},
'/api/atlas/v2/unauth/custom:method': {
post: {},
},
'/api/atlas/v2/unauth': {
post: {},
},
},
},
errors: [],
},
{
name: 'invalid paths - api/atlas/v2',
document: {
paths: {
'/api/atlas/v2/resourceName1/resourceName2': {
post: {},
get: {},
},
'/api/atlas/v2/resourceName/{pathParam1}/{pathParam2}': {
patch: {},
delete: {},
},
'/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/resourceName3': {
patch: {},
delete: {},
},
'/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}': {
patch: {},
delete: {},
},
'/api/atlas/v2/{pathParam}': {
post: {},
get: {},
},
'/api/atlas/v2/{pathParam1}/{pathParam2}': {
post: {},
get: {},
},
},
},
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': {
post: {},
get: {},
},
'/api/atlas/v2/unauth/resourceName/{pathParam1}/{pathParam2}': {
patch: {},
delete: {},
},
'/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/resourceName3': {
patch: {},
delete: {},
},
'/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}': {
patch: {},
delete: {},
},
'/api/atlas/v2/unauth/{pathParam}': {
post: {},
get: {},
},
'/api/atlas/v2/unauth/{pathParam1}/{pathParam2}': {
post: {},
get: {},
},
},
},
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,34 @@
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?

});
};

// eslint-disable-next-line no-unused-vars
export default (input, _0, _1) => {
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('/');
return validatePathStructure(elements) ? [] : ERROR_RESULT;
};
5 changes: 5 additions & 0 deletions tools/spectral/ipa/rulesets/functions/utils/pathUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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