Wrapping my head around JSON Schema's oneOf
.
I want to produce a JSON version of an XML format and have a JSON schema validate the essential aspects (I know there'll be some differences).
I have an XML Schema concept where you can specify a name or ID for some entity:
<xs:element name="Entity" type="test:EntityType" />
<xs:complexType name="EntityType">
<xs:choice>
<xs:element name="EntityID" />
<xs:element name="EntityName" />
</xs:choice>
</xs:complexType>
In the corresponding JSON schema I'm having trouble with where to put the oneOf
object.
In the JSON schema examples it looks like you should put complete schemas into oneOf
, is that right? How should this look in the general case? Has anyone documented the similarities and differences between XSD and JSON schema for reference?
I didn't try it myself, but I think you need something like this:
{
"allOf" : [
{
"type" : "object",
"properties" : {
"entityName" : {"type" : "string"},
"entityID" : {"type" : "integer"}
}
},
{
"oneOf" : [
{
"required" : ["entityName"]
},
{
"required" : ["entityID"]
}
]
}
]
}
So after under a top-level "allOf"
you first describe the basic structure, then with a "oneOf"
you express the choice.