我正在使用python 的 jsonschema來驗證 YAML 檔案。我無法弄清楚如何做的一件事是允許嵌套陣列,但強制所有陣列項的基本型別都是字串。我需要此功能來處理 YAML 錨點。例如,我將如何構建架構以確保a, b, c,... 都是字串?作為參考,我不知道這個陣列是如何嵌套的,所以我認為使用 simpleanyOf行不通。
["a", ["b", ["c"]], ...]
我參考了關于遞回的檔案,這似乎是我需要的,我只是不太了解它,無法在這種情況下實作它。
理想情況下,我希望陣列的所有基本項都是唯一的,但這可能要求太多,因為我可以在展平陣列后輕松完成在 python 中的檢查。
uj5u.com熱心網友回復:
對于單級字串陣列:
{
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true
}
您可以items通過允許它是一個陣列或字串數??組來使模式遞回:
{
"$defs": {
"nested_array": {
"type": "array",
"items": {
"anyOf": [
{ "type": "string" },
{ "$ref": "#/$defs/nested_array" }
]
},
"uniqueItems": true
}
},
"$ref": "#/$defs/nested_array"
}
參考:https : //json-schema.org/understanding-json-schema/reference/array.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/338824.html
