2

如何为下一个对象设置 json 模式验证器?他们都有场account_id,但options都是依赖type

如果twitter它有列表tokens,则每个都有四个字段。如果typeinstagram,则每个标记在 中只有一个字段 tokens。并且 forfacebook type字段options必须为空。

{
  "type": "twitter",
  "account_id": "xyz",
  "options": {
    "tokens": [{
      "access_token": "long string",
      "access_token_secret": "long string",
      "consumer_key": "long string",
      "consumer_secret": "long string"
    }]
  }
}

{
  "type": "instagram",
  "account_id": "xyz",
  "options": {
    "tokens": [{
      "access_token": "long string"
    }]
  }
}

{
  "type": "facebook",
  "account_id": "xyz",
  "options": {}
}

是否可以制作满足这些要求的 json 模式?type当每个定义都包含在关键字中时,即使是最简单的模式oneOf也不起作用。

{
  "type": "object",
  "additionalProperties": false,
  "oneOf": [{
      "properties": {
        "account_id": {
          "type": "string",
        },
        "type": {
          "const": "facebook"
        },
        "options": {
          "type": "object",
          "additionalProperties": false
        }
      },
      "required": [
        "account_id",
        "type",
        "options"
      ]
    },
    {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "account_id": {
          "type": "string",
        },
        "type": {
          "const": "twitter"
        },
        "options": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "access_token": {
                "type": "string"
              },
              "access_token_secret": {
                "type": "string"
              },
              "consumer_key": {
                "type": "string"
              },
              "consumer_secret": {
                "type": "string"
              }
            },
            "required": [
              "access_token",
              "access_token_secret",
              "consumer_key",
              "consumer_secret"
            ]
          }
        }
      },
      "required": [
        "account_id",
        "type",
        "options"
      ]
    },
    {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "account_id": {
          "type": "string",
        },
        "type": {
          "const": "instagram"
        },
        "options": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "access_token": {
                "type": "string"
              }
            },
            "required": [
              "access_token"
            ]
          }
        }
      },
      "required": [
        "account_id",
        "type",
        "options"
      ]
    }
  ]
}

感谢您的任何建议!

4

0 回答 0