【问题标题】:Json-schema - How to verify that a specific element is not in an array?Json-schema - 如何验证特定元素不在数组中?
【发布时间】:2020-05-12 05:43:50
【问题描述】:

我有一个帐户列表(accountId、currency、accountType)

Id1 ,美元,账户 / Id2、英镑、账户/ Id3 , 欧元, 账户 / Id4、NOK、账户

当我调用 api 以获取帐户列表时,我想确保 accountid = 3 不在结果中。我怎样才能做到这一点?这是我的架构:

    {
  "title": "Acc schema",
  "type": "object",
  "definitions": {
    "account": {
      "properties": {
        "accountId": {
          "type": "string",
          "minLength": 1,
          "maxLength": 15
        },
        "currency": {
          "type": "string",
          "minLength": 3,
          "maxLength": 3
        },
        "accountType": {
          "type": "string",
          "enum": [
            ”Account”
          ]
        }
      },
      "required": ["accountId"],
      "additionalProperties": false
    },
    "data": {
      "type": "object",
      "properties": {
        "accounts": {
          "type": "array",
          "items": {
            "anyOf": [
              {
                "$ref": "#/definitions/account"
              }
            ],
            "additionalItems": false
          }
        },
        "additionalProperties": false
      }
    }
  },
  "properties": {
    "data": {
      "$ref": "#/definitions/data"
    },
    "headers": {
      "type": "object"
    },
    "config": {
      "type": "object"
    },
    "request": {
      "type": "object"
    },
    "status": {
      "type": "number"
    },
    "statusText": {
      "type": "string"
    }
  },
  "additionalProperties": false
}

【问题讨论】:

    标签: arrays jsonschema


    【解决方案1】:

    主要的关键字是notcontains

    您在definitions 中的data 条目可能看起来像这样(也没有不必要的anyOf):

        "data": {
          "type": "object",
          "properties": {
            "accounts": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/account"
              },
              “not”: {
                “contains”: {
                  “type”: “object”,
                  “properties”: {
                    “accountId”: {
                      “const”: “3”
                    }
                  }
                }
              }
            }
          }
          "additionalProperties": false
        }
    

    【讨论】:

    • 如果这回答了您的问题,请随时通过左侧的勾号将其标记为“已接受”。这样,其他人就不会花时间不必要地提供替代答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多