Question

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.

Était-ce utile?

La solution

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?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top