Custom lint rule for OAS 3 to enforce all operations have a particular request header parameter

We have a required header parameter of a correlationId in all OAS 3 api calls. I am trying to determine how to make sure the parameters section of each operation has this header. But there may also be other headers defined as well. I can determine if the operation has a parameter section and that the length of that parameter array is at least 1, but I have not found a way to determine if at least one member of the array has the name = “CorrelationId”.

I tried this to make sure parameters exist

"parameter-object-must-exist-for-all-paths": {
		"description": "All Operations should have CorrelationId Parameter.",
		"message": "Operations should have CorrelationId Header Parameter",
		"recommended": true,
		"resolved": true,
		"severity": "warn",
		"type": "style",
		"given": "$.paths.*[?( @property === 'get' || @property === 'put' || @property === 'post' || @property === 'delete' || @property === 'options' || @property === 'head' || @property === 'patch' || @property === 'trace' )]",
		"then": [{
				"field": "parameters",
				"function": "truthy"
			},
			{
				"field": "parameters",
				"function": "length",
				"functionOptions": {
					"min": 1
				}
			}
		]
	},

and to check for the correlationId
“required-skytouch-correlationid-parameter”: {
“description”: “All Operations should have SkyTouch-CorrelationId Parameter.”,
“message”: “Operations should have SkyTouch-CorrelationId Header Parameter”,
“recommended”: true,
“resolved”: true,
“severity”: “warn”,
“type”: “style”,
“given”: “$.paths…parameters…name”,
“then”: [{
“field”: “name”,
“function”: “pattern”,
“functionOptions”: {
“match”: “.CorrelationId.
}
}
]
},
but that checks all parameters for the name not checking that at least one of the parameters has the name. Any suggestions would be appreciated.

Forgot to add I tried this as well

"required-skytouch-correlationid-parameterv2": {
		"description": "All Operations should have CorrelationId Parameter.",
		"message": "Operations should have CorrelationId Header Parameter",
		"recommended": true,
		"resolved": true,
		"severity": "warn",
		"type": "style",
		"given": "$.paths..parameters[?(@.name.match('.*CorrelationId.*'))]",
		"then": [{
				"function": "truthy"
			}
		]
	},

with truthy I get no results with falsy I get operations that have it not those that do not have it.

I apologize but I have not been able to find a way to attach a json test file and it will not let me paste it as code for some reasons (new use limit on links?). If someone can tell me how to post a test json openapi file I am happing to do so.

Thanks

I was unable to find a way to do this with existing functions so I created a new function

module.exports = (params, opts) => {

const reqParam = opts.param;
if (!Array.isArray(params)) return;

if (params.length < 1) return;

let reqParamExists = false;

let index = -1;

for (const param of params) {
index++;

if (param === null || typeof param !== 'object') continue;

// skip params that are refs
if ('$ref' in param) continue;

// Parameters must include name = string sent as option.

if (param.name == reqParam  && param.in == 'header') {
  reqParamExists = true;
} 

}

if (reqParamExists == false) {
return[
{
message: Required request parameter ${reqParam} does not exist,
}
];
} else {
return
}

};

with sample rule

"required-parameter-correlationid": {
		"description": "Required Request Parameter does not exist",
		"message": "{{error}}",
		"recommended": true,
		"type": "validation",
		"resolved": true,
		"given": "$..paths..parameters",
		"severity": "warn",
		"then": {
			"function": "requiredRequestParameter",
			"functionOptions": {
				"param": "CorrelationId"
			}
		}
	}

Not sure how to close issue, please close