JSON 使用 json 数据的架构验证无效,因为我不知道如何引用 objects

JSON Schema verification with json data invalid due to me not knowing how to reference objects

如标题所示,我在为我的 json 文件创建有效的 json 模式时遇到问题(或者相反,因为我已经开始创建 json 模式首先然后制作一个 json 应该与模式对应的数据文件)。描述部分是用我的母语写的,但这不是问题。当 object "Paket" 可以选择在主 object 中递归地包含一个或多个 objects "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"
                            }
                        ]
                    }
                ]

            }
        ]
    }   
]

}

当我将其放入验证器时,错误指向 object Paket 中的数组列表 paketi 包含另一个 object 类型 Paket 的部分。它似乎应该在我的脑海中起作用,但显然它没有。谁能帮忙指出我思维上的缺陷?提前致谢。 :D

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

请参考下图,

{
"$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 数据中进行相同的更改。

主要问题在于 "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 架构中的数字。它是一个字符串。我会把剩下的留给你来清理这样的小问题。