Skip to content

Add array validation keywords #36

Description

@crmne

Goal

Add support for array-specific Draft 2020-12 validation keywords:

  • uniqueItems
  • prefixItems
  • contains
  • minContains
  • maxContains

Meanings

uniqueItems means an array cannot contain duplicate values.

prefixItems is JSON Schema's tuple-style positional validation. The name is awkward, but it means "the first item must match this schema, the second item must match this other schema, and so on."

contains means the array must contain items matching a schema.

minContains and maxContains control how many items must match the contains schema.

Proposed DSL for unique arrays

array :tags, of: :string, unique: true

Proposed DSL for tuple arrays

tuple :coordinates do
  number description: "Latitude"
  number description: "Longitude"
end

Generated shape:

{
  "type": "array",
  "prefixItems": [
    { "type": "number", "description": "Latitude" },
    { "type": "number", "description": "Longitude" }
  ],
  "minItems": 2,
  "maxItems": 2
}

Bigger example:

tuple :event do
  string description: "Event name"
  integer description: "Unix timestamp"
  object description: "Payload" do
    string :id
    string :source
  end
end

Valid value:

["created", 1710000000, { "id": "evt_1", "source": "api" }]

Invalid value:

[1710000000, "created", { "id": "evt_1" }]

Proposed DSL for contains

array :scores do
  contains min: 1, max: 3 do
    integer minimum: 10
  end
end

This means the array must contain between 1 and 3 integers greater than or equal to 10.

Acceptance criteria

  • Existing homogeneous items array behavior remains.
  • Tuple behavior is tested.
  • contains, minContains, and maxContains behavior is tested.
  • Generated schemas validate against Draft 2020-12.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions