Description
Hello,
I am new to json-schema, and try to create a schema which expresses the following constraint: "a list of various elements".
As an example, let's take a json containing a list of "Animals". An animal can be a "Dog", a "Wolf" or a "Turtle". They share some common properties ("name"), and have specific ones.
Example:
{
"listOfAnimals":
[
{
"type": "dog",
"name": "Doggy",
"owner": "bob"
},
{
"type": "wolf",
"name": "wolfy",
"pack": 0
}
]
}
The problem of my first schema implementation is that Ajv validator returns a lot of errors (for what I consider a single error in the data), because it explores all branches.
Here is an excerpt from the schema (full is here: https://gitlab.com/graham2071/json-schema-example/blob/master/test/case1/animals.schema.json):
"animal": {
"$id": "#Animal",
"type": "object",
"anyOf": [
{
"$ref": "#Dog"
},
{
"$ref": "#Wolf"
},
{
"$ref": "#Turtle"
}
]
},
So I've come to try different implementations, and am wondering if I missed something.
My conclusion is to use "if" to force the selection of the correct branch in Ajv.
You can see all my tests here: https://gitlab.com/graham2071/json-schema-example
Running npm test will print out the number of errors found by Ajv.
Thanks for your help,
g.