apisyouwonthate / style-guide

A shared and somewhat opinionated style guide for everyone to enjoy.
139 stars 20 forks source link

Checking for example or examples #12

Closed savage-alex closed 3 years ago

savage-alex commented 3 years ago

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 3 years ago

Hey! Unfortunately, Spectral does not support OR just yet. We have a pending feature request that can be found here https://github.com/stoplightio/spectral/issues/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 3 years ago

Duplicate as discussed above.

Thanks @P0lip !