Openapi-specification: Is there a way to indicate conditional interdependencies between object properties in a model?

Created on 15 Aug 2017  路  4Comments  路  Source: OAI/OpenAPI-Specification

I have a use case where if the type property in my object is of value A then I another field in the object becomes required, and can only have a specific set of values which are associated with type A.

Is there any way of noting this type of interdependency between fields in a model?

schema-object

Most helpful comment

@nomadtechie Is this within a single Schema Object? If so, you can do something like:

{
    "oneOf": [
        {
            "properties": {
                "type": {"enum": [A]},
                ...
            },
            "required": ["otherProperties"]
        },
        {
            "properties": {
                "type": {"enum": [...]},
            }
        }
    ]
}

If you can give a bit more context I can give a more detailed example, but this shows the general idea. oneOf can be made to work as a conditional.

In later drafts of JSON Schema there is an if keyword to make this more intuitive, so hopefully OpenAPI will eventually be able to support that. But everything that if does can also already be done with oneOf once you get a feel for how i works.

All 4 comments

@nomadtechie Is this within a single Schema Object? If so, you can do something like:

{
    "oneOf": [
        {
            "properties": {
                "type": {"enum": [A]},
                ...
            },
            "required": ["otherProperties"]
        },
        {
            "properties": {
                "type": {"enum": [...]},
            }
        }
    ]
}

If you can give a bit more context I can give a more detailed example, but this shows the general idea. oneOf can be made to work as a conditional.

In later drafts of JSON Schema there is an if keyword to make this more intuitive, so hopefully OpenAPI will eventually be able to support that. But everything that if does can also already be done with oneOf once you get a feel for how i works.

Also, for purely JSON Schema questions, there is a Slack channel available for support

thanks @handrews! That's pretty helpful.

@nomadtechie great! If that answers the question if you could close this that would be lovely (or I'll flag @MikeRalphson to close it as I don't have the right permissions :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jblazek picture jblazek  路  3Comments

kolisko picture kolisko  路  4Comments

ricellis picture ricellis  路  3Comments

ePaul picture ePaul  路  5Comments

nelz9999 picture nelz9999  路  4Comments