stoplightio / spectral

A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI (v3.1, v3.0, and v2.0), Arazzo v1.0, as well as AsyncAPI v2.x.
https://stoplight.io/spectral
Apache License 2.0
2.52k stars 240 forks source link

Auto escape square brackets in URI validations #1689

Open ThomasKoppensteiner opened 3 years ago

ThomasKoppensteiner commented 3 years ago

User story. As an API designer, I can use square brackets in URI strings so that the validation is still valid.

Is your feature request related to a problem? I would like to use square brackets in paginations links (for convenience and readability reasons), without the need to url-encoded them.

{
  "links": {
     "next":  "http://example.com?page[size]=3&page[number]=2"
  }
}

Describe the solution you'd like Squared brackets are automatically url-encoded before the validation in case string <uri> or string <uri-reference> is used.

Additional context Error message "next" property must match format "uri-reference"

faevourite commented 2 years ago

Just adding another example here:

openapi: 3.1.0
info:
  title: URI Reference Test Case
  version: '1.0'
servers:
  - url: 'http://localhost:3000'
paths:
  /thing:
    parameters: []
    get:
      summary: ''
      operationId: get-thing
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  uri-broken:
                    type: string
                    format: uri-reference
                    readOnly: true
                  uri-working:
                    type: string
                    format: uri-reference
                    readOnly: true
              examples:
                example-1:
                  value:
                    uri-broken: 'https://example.com/some/endpoint?page[cursor]=123'
                    uri-working: 'https://example.com/some/endpoint?page%5Bcursor%5D=123'
components:
  schemas: {}

This results in a oas3-valid-media-example error in Stoplight UI and Spectral lint for the uri-broken field. This means that for APIs based on JSON:API, for example, which uses square brackets extensively, API designers have to constantly encode square brackets even though they're valid characters in a URI.

Another point of reference: https://jsonschemalint.com/#!/version/draft-07/markup/json#%2Fversion%2Fdraft-04%2Fmarkup%2Fjson . Using this schema:

{
  "type": "object",
  "properties": {
    "test": {
      "type": "string",
      "format": "uri-reference"
    }
  }
}

validates correctly for this doc:

{
  "test": "https://example.com/some/endpoint?page[cursor]=123"
}