1

我有以下 JSON 有效负载,我正在尝试为其中一个元素添加 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"
  } ]
}

这是我的架构,我在使用此网站自动生成后尝试对其进行修改。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"
              }
            }
          }
        ]
      }
    }
  }
}
4

1 回答 1

4

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

{
  "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"
              }
            }
          }
        ]
      }
    }
  }
}
于 2015-03-31T02:27:21.823 回答