Custom rule for application/problem+json using Stoplight Spectral CLI

926 Views Asked by At

I'm trying to make a custom rule based on the unkown-error-format that can be found here here. Using this Open API document as an example:

openapi-generated.yaml


openapi: 3.0.3
info:
  title: API
  version: 1.0.0
servers:
- url: https://api.com/
paths:
  /:
    get:
      tags: []
      summary: Your GET endpoint
      description: Your GET endpoint
      operationId: get-endpoint
      responses:
        "500":
          description: Error
          content:
            application/json:
              schema:
                properties:
                  errorDescription:
                    type: string

and this rule set:

.spectral.yml

extends:
  - https://raw.githubusercontent.com/openapi-contrib/style-guides/master/apisyouwonthate.yml

rules:
  unknown-error-format: error

When running

spectral lint --ruleset .spectral.yml openapi-generated.yaml

I was expecting an error to be returned, because the 500 response content is application/json and not one of the allowed values application/vnd.api+json, application/problem+xml, and application/problem+json.

Instead no errors are found:

No results with a severity of 'error' or higher found!

I experimented with some other Spectral core functions that should make the linter return an error, but it does not. I suspect the expression in given is not returning the right result, internally.

Additionally, I tried using this JSON Path Demo to check if the output array with "application/json" is returned for the path in the rule's given. It does.

Is there a problem with this rule or am I doing something wrong?

I'm using Spectral version 6.1.0.

Thanks in advance.

0

There are 0 best solutions below