为什么这个JSON模式有效?

时间:2015-08-12 13:52:36

标签: json jsonschema

即使文档的最后一行使用" none"以下架构和文档也可以正常验证。作为一个不在枚举中的值。

{
        "$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" ] }
        ]
}

以下说无效。 (" XXX"定义之前的两行是唯一真正的区别。)这里"无"无效。

{
        "$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" ] }
        ]
}

1 个答案:

答案 0 :(得分:1)

第一个说它有效,因为它不验证xxx属性。 但是在第二个上你已经改变了jsonSchema以满足JSON文档,这次它使用jsonSchema验证xxx属性,并且属性无效,因为它包含一个无效节点,类别为#34; none&#34 ;