例如,文件系統的模式,目錄包含文件列表。該模式由文件規範,下一個子類型「圖像」和另一個「文本」組成。如何告訴JSON模式驗證器從屬性值中選擇模式?
底部有主目錄模式。目錄具有屬性內容,該內容是應該是文件的子類型的項目的數組。
基本上我正在尋找的是一種方法來告訴驗證器從被驗證的json對象的屬性中查找「$ ref」的值。
例JSON:
{
"name":"A directory",
"content":[
{
"fileType":"http://x.y.z/fs-schema.json#definitions/image",
"name":"an-image.png",
"width":1024,
"height":800
}
{
"fileType":"http://x.y.z/fs-schema.json#definitions/text",
"name":"readme.txt",
"lineCount":101
}
{
"fileType":"http://x.y.z/extended-fs-schema-video.json",
"name":"demo.mp4",
"hd":true
}
]
}
「僞」模式注意「形象」和「文本」的定義都包含在相同的架構,但他們可能會在其他地方定義
{
"id": "http://x.y.z/fs-schema.json",
"definitions": {
"file": {
"type": "object",
"properties": {
"name": { "type": "string" },
"fileType": {
"type": "string",
"format": "uri"
}
}
},
"image": {
"allOf": [
{ "$ref": "#definitions/file" },
{
"properties": {
"width": { "type": "integer" },
"height": { "type": "integer"}
}
}
]
},
"text": {
"allOf": [
{ "$ref": "#definitions/file" },
{ "properties": { "lineCount": { "type": "integer"}}}
]
}
},
"type": "object",
"properties": {
"name": { "type": "string"},
"content": {
"type": "array",
"items": {
"allOf": [
{ "$ref": "#definitions/file" },
{ *"$refFromProperty"*: "fileType" } // the magic thing
]
}
}
}
}
需要澄清。你想強制目錄數據被定義時(而不是在設計時)定義的目錄中的所有文件項都滿足某些屬性,對嗎? – jruizaranguren
我希望它從屬性fileType – redben