由于我不知道如何引用对象,json Schema验证与json数据无效

时间:2017-03-24 11:59:11

标签: json validation recursion jsonschema

正如标题所示,我无法为我的json文件创建有效的json模式(或者反过来,因为我已经开始先创建json模式然后创建一个应该与模式对应的json数据文件)。描述部分是用我的母语编写的,但这不是问题。当对象“Paket”可以选择在主要对象内部递归地包含一个或多个对象“Paket”时,它似乎会产生问题。以下是我的json架构:

{
"$schema": "http://json-schema.org/schema#",
"title": "Skladiste",
"description": "Elektronski magacin visestrukog tipa za cuvanje podataka, to jest paketa sa svojim entitetima koji sadrze podatke.",
"type": "object",
"properties": {
    "id": {
        "description": "Jedinstveni identifikacioni broj skladista.",
        "type": "integer"
    },
    "name": {
        "description": "Ime, to jest naziv, skladista.",
        "type": "string"
    },
    "paketi": {
        "description": "Lista paketa.",
        "type": "array",
        "items": {
            "title": "Paket",
            "description": "Virtuelna fascikla koja moze da sadrzi entitete, ali i druge pakete.",
            "type": "object",
            "properties": {
                "id": {
                    "description": "Jedinstveni identifikacioni broj paketa.",
                    "type": "integer"
                },
                "name": {
                    "description": "Ime, to jest naziv paketa.",
                    "type": "string"
                },
                "paketi": {
                    "description": "Niz paketa unutar paketa.",
                    "type": "array",
                    "items": {
                        "type": "Paket"
                    }
                },
                "entiteti": {
                    "description": "Niz entiteta.",
                    "type": "array",
                    "items": {
                        "title": "Entitet",
                        "description": "Objekat u sistemu u kome se skladiste atributi, to jest sadrzioci podataka i informacija.",
                        "type": "object",
                        "properties": {
                            "id": {
                                "description": "Jedinstven identifikacioni broj entiteta.",
                                "type": "number"
                            },
                            "name": {
                                "description": "Ime, to jest naziv entiteta.",
                                "type": "integer"
                            },
                            "atributi": {
                                "description": "Niz atributa.",
                                "type": "array",
                                "items": {
                                    "title": "Atribut",
                                    "description": "Objekat koji sadrzi, to jest skladisti informacije i podatke.",
                                    "type": "object",
                                    "properties": {
                                        "id": {
                                            "type": "integer"
                                        },
                                        "name": {
                                            "type": "string"
                                        },
                                        "type": {
                                            "enum": [
                                                "String",
                                                "Int",
                                                "DateTime"
                                            ]
                                        }
                                    },
                                    "required": ["id", "name", "type"]
                                }
                            },
                            "relacije": {
                                "description": "Niz relacija.",
                                "type": "array",
                                "items": {
                                    "title": "Relacija",
                                    "description": "Veza koja ukazuje na bliskost dva entiteta.",
                                    "type": "object",
                                    "properties": {
                                        "id": {
                                            "type": "integer"
                                        },
                                        "name": {
                                            "type": "string"
                                        },
                                        "ReferenciranEntitet": {
                                            "type": "string"
                                        }
                                    },
                                    "required": ["id", "name", "ReferenciranEntitet"]
                                }
                            }
                        },
                        "required": ["id", "name"]
                    }
                }
            },
            "required": ["id", "name"]
        }   
    }
},
"required": ["id", "name"]

}

这是我的json数据文件

{
"id":"1", 
"name": "KpopGrupe",
"paketi": [
    {
        "id": "1",
        "name": "Seventeen",
        "paketi": [
            {
                "id": "1",
                "name": "HipHopTim",
                "entiteti": [
                    {
                        "id": "1",
                        "name": "ChoiSungCheol",
                        "atributi": [
                            {
                                "id": "1",
                                "name": "IzvodjackoIme",
                                "type": "String"
                            },
                            {
                                "id": "2",
                                "name": "Nacionalnost",
                                "type": "String"
                            },
                            {
                                "id": "3",
                                "name": "DatumRodjenja",
                                "type": "DateTime"
                            }
                        ],
                        "relacije": [
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "JeonWonwoo"
                            },
                            {
                                "id": "2",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "KimMingyu"
                            },
                            {
                                "id": "3",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "HansolVernonChoi"
                            }
                        ]
                    },
                    {
                        "id": "2",
                        "name": "JeonWonwoo",
                        "atributi": [
                            {
                                "id": "1",
                                "name": "IzvodjackoIme",
                                "type": "String"
                            },
                            {
                                "id": "2",
                                "name": "Nacionalnost",
                                "type": "String"
                            },
                            {
                                "id": "3",
                                "name": "DatumRodjenja",
                                "type": "DateTime"
                            }
                        ],
                        "relacije": [
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "ChoiSungCheol"
                            },
                            {
                                "id": "2",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "KimMingyu"
                            },
                            {
                                "id": "3",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "HansolVernonChoi"
                            }
                        ]
                    },
                    {
                        "id": "3",
                        "name": "KimMingyu",
                        "atributi": [
                            {
                                "id": "1",
                                "name": "IzvodjackoIme",
                                "type": "String"
                            },
                            {
                                "id": "2",
                                "name": "Nacionalnost",
                                "type": "String"
                            },
                            {
                                "id": "3",
                                "name": "DatumRodjenja",
                                "type": "DateTime"
                            }
                        ],
                        "relacije": [
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "ChoiSungCheol"
                            },
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "JeonWonwoo"
                            },
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "HansolVernonChoi"
                            }
                        ]
                    },
                    {
                        "id": "4",
                        "name": "HansolVernonChoi",
                        "atributi": [
                            {
                                "id": "1",
                                "name": "IzvodjackoIme",
                                "type": "String"
                            },
                            {
                                "id": "2",
                                "name": "Nacionalnost",
                                "type": "String"
                            },
                            {
                                "id": "3",
                                "name": "DatumRodjenja",
                                "type": "DateTime"
                            }
                        ],
                        "relacije": [
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "ChoiSungCheol"
                            },
                            {
                                "id": "2",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "JeonWonwoo"
                            },
                            {
                                "id": "3",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "KimMingyu"
                            }
                        ]
                    }
                ]
            },
            {
                "id": "2",
                "name": "VokalniTim",
                "entiteti": [
                    {
                        "id": "1",
                        "name": "YoonJungHan",
                        "atributi": [
                            {
                                "id": "1",
                                "name": "IzvodjackoIme",
                                "type": "String"
                            },
                            {
                                "id": "2",
                                "name": "Nacionalnost",
                                "type": "String"
                            },
                            {
                                "id": "3",
                                "name": "DatumRodjenja",
                                "type": "DateTime"
                            }
                        ],
                        "relacije": [
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "HongJisoo"
                            },
                            {
                                "id": "2",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "LeeJihun"
                            },
                            {
                                "id": "3",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "LeeSeokmin"
                            },
                            {
                                "id": "4",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "BooSeungKwan"
                            }
                        ]
                    },
                    {
                        "id": "2",
                        "name": "HongJisoo",
                        "atributi": [
                            {
                                "id": "1",
                                "name": "IzvodjackoIme",
                                "type": "String"
                            },
                            {
                                "id": "2",
                                "name": "Nacionalnost",
                                "type": "String"
                            },
                            {
                                "id": "3",
                                "name": "DatumRodjenja",
                                "type": "DateTime"
                            }
                        ],
                        "relacije": [
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "YoonJungHan"
                            },
                            {
                                "id": "2",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "LeeJihun"
                            },
                            {
                                "id": "3",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "LeeSeokmin"
                            },
                            {
                                "id": "4",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "BooSeungKwan"
                            }
                        ]
                    },
                    {
                        "id": "3",
                        "name": "LeeJihun",
                        "atributi": [
                            {
                                "id": "1",
                                "name": "IzvodjackoIme",
                                "type": "String"
                            },
                            {
                                "id": "2",
                                "name": "Nacionalnost",
                                "type": "String"
                            },
                            {
                                "id": "3",
                                "name": "DatumRodjenja",
                                "type": "DateTime"
                            }
                        ],
                        "relacije": [
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "YoonJungHan"
                            },
                            {
                                "id": "2",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "HongJisoo"
                            },
                            {
                                "id": "3",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "LeeSeokmin"
                            },
                            {
                                "id": "4",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "BooSeungKwan"
                            }
                        ]
                    },
                    {
                        "id": "4",
                        "name": "LeeSeokmin",
                        "atributi": [
                            {
                                "id": "1",
                                "name": "IzvodjackoIme",
                                "type": "String"
                            },
                            {
                                "id": "2",
                                "name": "Nacionalnost",
                                "type": "String"
                            },
                            {
                                "id": "3",
                                "name": "DatumRodjenja",
                                "type": "DateTime"
                            }
                        ],
                        "relacije": [
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "YoonJungHan"
                            },
                            {
                                "id": "2",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "HongJisoo"
                            },
                            {
                                "id": "3",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "LeeJihun"
                            },
                            {
                                "id": "4",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "BooSeungKwan"
                            }
                        ]
                    },
                    {
                        "id": "5",
                        "name": "BooSeungKwan",
                        "atributi": [
                            {
                                "id": "1",
                                "name": "IzvodjackoIme",
                                "type": "String"
                            },
                            {
                                "id": "2",
                                "name": "Nacionalnost",
                                "type": "String"
                            },
                            {
                                "id": "3",
                                "name": "DatumRodjenja",
                                "type": "DateTime"
                            }
                        ],
                        "relacije": [
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "YoonJungHan"
                            },
                            {
                                "id": "2",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "HongJisoo"
                            },
                            {
                                "id": "3",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "LeeJihun"
                            },
                            {
                                "id": "4",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "LeeSeokmin"
                            }
                        ]
                    }
                ]
            },
            {
                "id": "3",
                "name": "PlesniTim",
                "entiteti": [
                    {
                        "id": "1",
                        "name": "WenJunhui",
                        "atributi": [
                            {
                                "id": "1",
                                "name": "IzvodjackoIme",
                                "type": "String"
                            },
                            {
                                "id": "2",
                                "name": "Nacionalnost",
                                "type": "String"
                            },
                            {
                                "id": "3",
                                "name": "DatumRodjenja",
                                "type": "DateTime"
                            }
                        ],
                        "relacije": [
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "KwonSoonyoung"
                            },
                            {
                                "id": "2",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "XuMinghao"
                            },
                            {
                                "id": "3",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "LeeChan"
                            }
                        ]
                    },
                    {
                        "id": "2",
                        "name": "KwonSoonyoung",
                        "atributi": [
                            {
                                "id": "1",
                                "name": "IzvodjackoIme",
                                "type": "String"
                            },
                            {
                                "id": "2",
                                "name": "Nacionalnost",
                                "type": "String"
                            },
                            {
                                "id": "3",
                                "name": "DatumRodjenja",
                                "type": "DateTime"
                            }
                        ],
                        "relacije": [
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "WenJunhui"
                            },
                            {
                                "id": "2",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "XuMinghao"
                            },
                            {
                                "id": "3",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "LeeChan"
                            }
                        ]
                    },
                    {
                        "id": "3",
                        "name": "XuMinghao",
                        "atributi": [
                            {
                                "id": "1",
                                "name": "IzvodjackoIme",
                                "type": "String"
                            },
                            {
                                "id": "2",
                                "name": "Nacionalnost",
                                "type": "String"
                            },
                            {
                                "id": "3",
                                "name": "DatumRodjenja",
                                "type": "DateTime"
                            }
                        ],
                        "relacije": [
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "WenJunhui"
                            },
                            {
                                "id": "2",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "KwonSoonyoung"
                            },
                            {
                                "id": "3",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "LeeChan"
                            }
                        ]
                    },
                    {
                        "id": "4",
                        "name": "LeeChan",
                        "atributi": [
                            {
                                "id": "1",
                                "name": "IzvodjackoIme",
                                "type": "String"
                            },
                            {
                                "id": "2",
                                "name": "Nacionalnost",
                                "type": "String"
                            },
                            {
                                "id": "3",
                                "name": "DatumRodjenja",
                                "type": "DateTime"
                            }
                        ],
                        "relacije": [
                            {
                                "id": "1",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "WenJunhui"
                            },
                            {
                                "id": "2",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "KwonSoonyoung"
                            },
                            {
                                "id": "3",
                                "name": "ClanIstePodgrupe",
                                "referenciranEntitet": "XuMinghao"
                            }
                        ]
                    }
                ]

            }
        ]
    }   
]

}

当我把它放入验证器时,错误指向对象Paket中的数组列表paketi包含另一个对象类型Paket的部分。它似乎应该在我脑海中起作用,但显然它不会。任何人都可以帮我指出我的思维缺陷吗?提前致谢。 :d

2 个答案:

答案 0 :(得分:0)

主要问题是"type": "Paket"。那是无效的。据推测,您尝试使用"title": "Paket"引用子模式。你想要这样的东西。

{
  "$schema": "http://json-schema.org/schema#",
  "title": "Skladiste",
  "description": "Elektronski magacin visestrukog tipa za cuvanje podataka, to jest paketa sa svojim entitetima koji sadrze podatke.",
  "type": "object",
  "definitions": {
    "paket": {
      "title": "Paket",
      ... snip ...
      "properties": {
        ... snip ...
        "paketi": {
          "description": "Niz paketa unutar paketa.",
          "type": "array",
          "items": { "$ref": "#/definitions/paket" }  <-- self reference
        },
        ... snip ...
      }
      ... snip ...
    }
  },
  "properties": {
    "id": {
      "description": "Jedinstveni identifikacioni broj skladista.",
      "type": "integer"
    },
    "name": {
      "description": "Ime, to jest naziv, skladista.",
      "type": "string"
    },
    "paketi": {
      "description": "Lista paketa.",
      "type": "array",
      "items": { "$ref": "#/definitions/paket" }  <-- definition reference
    }
  },
  "required": ["id", "name"]
}

通过该更改,您可以获得有效的架构,但仍有一些架构与架构不匹配。例如,您的“id”属性被定义为“integer”或“number”类型,但您的数据都将“id”表示为字符串"1""1"不是JSON模式中的整数或数字。这是一个字符串。我会把剩下的部分留给你来清理这样的小问题。

答案 1 :(得分:-1)

您的架构有一些错误,类型应该是“数字”而不是整数。

请参考以下架构,

{
"$schema": "http://json-schema.org/schema#",
"title": "Skladiste",
"description": "Elektronski magacin visestrukog tipa za cuvanje podataka, to jest paketa sa svojim entitetima koji sadrze podatke.",
"type": "object",
"properties": {
    "id": {
        "description": "Jedinstveni identifikacioni broj skladista.",
        "type": "number"
    },
    "name": {
        "description": "Ime, to jest naziv, skladista.",
        "type": "string"
    },
    "paketi": {
        "description": "Lista paketa.",
        "type": "array",
        "items": {
            "title": "Paket",
            "description": "Virtuelna fascikla koja moze da sadrzi entitete, ali i druge pakete.",
            "type": "object",
            "properties": {
                "id": {
                    "description": "Jedinstveni identifikacioni broj paketa.",
                    "type": "number"
                },
                "name": {
                    "description": "Ime, to jest naziv paketa.",
                    "type": "string"
                },
                "paketi": {
                    "description": "Niz paketa unutar paketa.",
                    "type": "array",
                    "items": {
                        "type": "object"
                    }
                },
                "entiteti": {
                    "description": "Niz entiteta.",
                    "type": "array",
                    "items": {
                        "title": "Entitet",
                        "description": "Objekat u sistemu u kome se skladiste atributi, to jest sadrzioci podataka i informacija.",
                        "type": "object",
                        "properties": {
                            "id": {
                                "description": "Jedinstven identifikacioni broj entiteta.",
                                "type": "number"
                            },
                            "name": {
                                "description": "Ime, to jest naziv entiteta.",
                                "type": "integer"
                            },
                            "atributi": {
                                "description": "Niz atributa.",
                                "type": "array",
                                "items": {
                                    "title": "Atribut",
                                    "description": "Objekat koji sadrzi, to jest skladisti informacije i podatke.",
                                    "type": "object",
                                    "properties": {
                                        "id": {
                                            "type": "integer"
                                        },
                                        "name": {
                                            "type": "string"
                                        },
                                        "type": {
                                            "enum": [
                                                "String",
                                                "Int",
                                                "DateTime"
                                            ]
                                        }
                                    },
                                    "required": ["id", "name", "type"]
                                }
                            },
                            "relacije": {
                                "description": "Niz relacija.",
                                "type": "array",
                                "items": {
                                    "title": "Relacija",
                                    "description": "Veza koja ukazuje na bliskost dva entiteta.",
                                    "type": "object",
                                    "properties": {
                                        "id": {
                                            "type": "integer"
                                        },
                                        "name": {
                                            "type": "string"
                                        },
                                        "ReferenciranEntitet": {
                                            "type": "string"
                                        }
                                    },
                                    "required": ["id", "name", "ReferenciranEntitet"]
                                }
                            }
                        },
                        "required": ["id", "name"]
                    }
                }
            },
            "required": ["id", "name"]
        }   
    }
},
"required": ["id", "name"]
}

并在json数据中也改变它。