Skip to content

Allow 'type' to be array, based on JSON Schema Draft4(partial solution for 'oneOf' problem). #624

@IvanGoncharov

Description

@IvanGoncharov

Spin off from #333, see this comment.

JSON Schema Draft4 supports arrays as values of type properties:

The value of this keyword MUST be either a string or an array. If it is an array, elements of the array MUST be strings and MUST be unique.
String values MUST be one of the seven primitive types defined by the core specification.

So you can write:

{
  "type": ["string", "array"],
  "items": {
     "type": "string"
  }
}

And it validates both string and array of strings.

It can be easily implemented in static-typed languages. Let's imagine following OpenAPI spec:

swagger: '2.0'
info:
  version: 0.0.0
  title: Simple API
paths:
  /:
    get:
      responses:
        200:
          description: OK
          schema: '#/definitions/Data'
definitions:
  Data:
    type:
      - string
      - array
    items:
      type: string

Based on it you can generate base class Data and two childs DataString and DataArray.
An appropriate class can be easily chosen based on data themselves based on type of actual JSON value.
So functional it's very similar to how discriminator works.

I think such step towards JSON Schema resolves a lot of oneOf controversial without introducing too many problems for underlining tools.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions