Json Schema - 如何表达字段混合类型(字符串和对象)?

时间:2017-01-09 08:57:39

标签: json jsonschema json-schema-validator

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

它可以是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"
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:1)

您可以使用oneOf / anyOf,也可以使用"type": ["string", "object"],如果是字符串,则会忽略“properties”关键字。