Checking for example or examples
savage-alex opened this issue · 2 comments
savage-alex commented
I have the following rule that is asking my designers to add examples for query params:
description: All query parameters SHOULD have an example.
severity: error
message: 'All query parameters SHOULD have an example.'
given: "$..parameters[?(@.in == 'query')]"
then:
field: example
function: truthy
I would also like it to accept when a designer has included "examples" as well (Hopefully through an OR field+truthy)
OAS syntax for multiple examples:
parameters:
- name: employeeId
description: A filter to include one or more employees' absences. A maximum of 20 employees' absences can be requested. The absences are grouped by employee.
required: false
in: query
schema:
type: string
style: form
explode: false
examples:
oneId:
value: A1234
multipleIds:
value: A12134,A13,M12334
Is this something you have thought of as a good rule?
P0lip commented
Hey!
Unfortunately, Spectral does not support OR just yet. We have a pending feature request that can be found here stoplightio/spectral#1276.
For the time being, you will need to write a custom function.
An example function for your use case could look as follows
module.exports = function (targetVal) {
if (!targetVal.example && !targetVal.examples) {
return { message: 'some message' };
}
};
For more info regarding the usage of custom functions please refer to docs
savage-alex commented
Duplicate as discussed above.
Thanks @P0lip !