Scott H
Scott H

Reputation: 1

A json Schema with an array of a $ref or an enum

I would like to have a Json Schema that would enforce an array of $ref and an enum of null. I have accidentally defined a tuple - not what I want. Here is my current schema (note I must use draft-04):

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "version": "4.4.0",
    "title": "myCollection",
    "description": "Resume/CV",
    "type": "object",
    "properties": {
        "EmploymentHistories": {
            "type": "array",
            "items": {
                "oneOf": [
                    {
                        "$ref": "../../../Common/json/base/TextType.json#"
                    },
                    {
                        "enum": [
                            null
                        ]
                }
                ]
            },
            "additionalProperties": false
        }
    }
}

And here is an instance I would like:

{
    "EmploymentHistories": [
        {
            "value": "String",
            "languageCode": "aa"
        },
        {
            "value": "String",
            "languageCode": "aa"
        },
        null,
        null
    ]
}

But I am getting an error on validation like:

File D:\Dev\Proj\Recruiting\json\resumecv\samples\Untitled5.json is not valid. A value of type 'null' is not permitted here. Reason: it must be of one of the following types (see below) 'string' 'object' Hint: Either 'type' is present and doesn't contain 'null' or 'enum' is present and doesn't contain a value of type 'null'. Error location: EmploymentHistories / 3 Details Array item '2' is not valid. Property 'EmploymentHistories' is not valid. A value of type 'null' is not permitted here. Reason: it must be of one of the following types (see below) 'string' 'object' Hint: Either 'type' is present and doesn't contain 'null' or 'enum' is present and doesn't contain a value of type 'null'. Error location: EmploymentHistories / 4 Details Array item '3' is not valid. Property 'EmploymentHistories' is not valid.

Any help is appreciated.n

Upvotes: 0

Views: 160

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24489

This looks like a bug in the validator implementation you are using. It seems to be saying that "enum": [null] is not allowed in a schema. The error is incorrect. This should be perfectly fine. However, you can probably work around this bug by changing it to "type": "null", which should have the same effect.

Upvotes: 1

Related Questions