确保 JSON 模式中的一个属性不为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38478406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:30:36  来源:igfitidea点击:

Ensure one property is not empty in JSON schema

jsonjsonschema

提问by Ric

For the given schema below, is it possible to ensure that at least one property contains a value (ie, minLength is 1):

对于下面给定的模式,是否可以确保至少一个属性包含一个值(即 minLength 为 1):

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "fundRaiseId": {
            "type": "string"
        },
        "productTypeId": {
            "type": "string"
        },
        "businessLineId": {
            "type": "string"
        }
    }
}

So this would pass validation:

所以这将通过验证:

{
 "fundRaiseId": "x"
}

And this would fail as no values are present:

这将失败,因为没有值存在:

{
  "fundRaiseId": "",
  "productTypeId": "",
  "businessLineId": ""
}

回答by erosb

I would try something like

我会尝试类似的东西

{
    "allOf": [{
        "type": "object",
        "properties": {
            "fundRaiseId": {
                "type": "string"
            },
            "productTypeId": {
                "type": "string"
            },
            "businessLineId": {
                "type": "string"
            }
        }
    }, {
        "anyOf": [{
            "properties": {
                "fundRaiseId": {
                    "$ref": "#/definitions/nonEmptyString"
                }
            }
        }, {
            "properties": {
                "productTypeId": {
                    "$ref": "#/definitions/nonEmptyString"
                }
            }
        }, {
            "properties": {
                "businessLineId": {
                    "$ref": "#/definitions/nonEmptyString"
                }
            }
        }]
    }],
    "definitions": {
        "nonEmptyString": {
            "type": "string",
            "minLength": 1
        }
    }
}

Explanation: the JSON to be validated should conform to 2 root-level schemas, one is your original definition (3 string properties). The other one contains 3 additional sub-schemas, each defining one of your original properties as non-empty string. These are wrapped in an "anyOf" schema, so at least oneof these should match, plus the original schema.

说明:要验证的 JSON 应符合 2 个根级架构,一个是您的原始定义(3 个字符串属性)。另一个包含 3 个额外的子模式,每个子模式将您的原始属性之一定义为非空字符串。这些被包装在一个“anyOf”模式中,所以至少其中一个应该匹配,加上原始模式。

回答by Jason Desrosiers

Is it a requirement that you allow the values to be empty? You can write a much cleaner schema if you requiring that all the strings are non-empty.

是否要求允许值为空?如果您要求所有字符串都非空,您可以编写一个更清晰的模式。

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "fundRaiseId": { "$ref": "#/definitions/non-empty-string" },
        "productTypeId": { "$ref": "#/definitions/non-empty-string" },
        "businessLineId": { "$ref": "#/definitions/non-empty-string" }
    },
    "anyOf": [
        { "required": ["fundRaiseId"] },
        { "required": ["productTypeId"] },
        { "required": ["businessLineId"] }
    ],
    "definitions": {
        "non-empty-string": {
            "type": "string",
            "minLength": 1
        },
    }
}