JSON Enum Schema问题 - 使用有效的架构纠正我

时间:2015-03-31 01:28:21

标签: json jsonschema json-schema-validator

我有以下JSON Payload,我正在尝试为其中一个元素添加ENUM类型值。

{
  "firstName" : "firstName",
  "lastName" : "lastName",
  "systemIds" : [ {
    "systemName" : "SAP",
    "systemId" : "99c27c63-e0b6-4585-8675-7aa3811eb4c3"
  }, {
    "systemName" : "SFDC",
    "systemId" : "b65abf1d-825d-4ee3-9791-02d2cdd5e6f4"
  }, {
    "systemName" : "MONGODB",
    "systemId" : "18e50430-8589-42d6-8477-58839a8bf202"
  } ]
}

这是我的Schema,我试图在使用本网站自动生成后进行修改。 http://jsonschema.net/#/

我按照我的期望手动添加了手动ENUM类型。请更正此SCHEMA的错误。

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://abcd.com/schemas/customerInfo",
  "type": "object",
  "properties": {
    "firstName": {
      "id": "http://abcd.com/schemas/customerInfo/firstName",
      "type": "string"
    },
    "lastName": {
      "id": "http://abcd.com/schemas/customerInfo/lastName",
      "type": "string"
    },
    "systemIds": {
      "id": "http://abcd.com/schemas/customerInfo/systemIds",
      "type": "array",
      "minItems": 1,
      "uniqueItems": false,
      "additionalItems": true,
      "items": {
        "anyOf": [
          {
            "id": "http://abcd.com/schemas/customerInfo/systemIds/0",
            "type": "object",
            "properties": {
              "systemName": {
                "id": "http://abcd.com/schemas/customerInfo/systemIds/0/systemName",
                "type": { "enum": [ "SAP", "MONGODB", "ERP", "SFDC" ] }
              },"required": ["type"],
              "systemId": {
                "id": "http://abcd.com/schemas/customerInfo/systemIds/0/systemId",
                "type": "string"
              }
            }
          }
        ]
      }
    }
  }
}

1 个答案:

答案 0 :(得分:3)

数组项的架构看起来不正确。

{
  "anyOf": [
    {
      "id": "http://abcd.com/schemas/customerInfo/systemIds/0",
      "type": "object",
      "properties": {
        "systemName": {
          "id": "http://abcd.com/schemas/customerInfo/systemIds/0/systemName",
          "type": {
            "enum": [
              "SAP",
              "MONGODB",
              "ERP",
              "SFDC"
            ]
          }
        },
        "required": [
          "type"
        ],
        "systemId": {
          "id": "http://abcd.com/schemas/customerInfo/systemIds/0/systemId",
          "type": "string"
        }
      }
    }
  ]
}

您声明应该有"required"属性但是您输入的架构无效。这需要删除。但也许你的意思是"type"属性在某处需要并且放错了地方。我没有看到任何关系。

"systemName"属性是一个字符串类型,其值应该在该枚举中。那里的架构无效。

这应该适合你:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://abcd.com/schemas/customerInfo",
  "type": "object",
  "properties": {
    "firstName": {
      "id": "http://abcd.com/schemas/customerInfo/firstName",
      "type": "string"
    },
    "lastName": {
      "id": "http://abcd.com/schemas/customerInfo/lastName",
      "type": "string"
    },
    "systemIds": {
      "id": "http://abcd.com/schemas/customerInfo/systemIds",
      "type": "array",
      "minItems": 1,
      "uniqueItems": false,
      "additionalItems": true,
      "items": {
        "anyOf": [
          {
            "id": "http://abcd.com/schemas/customerInfo/systemIds/0",
            "type": "object",
            "properties": {
              "systemName": {
                "id": "http://abcd.com/schemas/customerInfo/systemIds/0/systemName",
                "type": "string",
                "enum": [ "SAP", "MONGODB", "ERP", "SFDC" ]
              },
              "systemId": {
                "id": "http://abcd.com/schemas/customerInfo/systemIds/0/systemId",
                "type": "string"
              }
            }
          }
        ]
      }
    }
  }
}