[英]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.