Skip to content

Commit

Permalink
feat(PO033): add new checks for valid elements in PO033 plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
DinoChiesa committed Dec 5, 2023
1 parent 7a97e24 commit dcf46f6
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
29 changes: 29 additions & 0 deletions lib/package/plugins/PO033-extractVariables-hygiene.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,35 @@ const onPolicy = function (policy, cb) {
}
}
});

["Variable", "URIPath", "QueryParam", "Header", "FormParam"].forEach(
(elementName) => {
policy.select(`/ExtractVariables/${elementName}`).forEach((elt) => {
const children = xpath.select("*", elt);
if (!children || children.length == 0) {
foundIssue = true;
policy.addMessage({
plugin,
line: elt.lineNumber,
column: elt.columnNumber,
message: `There should be at least one Pattern element as child of ${elt.nodeName}.`
});
} else {
children.forEach((childElement) => {
if (childElement.nodeName != "Pattern") {
foundIssue = true;
policy.addMessage({
plugin,
line: childElement.lineNumber,
column: childElement.columnNumber,
message: `Unexpected element '${childElement.nodeName}' as child of ${elt.nodeName}.`
});
}
});
}
});
}
);
}
if (typeof cb == "function") {
cb(null, foundIssue);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ExtractVariables name="EV-URIPath-with-lowercase-pattern-element">
<URIPath>
<pattern ignoreCase="false">/accounts/{id}</pattern>
</URIPath>
<Source clearPayload="false">request</Source>
</ExtractVariables>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ExtractVariables name="EV-Variable-with-no-Pattern">
<Variable>
</Variable>
<Source clearPayload="false">request</Source>
</ExtractVariables>
6 changes: 5 additions & 1 deletion test/fixtures/resources/PO033/fail/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ module.exports = {
"EV-Missing-Variable.xml":
"JSONPayload element exists but there is no Variable element.",
"EV-XML-bool.xml":
"XMLPayload/Variable/@type is (bool), must be one of boolean,double,float,integer,long,nodeset,string"
"XMLPayload/Variable/@type is (bool), must be one of boolean,double,float,integer,long,nodeset,string",
"EV-URIPath-with-lowercase-pattern-element.xml":
"Unexpected element 'pattern' as child of URIPath.",
"EV-Variable-with-no-Pattern.xml":
"There should be at least one Pattern element as child of Variable."
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ExtractVariables name="EV-URIPath-with-Pattern-element">
<URIPath>
<Pattern ignoreCase="false">/accounts/{id}</Pattern>
<Pattern ignoreCase="false">/foob/{id}</Pattern>
</URIPath>
<Source clearPayload="false">request</Source>
</ExtractVariables>
2 changes: 1 addition & 1 deletion test/specs/PO033-extractVariables-JSON-Variable-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe(`${testID} - policy passes ExtractVariables hygiene check`, function ()
.forEach(testOne);
});

describe(`${testID} - policy does not ExtractVariables hygiene check`, () => {
describe(`${testID} - policy does not pass ExtractVariables hygiene check`, () => {
const sourceDir = path.join(rootDir, "fail");
const expectedErrorMessages = require(path.join(sourceDir, "messages.js"));
const testOne = (shortFileName) => {
Expand Down

0 comments on commit dcf46f6

Please sign in to comment.