A. Gladkiy
A. Gladkiy

Reputation: 3450

json schema validate empty array of objects

var schema = {
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "http://example.com/example.json",
  "type": "object",
  "required": [
    "data"
  ],
  "properties": {
    "data": {
      "$id": "#/properties/data",
      "type": "object",
      "required": [
        "id",
        "name",
        "description",
        "groups"
      ],
      "properties": {
        "id": {
          "$id": "#/properties/data/properties/id",
          "type": "integer"
        },
        "name": {
          "$id": "#/properties/data/properties/name",
          "type": "string"
        },
        "description": {
          "$id": "#/properties/data/properties/description",
          "type": "string"
        },
        "groups": {
          "$id": "#/properties/data/properties/groups",
          "type": "array",
          "items": {
            "$id": "#/properties/data/properties/groups/items",
            "anyOf": [
              {
                "$id": "#/properties/data/properties/groups/items/anyOf/0",
                "type": "object",
                "required": [
                  "id",
                  "name",
                  "description"
                ],
                "properties": {
                  "id": {
                    "$id": "#/properties/data/properties/groups/items/anyOf/0/properties/id",
                    "type": "integer"
                  },
                  "name": {
                    "$id": "#/properties/data/properties/groups/items/anyOf/0/properties/name",
                    "type": "string"
                  },
                  "description": {
                    "$id": "#/properties/data/properties/groups/items/anyOf/0/properties/description",
                    "type": "string"
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
};

And I have json result either:

{
    "data": {
        "id": 1,
        "name": "Contacts",
        "description": "Pre-installed contacts management for your account.",
        "groups": []
    }
}

or

{
    "data": {
        "id": 4,
        "name": "Update Container Sub Type 913",
        "description": "I'll calculate the virtual SQL bus, that should circuit the FTP sensor!",
        "groups": [
            {
                "id": 1,
                "name": "tech",
                "description": "tech description group"
            }
        ]
    }
}

And when I validate json schema, the second - valid, the first - invalid. How can I resolve it to do both as valid?

Upvotes: 4

Views: 8996

Answers (2)

lbodevan
lbodevan

Reputation: 153

As described in the JSON Schema Specification version 2020-12, it's possible to validate the minimum amount of items inside an array. Just use the property minItems.

You can write something like this:

{
    "$id": "#/properties/data/properties/groups",
    "type": "array",
    "minItems": 1,
    "items": {
       ...
    }
}

Upvotes: 3

jmbourdaret
jmbourdaret

Reputation: 261

edited: ** This does not answer the question **

Option 1 : Validators and Json-schema versions

edited: deleted wrong information.

Option 2 : discard the anyOf

The problem might be the anyOf: it means one or more of these , therefore, group cannot be empty. See the docs on json-schema.org for more info.

But why do you need an anyOf , when you only have one type of items ? Try :

 {
  "$schema": "http://json-schema.org/draft-07/schema#",

  "type": "object",
  "required": [
    "data"
  ],
  "properties": {
    "data": {
      "$id": "#/properties/data",
      "type": "object",
      "required": [
        "id",
        "name",
        "description",
        "groups"
      ],
      "properties": {
        "id": {
          "$id": "#/properties/data/properties/id",
          "type": "integer"
        },
        "name": {
          "$id": "#/properties/data/properties/name",
          "type": "string"
        },
        "description": {
          "$id": "#/properties/data/properties/description",
          "type": "string"
        },
        "groups": {
          "$id": "#/properties/data/properties/groups",
          "type": "array",
          "items": {
                "$id": "#/properties/data/properties/groups/items/anyOf/0",
                "type": "object",
                "required": [
                  "id",
                  "name",
                  "description"
                ],
                "properties": {
                  "id": {
                    "$id": "#/properties/data/properties/groups/items/anyOf/0/properties/id",
                    "type": "integer"
                  },
                  "name": {
                    "$id": "#/properties/data/properties/groups/items/anyOf/0/properties/name",
                    "type": "string"
                  },
                  "description": {
                    "$id": "#/properties/data/properties/groups/items/anyOf/0/properties/description",
                    "type": "string"
                  }
                }
          }
        }
      }
    }
  }
}

So you can get rid of the anyOf constraints , and validate an empty array as it is allowed by default.

Option 3 : conditional schemas

If your validator doesn't accept an empty array of anyOfs (some does) , There might be a possibility to work something out with conditional structures like if, then ... see reference. something in the lines of :

  • if array size is > 0 then apply the "anyOf" sub-schema

Please let us know how you solved this issue.

Upvotes: 2

Related Questions