spectral icon indicating copy to clipboard operation
spectral copied to clipboard

Built in rule to validate request and response media types

Open savage-alex opened this issue 4 years ago • 3 comments

User story. As an API designer, I want to have my api definition validated and checked that the content types are valid So that I create beautiful consistent API definitions my consumers love

Is your feature request related to a problem? Currently its possible to create unknown media types (well its always possible but it can introduce accidental errors such as image

Describe the solution you'd like Spectral could offer a build in allow / accept list of media types (expect the usual base set and perhaps some extension) to highlight where errors have been made as shown above

Additional context Handmade rule based on https://github.com/openapi-contrib/style-guides/blob/c5326037027afb8bd0ce5a5d4ad88be7b048ef64/apisyouwonthate.yml#L130

response-media-type:
    description: >-
      Every response body SHOULD be either application/json,
      application/xml or text/csv
    formats:
      - oas3
    severity: warn
    given: $.paths.[*].responses[?(@property.match(/^(2)/))].content.*~
    then:
      function: enumeration
      functionOptions:
        values:
          - application/json
          - application/xml
          - text/csv

savage-alex avatar Apr 29 '21 12:04 savage-alex

Could you define a valid vs invalid MIME type in this context? Does valid mean "only things in the IANA list?" Because there's a lot.

String comparisons never work because application/vnd.acme-v1+json; charset=utf-8 is application/json but with extra information and folks always seem to forget that.

We could use a mime type validator, but I question the necessity of this feature compared to the overhead of writing it and running it.

philsturgeon avatar Apr 30 '21 16:04 philsturgeon

Agree its a tricky one. @P0lip and I were discussing it on slack. Agree its a bit niche and it would likely need a base list + allow list to be defined.

savage-alex avatar May 04 '21 06:05 savage-alex

Ok, if somebody can work up a PR and add the ability to extend them in all for it, but it’s not something we’ll have time to work on in house I think. Gotta focus on everything for v6 and other big features beyond that for the moment.

philsturgeon avatar May 04 '21 08:05 philsturgeon