I have data that comes back from GraphQL, I'd like to verify that data with JSON schema before manipulating it.
There might be a better way of doing this in graphQL than I currently am, but the data that comes back can be of two types with the same properties:
e.g. a simplified version of the data
obj: {
audio: {
artists: []
},
video: {}
}
or
obj: {
audio: {},
video: {
artists: []
}
}
So validity would be:
- an object with both a
audioandvideoproperty - an object with
audioas an object with a propertyartistsand an empty propertyvideoobject - an object with
videoas an object with a propertyartistsand an empty propertyaudioobject - neither
audioorvideoshould be empty together - neither
audioorvideoshould have properties together
I've built a simplified schema that looks like this:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "file://schemas/simple.schema.json",
"title": "simplified data",
"description": "simple",
"type": "object",
"properties": {
"audio": {
"type": "object"
},
"video": {
"type": "object"
}
},
"oneOf": [
{
"audio": {
"type": "object",
"properties": {
"artists": {
"type": "array"
}
}
},
"video": {
"type": "object",
"properties": {}
}
},
{
"audio": {
"type": "object",
"properties": {}
},
"video": {
"type": "object",
"properties": {
"artists": {
"type": "array"
}
}
}
}
]
}
but AJV doesn't seem to validate the data correctly when run against:
{
"audio": {
"artists": []
},
"video": {}
}
What might have I got wrong with my schema?