Rule for Confirming Header Exists in OpenAPI Spec #2291
-
Hello everyone. I am trying to write a custom rule in Spectral to ensure that requests use a "Ocp-Apim-Subscription-Key" parameter in the header. I am having trouble getting the rule to work. Can someone please help me? This is what my custom rule looks like: I have attached part of I want my custom rule to apply when I remove the "Ocp-Apim-Subscription-Key" parameter from line 20. But it's not working. Any help would be greatly appreciated. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I like to do it this way: the rulegiven: the functionmodule.exports = (targetVal, opts) => { |
Beta Was this translation helpful? Give feedback.
I like to do it this way:
the rule
given:
- $.paths..parameters
severity: hint
then:
function: parameters-header
functionOptions:
name: Content-Location
type: header
the function
module.exports = (targetVal, opts) => {
const { name } = opts;
const { type } = opts;
if (targetVal) {
for (let i = 0; i < targetVal.length; i++) {
if (targetVal[i].in == type && targetVal[i].name == name) {
return;
}
}
return [{ message: 'Missing header parameter.' }];
}
}