首页 文章

json架构对象属性约束

提问于
浏览
1

在我的架构中,我有一组电话对象 . 每个对象都有一个“status”属性,它可以是三个值之一:“Primary”,“Active”和“Not-in-use” .

我想设置以下约束:如果电话对象的数量> 0,那么一个必须具有status =“Primary”

这有可能与json架构?如果是这样,怎么样?

1 回答

  • 0

    这个架构非常接近你想要的 . 唯一的限制是“主要”电话号码必须是数组中的第一项 .

    您可以通过 not 的一些创造性使用将"Primary"放在数组中的任何位置 . 如果我搞清楚,我会更新答案 .

    {
      "type": "object",
      "properties": {
        "phoneNumbers": {
          "type": "array",
          "items": [{ "$ref": "#/definitions/primaryPhone" }],
          "additionalItems": { "$ref": "#/definitions/additionalPhone" }
        }
      },
      "definitions": {
        "phone": {
          "type": "object",
          "properties": {
            "label": { "type": "string" },
            "number": { "type": "string" }
          },
          "required": ["label", "number", "status"]
        },
        "primaryPhone": {
          "allOf": [{ "$ref": "#/definitions/phone" }],
          "properties": {
            "status": { "enum": ["Primary"] }
          }
        },
        "additionalPhone": {
          "allOf": [{ "$ref": "#/definitions/phone" }],
          "properties": {
            "status": { "enum": ["Active", "Not-in-use"] }
          } 
        }
      }
    }
    

相关问题