Skip to content

I want to create a spectral ruleset that checks whether 200 or 400 responses have a schema.$ref tag #2775

Open
@nandeshwarshubh

Description

@nandeshwarshubh

I want to create a spectral ruleset that checks whether 200 or 400 responses have a schema.$ref tag

OPEN API YAML - myapi.yaml

openapi: 3.0.3
info:
  title: Simple API
  description: A simple OpenAPI specification with a single endpoint
  version: 1.0.0
paths:
  /example:
    post:
      summary: Example API endpoint
      description: Accepts a request DTO and returns a response DTO
      operationId: exampleOperation
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RequestDTO'
      responses:
        "200":
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ResponseDTO'
        "400":
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ExceptionDTO'
components:
  schemas:
    RequestDTO:
      type: object
      properties:
        id:
          type: string
          format: uuid
        name:
          type: string
      required:
        - id
        - name
    ResponseDTO:
      type: object
      properties:
        message:
          type: string
        timestamp:
          type: string
          format: date-time
      required:
        - message
        - timestamp
    ExceptionDTO:
      type: object
      properties:
        error:
          type: string
        details:
          type: string
      required:
        - error

My custom spectral ruleset - .spectral.yaml

rules:
  response-schema-ref-required:
    description: "Responses with status codes 200, 400, or 500 must have a schema with a $ref key."
    message: "Response schema must reference a schema via $ref."
    severity: error
    given: "$.paths[*][*].responses[200,400,500].content[*].schema"
    then:
      field: $ref
      function: truthy

run - npx spectral lint myapifile.yaml

Response -
/Users/myuser/Downloads/spectral-demo/myapifile.yaml
47:17 error response-schema-ref-required Response schema must reference a schema via $ref. components.schemas.ResponseDTO
59:18 error response-schema-ref-required Response schema must reference a schema via $ref. components.schemas.ExceptionDTO

Image

Why is it checking in components.schemas when we have specifically mentioned - given: "$.paths ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions