3

I'm using tv4.js to validate some json against a schema (which has nested oneOf properties) but it returns errors when I am using valid data. Here is the result object I get back from the tv4.js validateMultiple method:

{"valid":false,"errors":[{"code":11,"message":"Data does not match any schemas from \"oneOf\"","schemaKey":null,"dataPath":"/shape","subErrors":[{"code":302,"message":"Missing required property: boxname","schemaKey":null,"dataPath":"/shape","subErrors":null},{"code":1,"message":"No enum match for: \"circle\"","schemaKey":null,"dataPath":"/shape/thetype","subErrors":null},{"code":12,"message":"Data is valid against more than one schema from \"oneOf\": indices 0 and 1","schemaKey":null,"dataPath":"/shape","subErrors":null}]}],"missing":[]}

Here is my test schema:

{
    "type": "object",
    "properties": {
        "shape": {
            "oneOf": [
                { "$ref":"#/definitions/squareSchema" },
                { "$ref":"#/definitions/circleSchema" }
            ]
        }
    },
    "definitions": {
        "squareSchema": {
            "type": "object",
            "properties": {
                "thetype": {
                    "type": "string",
                    "enum": ["square"]
                },
                "colour":{},
                "shade":{},
                "boxname": {
                    "type":"string"
                }
            },
            "oneOf":[
                { "$ref":"#/definitions/colourSchema" },
                { "$ref":"#/definitions/shadeSchema" }
            ],
            "required": ["thetype", "boxname"],
            "additionalProperties":false
        },
        "circleSchema": {
            "type": "object",
            "properties": {
                "thetype": {
                    "type": "string",
                    "enum":["circle"]
                },
                "colour":{},
                "shade":{}
            },
            "oneOf":[
                { "$ref":"#/definitions/colourSchema" },
                { "$ref":"#/definitions/shadeSchema" }
            ],
            "additionalProperties":false
        },
        "colourSchema":{
            "type":"object",
            "properties":{
                "colour":{
                    "type":"string"
                },
                "shade":{
                    "type":"null"
                }
            }
        },
        "shadeSchema":{
            "type":"object",
            "properties":{
                "shade":{
                    "type":"string"
                },
                "colour":{
                    "type":"null"
                }
            }
        }
    }
}

And here is the data I would expect to validate:

{
    "shape": {
        "thetype": "circle",
        "shade":"red"
    }
}

I seem to only encounter this issue when using nested "oneOf". Is this an issue with my schema? Or a bug with tv4.js? Are there any alternative validators which will do this validation within a web browser?

Any help would be appreciated.

1 Answer 1

4

It works for me (using the "Try out tv4" demo).

Normally, I'd suggest you file a issue on the GitHub repo if you think you've found an error. However, the fact that the error output includes schemaKey makes me think you're using a fairly old version.

Are you using an up-to-date version of tv4?

Sign up to request clarification or add additional context in comments.

3 Comments

I was using the latest version from github. But you helped me figure out whats happening. It works when you use the "validate" or "validateResult" methods but it doesn't work when using the "validateMultiple" method. Thanks for the help!!! I'll mark this correct as soon as I can :)
Strange, given that the "try out tv4" demo uses validateMultiple(). If it's failing with validateMultiple() but passing with validate(), that's a serious bug which you should raise on the GitHub repo. (As the author of tv4, I would appreciate it :p)
Ok will do. apart from the issue in this stack question tv4 has been working great. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.