Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Null or reference? #151

Closed
felsspat opened this issue Nov 10, 2021 · 3 comments
Closed

Question: Null or reference? #151

felsspat opened this issue Nov 10, 2021 · 3 comments
Labels
question Issues that look for answers.

Comments

@felsspat
Copy link

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
Copy link
Author

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

@ChALkeR ChALkeR added the question Issues that look for answers. label Oct 4, 2022
@ChALkeR
Copy link
Contributor

ChALkeR commented Oct 4, 2022

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
Copy link
Contributor

ChALkeR commented Oct 6, 2022

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

@ChALkeR ChALkeR closed this as completed Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

2 participants