【问题标题】:How to validate enumerated types in JSON如何验证 JSON 中的枚举类型
【发布时间】:2020-09-22 11:40:17
【问题描述】:

我有以下 JSON 架构:

{  
  "type": "object",
  "properties": {
    "Ingredients": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "Description": {
              "type": "string"
            },
            "ProductType": {
              "enum": [
                "Cheese",
                "Salad",
                "Condiment"
              ],
              "type": "string"
            }
          },
          "required": [
            "Description",                        
            "ProductType"
          ]
        }
      ]
    }
  }
}

还有下面的 JSON sn-p:

{
  "Ingredients": [
    {
      "Description": "Cheddar",
      "ProductType": "Cheese"
    },
    {
      "Description": "Rocket",
      "ProductType": "Salad"
    },
    {
      "Description": "Onion Relish",
      "ProductType": "Condiment"
    },
    {
      "Description": "Parma Ham",
      "ProductType": "SlicedMeat"
    }
  ]
}

我预计这里的验证会失败,因为“SlicedMeat”不是验证枚举。

知道我需要如何在架构中定义它吗?

我正在使用https://www.jsonschemavalidator.net/,这会有所不同。

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    我认为你的架构有问题。

    如果您查看以下网站:https://json-schema.org/learn/getting-started-step-by-step.html

    您在标题中看到:“深入了解属性”

    数组的定义如下:

    {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
    

    因此,如果您将架构更改为以下内容:(编辑类型数组定义),您应该会得到预期的行为

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "$id": "http://example.com/product.schema.json",
      "title": "Product",
      "description": "A product from Acme's catalog",
      "type": "object",
      "properties": {
        "Ingredients": {
          "type": "array",
          "items": {
              "type": "object",
              "properties": {
                "Description": {
                  "type": "string"
                },
                "ProductType": {
                  "type": "string",
                  "enum": [
                    "Cheese",
                    "Salad",
                    "Condiment"
                  ]
                }
              },
              "required": [
                "Description",
                "ProductType"
              ]
            }
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      在 2019 年 9 月及之前的草案中,items 可能是数组或架构对象。

      当它是一个数组时,该数组中的每个项目(它们是架构对象),它们被顺序应用于适用的数组。

      您正在寻找items 的单一架构版本。

      这是一个常见的混淆,我们已将其简化为草案2020-NN,该草案仍在制作中。

      来源:http://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.3.1.1

      如果“items”是一个模式,则验证成功,如果所有元素在 数组已成功验证该架构。

      如果“items”是一个模式数组,则验证成功,如果每个元素 实例的验证针对同一位置的架构,如果 任何。

      【讨论】:

        猜你喜欢
        • 2020-08-30
        • 2017-07-03
        • 1970-01-01
        • 2019-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多