json - 针对包含多个模式定义的 JSON 模式进行验证

标签 json postman jsonschema

我正在创建一个用于 Postman 的 JSON 架构,其中包含一些架构定义。我根据模式验证的 JSON 需要与模式定义之一匹配。

示例架构

{
  "oneOf": [
    {
      "$ref": "#/definitions/schema1"
    },
    {
      "$ref": "#/definitions/schema2"
    },
    {
      "$ref": "#/definitions/schema3"
    }
  ],
  "definitions": {
    "schema1": {
      "type": "object",
      "properties": {
        "propertyA": {
          "type": "string"
        }
      },
      "required": [
        "propertyA"
      ]
    },
    "schema2": {
      "type": "object",
      "properties": {
        "propertyB": {
          "type": "string"
        }
      },
      "required": [
        "propertyB"
      ]
    },
    "schema3": {
      "type": "object",
      "properties": {
        "propertyC": {
          "type": "string"
        }
      },
      "required": [
        "propertyC"
      ]
    }
  }
}

示例 JSON 数据

此 JSON 根据架构进行验证并正确标记为无效(因为需要字符串):

{
    "propertyA": 123
}

问题

此示例根据 https://www.jsonschemavalidator.net/ 返回 4 个错误:

  • 消息:JSON 对“oneOf”中的任何模式均无效。架构路径: #/一个
  • 消息:类型无效。预期的字符串但得到了整数。架构路径: #/definitions/schema1/properties/propertyA/类型
  • 消息:对象缺少必需的属性:propertyC。
    架构路径:#/definitions/schema3/required
  • 消息:对象缺少必需的属性:propertyB。
    架构路径:#/definitions/schema2/required

我只对提示需要字符串的错误消息感兴趣。 如何在将架构定义保存在 1 个文件中的同时避免那些其他错误消息?

最佳答案

是的,oneOf 对这种事情来说很糟糕。 if/then 很冗长,但可以提供更好的结果。基本上,您需要确定一些条件来确定架构是否应应用于实例。

通常,该条件是公共(public)字段的值。在这种情况下,如果被验证的实例是具有 "type": "A" 的对象,那么它必须针对 /definitions/a 模式进行验证。如果它有 "type": "B" 那么它必须根据 /definitions/b 模式进行验证。

{
  "allOf": [
    {
      "if": {
        "properties": {
          "type": { "const": "A" }
        },
        "required": ["type"]
      },
      "then": { "$ref": "#/definitions/a" }
    },
    {
      "if": {
        "properties": {
          "type": { "const": "B" }
        },
        "required": ["type"]
      },
      "then": { "$ref": "#/definitions/b" }
    }
  ]
}

如果您的条件是特定字段的存在,您可以使用 dependencies 关键字作为快捷方式。如果被验证的实例是一个具有“propertyA”属性的对象,那么该实例必须对 /definitions/a 模式有效。对于“propertyB”也是如此。

{
  "dependencies": {
    "propertyA": { "$ref": "#/definitions/a" },
    "propertyB": { "$ref": "#/definitions/b" }
  }
}

您的示例实际上有一个 super 简单的解决方案,但我回答了一般情况,因为我假设您的实际架构比示例更复杂。

{
  "type": "object",
  "properties": {
    "propertyA": { "type": "string" },
    "propertyB": { "type": "string" },
    "propertyC": { "type": "string" }
  },
  "oneOf": [
    { "required": ["propertyA"] },
    { "required": ["propertyB"] },
    { "required": ["propertyC"] }
  ]
}

关于json - 针对包含多个模式定义的 JSON 模式进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66462175/

相关文章:

Postman GET 请求到 Binance API

c# - 根据 JSON Schema C# 验证 JSON

json - Jackson JsonTypeInfo.As.EXTERNAL_PROPERTY 无法按预期工作

php - 创建两个数组之间的关系

c# - 正则表达式检索第二个捕获组

javascript - Postman:在预请求脚本中从 JSON 文件获取数据

json - 如何解决flutter中的http error code 400 missing parameters?

带有 JSON 和原始正文的 PHP cURL

jsonschema - 如何验证单个 JSON Schema 上的输入和输出(带有只读的边缘情况)

json - 当元素是可选的时,如何在 json 模式中定义选择元素?