json模式对象属性约束 [英] json schema object property constraints

查看:56
本文介绍了json模式对象属性约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模式中,我有一组电话对象.每个对象都有一个状态"属性,该属性可以是三个值之一:主要",活动"和未使用".

In my schema I have an array of phone objects. Each object has a "status" property, which can be one of three values: "Primary", "Active" and "Not-in-use".

我要设置以下约束: 如果电话对象的数量> 0,则恰好一个对象必须具有status ="Primary"

I want to set the following constraint: If the number of phone objects > 0 then exactly one must have status="Primary"

json模式可能吗?如果可以,怎么办?

Is this possible with json schema? If so, how?

推荐答案

此架构非常接近您想要的架构.唯一的限制是主"电话号码必须是数组中的第一项.

This schema is pretty close to what you want. The only restriction is that the "Primary" phone number needs to be the first item in the array.

通过创造性地使用not,您可能可以将"Primary"设置为数组中的任何位置.如果知道了,我将更新答案.

You might be able to get "Primary" to be anywhere in the array with some creative use of not. I'll update the answer if I figure it out.

{
  "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"] }
      } 
    }
  }
}

这篇关于json模式对象属性约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆