[英]How to define nested arrays in JSON schema where the base type of array items is specific type
I am using python's jsonschema to validate YAML files.我正在使用python 的 jsonschema来验证 YAML 文件。 One of the things I can't figure out how to do is allow nested arrays but enforce the base type of all array items are strings.
我无法弄清楚如何做的一件事是允许嵌套数组,但强制所有数组项的基本类型都是字符串。 I need this capability to handle YAML anchors.
我需要此功能来处理 YAML 锚点。 For example, how would I construct the schema to ensure that
a
, b
, c
,... are all strings?例如,我将如何构造架构以确保
a
、 b
、 c
、... 都是字符串? For reference, I don't know how nested this array will be so I don't think using simple anyOf
would work.作为参考,我不知道这个数组是如何嵌套的,所以我不认为使用简单的
anyOf
会起作用。
["a", ["b", ["c"]], ...]
I referenced the docs about recursion and this seems like what I need, I just don't understand it well enough to implement it for this case.我参考了关于递归的文档,这似乎是我需要的,我只是不太了解它,无法在这种情况下实现它。
Ideally, I would like all base items of the array to be unique, but this might be asking too much as I can easily accomplish checking that in python after flattening the array.理想情况下,我希望数组的所有基本项都是唯一的,但这可能要求太多,因为我可以在展平数组后轻松完成在 python 中的检查。
For a single level array of strings:对于单级字符串数组:
{
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true
}
You can make the items
schema recursive by allowing it to be an array of arrays, or strings:您可以通过允许它是一个数组或字符串数组来使
items
架构递归:
{
"$defs": {
"nested_array": {
"type": "array",
"items": {
"anyOf": [
{ "type": "string" },
{ "$ref": "#/$defs/nested_array" }
]
},
"uniqueItems": true
}
},
"$ref": "#/$defs/nested_array"
}
reference: https://json-schema.org/understanding-json-schema/reference/array.html参考: https : //json-schema.org/understanding-json-schema/reference/array.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.