简体   繁体   English

如何在 JSON 模式中定义嵌套数组,其中数组项的基本类型是特定类型

[英]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?例如,我将如何构造架构以确保abc 、... 都是字符串? 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.

相关问题 在 numpy arrays 中添加项目,其中每个项目都有一个关联的“类型” - Add items in numpy arrays where each item has an associated “type” 如何在Avro架构中定义复杂类型 - How do I define a complex type in an Avro Schema 验证'type'json架构失败 - Failed validating 'type' json schema 如何为存储在嵌套 JSON 文件中的数据库模式中的每个表元数据(列名、类型、格式)创建 Pandas dataframe - How to create a Pandas dataframe for each table meta data (Column Name, Type, Format) stored within a Database Schema in nested JSON file 如何定义一个新的type() - How to define a new type() 如何循环使用 None 类型的嵌套数组 - how to loop through nested array with None type 如何从 django 模型中为模型字段定义 json 类型 - how to define a json type to a model field from django models 如何在 python 中的许多其他 arrays 中找到特定的 var 类型? - How to locate a specific var type inside many others arrays in python? 如何在 POSTGRESQL FlaskSQLAlchemy 中将列定义为任何类型的数组? - How to define a column as an Array of any type in POSTGRESQL FlaskSQLAlchemy? Sklearn:有没有办法为管道定义特定的分数类型? - Sklearn: Is there a way to define a specific score type to pipeline?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM