【问题标题】:Json Schema - how to express a field mixed Types (string and object)?Json Schema - 如何表示字段混合类型(字符串和对象)?
【发布时间】:2017-01-09 08:57:38
【问题描述】:

我的数据中有一个字段可以多次输入:

它可以是 type=string,它具有架构:

{"mixed_field" : {"type":"string"} }

其他时候它可能是 type=object,架构看起来像:

{"mixed_field" : {
  "properties": {
    "access_token": {
      "type": "string"
    },
    "created_at": {
      "type": "integer"
    }
  },
  "type": "object"
  }
}

如何表达“mixed_field”可以是类型字符串或类型对象?我应该如下使用“oneOf”关键字吗?

{
  "mixed_field": {
    "oneOf": [
      {
        "type": "string"
      },
      {
        "properties": {
          "access_token": {
            "type": "string"
          },
          "created_at": {
            "type": "integer"
          }
        },
        "type": "object"
      }
    ]
  }
}

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    你可以使用oneOf/anyOf或者你可以使用"type": ["string", "object"],如果是字符串“properties”关键字将被忽略。

    【讨论】:

    • 不知道如果类型是原始的,“属性”会被忽略!感谢您的提醒。与使用 oneOf 关键字和枚举类型字段相比,是否有特定的偏好?
    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    相关资源
    最近更新 更多