Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking for example or examples #12

Closed
savage-alex opened this issue Oct 14, 2020 · 2 comments
Closed

Checking for example or examples #12

savage-alex opened this issue Oct 14, 2020 · 2 comments

Comments

@savage-alex
Copy link
Contributor

savage-alex commented Oct 14, 2020

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
Copy link
Contributor

P0lip commented Oct 19, 2020

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
Copy link
Contributor Author

Duplicate as discussed above.

Thanks @P0lip !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants