字段的JSON模式验证可以是可选字段,也可以是必需字段,具体取决于另一个字段的值

时间:2019-07-24 10:07:55

标签: json jsonschema json-schema-validator

是否有一种方法可以告诉您的JSON模式,根据一个字段的值,另外两个字段将变为必填或可选状态?

我的JSON数据:

{
      "id": "111-222-333",
      "indicatorId": "indicator1",
      "serviceId": "service1",
      "time": "1992-03-03",
      "locations": null,
      "valueIsRange": true,
      "value": null,
      "rangeValue": {
        "fromValue": "10",
        "toValue": "20"
      } 
}

我的JSON模式:

{
    "properties": {
        "id": {
            "type": "string"
        },
        "indicatorId": {
            "type": "string"
        },
        "locations": {
            "items": {
                "type": ["string", "null"]
            },
            "type": "array"
        },
        "rangeValue": {
            "properties": {
                "fromValue": {
                    "type": "string"
                },
                "toValue": {
                    "type": "string"
                }
            },
            "required": [
                "fromValue",
                "toValue"
            ],
            "type": ["object", "null"]
        },
        "serviceId": {
            "type": "string"
        },
        "time": {
            "type": ["string", "number"]
        },
        "value": {
            "type": ["string", "null"]
        },
        "valueIsRange": {
            "type": "boolean"
        }
    },
    "required": [
        "id",
        "indicatorId",
        "serviceId",
        "valueIsRange"
    ]
}

我的目标是取决于“ valueIsRange”的值,“ value”和“ rangeValue”字段将更改状态。

更具体地说,当“ valueIsRange”为false时,则“ value”变为必需,而“ rangeValue”变为可选。 另一方面,当“ valueIsRange”为true时,“ value”变为false,并且需要“ rangeValue”。

使用特定的架构结构,我可以将“ value”和“ rangeValue”都设置为null。我需要不断设置1个null和1个不为null的

有没有办法实现这一目标?

谢谢!

0 个答案:

没有答案
相关问题