schemasafe icon indicating copy to clipboard operation
schemasafe copied to clipboard

Question: Null or reference?

Open felsspat opened this issue 3 years ago • 1 comments

This is probably not a problem with schemasafe, just a question:

Is it possible to have a property either be a reference or null?

In Webstorm for example this works if I want media to be nullable:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "my.schema",
    "type": "object",
    "additionalProperties": false,
    "title": "my secret schema",
    "required": [
        "ownerNumber"
    ],
    "media": {
        "oneOf": [
            {
                "type": "null"
            },
            {
                "$ref": "media.schema.json"
            }
        ]
    }
}

If I send media as object everything works as it should, but if I send media: null I get this error message from schemasafe:

{
    "keywordLocation": "#/properties/objects/items/$ref/properties/media/oneOf/1/$ref/type",
    "instanceLocation": "#/objects/0/media"
}

So is it possible to allow both null and the reference?

felsspat avatar Nov 10 '21 12:11 felsspat

Media is actually under "properties", I didn't want to post my whole schema.

felsspat avatar Nov 12 '21 13:11 felsspat

Perhaps it satisfies both conditions, and oneOf is looking for exactly one per spec?

Does using anyOf help? It's hard to tell without media.schema.json (i.e. does null pass it or not).

See https://json-schema.org/understanding-json-schema/reference/combining.html#oneof for an explainer.

Also, sorry for the late response.

ChALkeR avatar Oct 04 '22 22:10 ChALkeR

Going to close as answered, please comment if this should be reopened.

ChALkeR avatar Oct 06 '22 22:10 ChALkeR