No One in Particular
No One in Particular

Reputation: 2894

Why is this JSON schema valid?

The following schema and document validate just fine even though the last line of the document uses "none" as a value which is not in the enum.

{
        "$schema"     : "http://json-schema.org/draft-04/schema#",
        "title"       : "Test schema",

        "definitions" : {
                "xxx_type" : {
                        "enum" : [ "X1", "X2", "X3" ]
                },

                "xxx_info" : {
                        "type" : "object",
                        "properties" : {
                                "date" : { "type" : "string" },
                                "category" : {
                                        "type" : "array",
                                        "items" : { "$ref" : "#/definitions/xxx_type" }
                                }
                        },
                        "required" : [ "date", "category" ]
                }
        },

        "XXX" : {
                "type" : "array",
                "items" : {
                        "$ref" : "#/definitions/xxx_info"
                }
        }
}

{
        "XXX" : [ { "date" : "2015/01/01", "category" : [ "X1" ] },
                  { "date" : "2015/02/01", "category" : [ "X2" ] },
                  { "date" : "2015/03/01", "category" : [ "X3" ] }
                  { "date" : "2015/04/01", "category" : [ "none" ] }
        ]
}

The following says invalid. (The two lines before the "XXX" definition are the only real difference.) Here the "none" is invalid.

{
        "$schema"     : "http://json-schema.org/draft-04/schema#",
        "title"       : "Test schema",

        "definitions" : {
                "xxx_type" : {
                        "enum" : [ "X1", "X2", "X3" ]
                },

                "xxx_info" : {
                        "type" : "object",
                        "properties" : {
                                "date" : { "type" : "string" },
                                "category" : {
                                        "type" : "array",
                                        "items" : { "$ref" : "#/definitions/xxx_type" }
                                }
                        },
                        "required" : [ "date", "category" ]
                }
        },

        "type" : "object",
        "properties" : {
                "XXX" : {
                        "type" : "array",
                        "items" : {
                                "$ref" : "#/definitions/xxx_info"
                        }
                }
        }
}

{
        "XXX" : [ { "date" : "2015/01/01", "category" : [ "X1" ] },
                  { "date" : "2015/02/01", "category" : [ "X2" ] },
                  { "date" : "2015/03/01", "category" : [ "X3" ] }
                  { "date" : "2015/04/01", "category" : [ "none" ] }
        ]
}

Upvotes: 0

Views: 100

Answers (1)

borja gómez
borja gómez

Reputation: 1051

The first one says it is valid because it does not validate the xxx property. But on the second one you have changed the jsonSchema to meet the JSON document and this time it is validating the xxx property using the jsonSchema and the property is invalid because it contains an invalid node the one with the category "none"

Upvotes: 1

Related Questions