Joe
Joe

Reputation: 856

Is it possible for Spectral (or another tool) to enforce query parameter / response object pairs within an OpenAPI / Swagger spec?

I have an OpenAPI 3.0 specification, and I am using Spectral to enforce some simple rules like naming conventions and the like.

However, there other more complex situations that I have been trying to figure out a pattern for without much luck.

For instance:

In the following example, notice that GET requires some pagination parameters. Also, the 200 response includes a PaginationStats object in addition to an array of ChildOrg objects.

paths:

/organizations/{organizationId}/childorgs:
    get:
      summary: Returns all child orgs for a given parent org.
      operationId: getChildOrgs
      parameters:
        - name: organizationId
          in: path
          description: The parent organization's ID
          required: true
          schema:
            type: string
        - $ref: '#/components/parameters/PaginationSorts'
        - $ref: '#/components/parameters/PaginationPageSize'
        - $ref: '#/components/parameters/PaginationOffset'
      responses:
        '200':
          description: ok
          content:
            application/json:
              schema:
                properties:
                  childOrgs:
                    type: array
                    items:
                      $ref: '#/components/schemas/ChildOrg'
                  paginationStats:
                    type: object
                    $ref: '#/components/schemas/PaginationStats'

I want to enforce that if a pagination parameter is included as a query parameter, that a PaginationStats object must also be included as part of the 200 response.

Unfortunately the OpenAPI 3 spec does not accomodate enforcing a rule like this.

Things I've considered:

Of course, I could build some custom tool or custom spectral function from scratch to enforce this, but I'd prefer to use an existing toolset.

Is Spectral capable of handling this? If so, what pattern might I consider? Are there other yaml/json linting tools that might be more suited to this kind of application?

Upvotes: 0

Views: 80

Answers (0)

Related Questions