Hari Sundararajan
Hari Sundararajan

Reputation: 678

json schema for an array of items (referenced earlier in the schema)

I am trying to come up with a schema for a json document that is , at the top level, an array of items. Each item describes a "git repo" we have with some mappings. Why is this failing?

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://i.am.awesome.com",
    "title": "title of the schema for our projects",
    "description": "description of the schema for our projects",
    "definitions": {
        "proj": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string"
                },
                "visibility": {
                    "type": "string",
                    "enum": [
                        "private",
                        "public"
                    ]
                },
                "languages": {
                    "type": "array",
                    "minItems": 2,
                }
            },
            "required": [
                "name",
                "visibility",
                "languages",
            ]
        }
    },
    "type": "array",
    "items": {
        "type": {
            "$ref": "#/definitions/proj"
        }
    }
}

I am using python 3.8 with jsonschema and get this error

Failed validating 'anyOf' in metaschema['properties']['items']:
    {'anyOf': [{'$ref': '#'}, {'$ref': '#/definitions/schemaArray'}],
     'default': True}

On schema['items']:
    {'type': {'$ref': '#/definitions/proj'}}

What's interesting is that if I don't care for a list, and am schema checking a single element, simply having

$ref": "#/definitions/proj

So my referencing is correct, just don't know why it doesn't work for a list of the same items.

Upvotes: 1

Views: 1663

Answers (1)

Carsten
Carsten

Reputation: 2147

The $ref should be contained directly in the items keyword and not under items.type.
type is a reserved keyword that can only be a string or an array but not an object. That makes your schema invalid.

This would be a valid schema (omitting some details for readability):

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "proj": {
            "type": "object"
        }
    },
    "type": "array",
    "items": {
        "$ref": "#/definitions/proj"
    }
}

Upvotes: 3

Related Questions