[英]Add items in numpy arrays where each item has an associated “type”
[英]How to define nested arrays in JSON schema where the base type of array items is specific type
我正在使用python 的 jsonschema來驗證 YAML 文件。 我無法弄清楚如何做的一件事是允許嵌套數組,但強制所有數組項的基本類型都是字符串。 我需要此功能來處理 YAML 錨點。 例如,我將如何構造架構以確保a
、 b
、 c
、... 都是字符串? 作為參考,我不知道這個數組是如何嵌套的,所以我不認為使用簡單的anyOf
會起作用。
["a", ["b", ["c"]], ...]
我參考了關於遞歸的文檔,這似乎是我需要的,我只是不太了解它,無法在這種情況下實現它。
理想情況下,我希望數組的所有基本項都是唯一的,但這可能要求太多,因為我可以在展平數組后輕松完成在 python 中的檢查。
對於單級字符串數組:
{
"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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.