Skip to content

Use case: list of abtract elements #644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
graham2071 opened this issue Jul 25, 2018 · 3 comments
Closed

Use case: list of abtract elements #644

graham2071 opened this issue Jul 25, 2018 · 3 comments

Comments

@graham2071
Copy link

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.

@handrews
Copy link
Contributor

handrews commented Aug 2, 2018

@graham2071 work on error reporting and output is being tracked in #643, so I am closing this in favor of that one, which you should follow. The case you are mentioning (verbose errors because of anyOf or similar keywords) is one of the primary cases they are discussing there, so there is no need to re-state it. Note that #643 is the summary of older discussions, so don't worry if you don't see your use case listed out directly. It's one of the major motivations.

@handrews handrews closed this as completed Aug 2, 2018
@handrews
Copy link
Contributor

handrews commented Aug 2, 2018

Note that any specific issues with Ajv should be filed at that repository- this repository is only for the specification, not implementations.

@graham2071
Copy link
Author

@handrews thank you very much for your answer. I'll stick to the anyOf and oneOf(without if) and filter the validation errors for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants