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?
@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 :-)
Most helpful comment
@nomadtechie Is this within a single Schema Object? If so, you can do something like:
If you can give a bit more context I can give a more detailed example, but this shows the general idea.
oneOfcan be made to work as a conditional.In later drafts of JSON Schema there is an
ifkeyword to make this more intuitive, so hopefully OpenAPI will eventually be able to support that. But everything thatifdoes can also already be done withoneOfonce you get a feel for how i works.