I asked this same question on the JSON schema google group, and it was answered quickly. User fge asked that I post his response here:
Hello,
The current specification is draft v4, not draft v3. More specifically, the validation specification is here:
https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00
The web site is not up to date, I don't know why... I'll submit a pull request.
With draft v4 you can use this:
{
"type": "array",
"items": {
"oneOf": [
{"first": [ "schema", "here" ] },
{"other": [ "schema": "here" ] }
]
}
}
For instance, this is a schema for an array where items can be either strings or integers (it can be written in a more simple way though):
{
"type": "array",
"items": {
"oneOf": [
{"type": "string"},
{"type": "integer"}
]
}
}
This is the correct answer. My corrected schema now includes:
"transactions" : {
"type" : "array",
"items" : {
"oneOf" : [
{
"type" : "object",
"properties" : {
"type" : {
"type" : "string",
"enum" : ["BUILD", "REASSIGN"]
}
}
},
{
"type" : "object",
"properties" : {
"type" : {
"type" : "string",
"enum" : ["BREAK"]
}
}
}
]
}
}
Answer from deepwinter on Stack OverflowI asked this same question on the JSON schema google group, and it was answered quickly. User fge asked that I post his response here:
Hello,
The current specification is draft v4, not draft v3. More specifically, the validation specification is here:
https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00
The web site is not up to date, I don't know why... I'll submit a pull request.
With draft v4 you can use this:
{
"type": "array",
"items": {
"oneOf": [
{"first": [ "schema", "here" ] },
{"other": [ "schema": "here" ] }
]
}
}
For instance, this is a schema for an array where items can be either strings or integers (it can be written in a more simple way though):
{
"type": "array",
"items": {
"oneOf": [
{"type": "string"},
{"type": "integer"}
]
}
}
This is the correct answer. My corrected schema now includes:
"transactions" : {
"type" : "array",
"items" : {
"oneOf" : [
{
"type" : "object",
"properties" : {
"type" : {
"type" : "string",
"enum" : ["BUILD", "REASSIGN"]
}
}
},
{
"type" : "object",
"properties" : {
"type" : {
"type" : "string",
"enum" : ["BREAK"]
}
}
}
]
}
}
I've been looking into this for quite a while too. But haven't been able to find a working solution. It works fine if you have only one schema eg.
"transactions" : {
"type" : "array",
"items" :
{
"type" : "object",
"properties" : {
"type" : {
"type" : "string",
"enum" : ["BREAK"]
},
}
}
Then you just skip the array brackets, and use an object. However if you want to do what you are doing, there seems to be no solid answer. This is the only thing that I've found so far: http://the-long-dark-tech-time.blogspot.se/2012/12/using-json-schema-with-array-of-mixed.html
Videos
Does the second schema correctly identify that "arr is an array of objects that have an integer named type an integer, and either an object named Steps or an object named Heartrate"?
Yes, somewhat. You do not state which properties must exist, and which properties cannot exist together at the same time, which we can do with the "required" and "oneOf" keywords.
Here's an updated schema, and also fixing an error you made in the "Heartrate" section (you said "steps" instead of "heartrates"):
Copy{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"arr": {
"type": "array",
"items": {
"type": "object",
"required": [ "type" ],
"oneOf": [
{ "required": ["Steps"] },
{ "required": ["HeartRate"] }
},
"properties": {
"type": {
"type": "integer"
},
"Steps": {
"type": "object",
"required": ["steps"],
"properties": {
"steps": {
"type": "integer"
}
}
},
"HeartRate": {
"type": "object",
"required": ["heartrates"],
"properties": {
"heartrates": {
"type": "integer"
}
}
}
}
}
}
}
}
Well, it passes because technically it is valid, you can have any combination of objects in an array. This schema explicitly states that there can be a heartrate object. You say you're familiar with arrays of a single object type, but what you should know is an array doesn't care what types of objects are in it, which is why your introduced schema is in fact valid. Whether or not it fits the function you're trying to achieve is another question all together.
It would imply that you would need either one very complex loop to traverse the array when you need to pull an object, assuming there will be so many that you can't adequately index them, or you would need some sort of index with address to the objects in the array, which might be cumbersome to write but possible. So the question is, what are you intending to do with these objects? Seems obvious from the properties you're using, seems like a fitness monitor of some sort, but then what your intention to do with the data isn't apparent so it's difficult to say what you could do even if this schema is valid, which keep in mind that it is indeed a valid structure for an array.
Although if you do have two types of objects, one could ask why not simply have an array for each type which will have supporting data pulling functions specific to their contents. But again it asks what you're intending to do.
Hope this helps your thought process.
UPDATE...
Not knowing exactly what you are intending to do, here is what I suggest you use as your data-structure format.
Copylet someArray = [
{
Steps: {
steps: 3500
},
HeartRate: {
heartrates: 4000
}
},
{
Steps: {
steps: 3500
},
HeartRate: {
heartrates: 4000
}
}
]
You examples have some redundancy in them it seems, where they are explicitly stating that something is an object and something is a property of that object. JavaScript already knows all that so no need to add more complexity to the functions you create to work with these that will then have to dig through all those layers. I suggest that you create one object and each has a property of the type you've declared that is an object and that object can if you want be expanded or condensed as you desire. This will mitigate a lot of complexity to your structure. And so you'll see that you have an Array of Objects and those objects contain properties representing the data you're intending to gather.
But this assumes that the data you're gathering is from multiple sources. Again would need to know a bit more about what you're trying to accomplish.