Skip to content

Reference to an array #903

@petermarcoen

Description

@petermarcoen

How do I allow a reference to an array in my schema?
For example, say I have an object that contains multiples of an array of numbers:

{
  "multiplesOf": [
    {
      "of": 1,
      "values": [1,2,3,4,5],
      "results": [1,2,3,4,5]
    },
    {
      "of": 2,
      "values": [1,2,3,4,5],
      "results": [2,4,6,8,10]
    }
  ]
}

I would write the following schema for this:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "properties": {
    "multiplesOf": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "of": {
            "type": "number"
          },
          "values": {
            "type": "array"
          },
          "results": {
            "type": "array"
          }
        }
      }
    }
  }
}

That validates fine, but suppose in my data I want to avoid writing the same values twice, so I seperate them into a seperate definition:

{
  "multiplesOf": [
    {
      "of": 1,
      "values": { "$ref": "#/values" },
      "results": [1,2,3,4,5]
    },
    {
      "of": 2,
      "values": { "$ref": "#/values" },
      "results": [2,4,6,8,10]
    }
  ],
  "values": [1,2,3,4,5]
}

Then every validator I tried gives me the following error: "Invalid type. Expected Array but got Object.".
In a way this is correct since the reference itself is an object.
Is there any way to allow this?

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