Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add custom spectral ruleset #93

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ install:
npm install

spectral: install
./node_modules/.bin/spectral lint spec/json/twilio_*.json -Dq

./node_modules/.bin/spectral lint spec/json/twilio_*.json -Dq --verbose
test: spectral

test-docker:
Expand Down
62 changes: 62 additions & 0 deletions functions/operations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { containsPathInstance, isInstancePath, HTTP_OPS } from './utils';


const instanceMap = {
"post": "Update",
"get": "Fetch",
"delete": "Delete"
};

const listMap = {
"get" : "List",
"post": "Create"
}


const urlOperation = (pathKey, pathObj, isInstance) => {
const results = []
const opMap = isInstance ? instanceMap : listMap
for (const [key, value] of Object.entries(pathObj)) {
if (!HTTP_OPS.includes(key)) {
continue
}
if (opMap[key] && !value.operationId.startsWith(opMap[key])) {
results.push(
{
message: "Error operationId for path " + pathKey + " has invalid operationId of " + value.operationId,
}
);
};
};
return results
}

const pathTypeOperation = (pathKey, pathObj) => {
const results = []
for (const [key, value] of Object.entries(pathObj)) {
if (!HTTP_OPS.includes(key)) {
continue
}
if (!value.operationId.startsWith(listMap[key]) || !value.operationId.startsWith(instanceMap[key])) {
const pathType = pathObj["x-twilio"]["pathType"] ;
results.push({
message: "Error operationId for path " + pathKey + " of defined pathType " + pathType + " has invalid operationId of " + value.operationId,
});
}
}
results;
}

export default (ops) => {
const results = []
for (const [pathKey, pathObj] of Object.entries(ops)) {
if (containsPathInstance(pathObj) ) {
results.concat(pathTypeOperation(pathKey, pathObj));
}
else{
results.concat(urlOperation(pathKey, pathObj, isInstancePath(pathKey)));
}
}
return results;
}

16 changes: 16 additions & 0 deletions functions/post_operations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { isInstancePath } from './utils';

export default (ops) => {
const results = []
for (const [pathKey, pathObj] of Object.entries(ops)) {
if (isInstancePath(pathKey)) {
if (pathObj["put"]) {
results.push(
{
message: "put operation not supported in instance path, please rename put to post ",
});
}
}
}
return results
}
34 changes: 34 additions & 0 deletions functions/recordkey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { containsPathInstance, isInstancePath } from './utils';

const isRecordKey = (value, pathKey) => {
const results = [];
let flag = false;
for (const [_, prop ] of Object.entries(value["responses"]["200"]["content"]["application/json"]["schema"]["properties"])) {
if (prop["type"] == "array") {
flag = true;
}
}
if (!flag) {
results.push(
{
message: "Missing record key for pathkey " + pathKey,
}
);
};
return results;
}


export default (ops) => {
const results = []
for (const [pathKey, pathObj] of Object.entries(ops)) {
if (!containsPathInstance(pathObj) && !isInstancePath(pathKey)) {
for (const [key, value] of Object.entries(pathObj)) {
if (key == "get") {
results.concat(isRecordKey(value, pathKey));
}
}
};
}
return results;
};
21 changes: 21 additions & 0 deletions functions/serverurl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = (ops) => {
const results = []
const info = ops.info
if (info.servers) {
return results
}
let flag = false;
for (const [_, pathObj] of Object.entries(ops.paths)) {
if (!pathObj.servers) {
flag = true
}
}
if (flag == true) {
results.push(
{
message: "Either add serverurl globally or add it serverurl for all the paths",
}
)
}
return results
}
18 changes: 18 additions & 0 deletions functions/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

export const containsPathInstance = (pathObj) => {
if (pathObj["x-twilio"] && pathObj["x-twilio"]["pathType"] == "instance") {
return true;
}
return false;
}

export const isInstancePath = (path) => {
const paths = path.split("/");
const last = paths[paths.length - 1];
if (last[last.length - 1] == '}' || last.slice(last.length - 6 , last.length) == '}.json' ) {
return true;
}
return false;
};

export const HTTP_OPS = ["get", "post", "delete", "put", "patch"];
34 changes: 32 additions & 2 deletions spectral.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends: spectral:oas
functions: [recordkey, operations, serverurl, post_operations]
rules:
no-$ref-siblings: false

component-name-rule:
severity: error
recommended: true
Expand All @@ -15,7 +15,7 @@ rules:
pascal-case-name-rule:
severity: error
recommended: true
message: OperationId should be PascalCased.
message: OperationId should be pascalCased.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I think we kept P as Uppercase to annotate that first letter should be Uppercase int OperationId

type: style
given: "$.paths.[*].operationId"
then:
Expand Down Expand Up @@ -52,3 +52,33 @@ rules:
max: 50
min: 4

list-operation-record-key:
message: "Response object does not have 2xx operation or default set"
given: "$.paths"
severity: error
then:
function: recordkey

operation-id-convention:
message: "{{error}}"
given: "$.paths"
severity: error
then:
function: operations

valid-server-url:
message: "{{error}}"
given: "$"
severity: error
then:
function: serverurl

update-operation:
message: "{{error}}"
given: "$.paths"
severity: error
then:
function: post_operations

oas3-valid-oas-content-example: off
oas3-valid-oas-parameter-example: off