确保 属性 在 JSON 架构中不为空
Ensure one property is not empty in JSON schema
对于下面给定的架构,是否可以确保至少一个 属性 包含一个值(即 minLength 为 1):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"fundRaiseId": {
"type": "string"
},
"productTypeId": {
"type": "string"
},
"businessLineId": {
"type": "string"
}
}
}
所以这将通过验证:
{
"fundRaiseId": "x"
}
这会失败,因为没有值存在:
{
"fundRaiseId": "",
"productTypeId": "",
"businessLineId": ""
}
我会尝试
{
"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
}
}
}
说明:要验证的 JSON 应符合 2 个根级架构,一个是您的原始定义(3 个字符串属性)。另一个包含 3 个额外的子模式,每个子模式将您的一个原始属性定义为非空字符串。它们包含在 "anyOf" 架构中,因此 至少其中一个 应该匹配,加上原始架构。
是否要求您允许值为空?如果您要求所有字符串都非空,则可以编写更简洁的模式。
{
"$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
},
}
}
对于下面给定的架构,是否可以确保至少一个 属性 包含一个值(即 minLength 为 1):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"fundRaiseId": {
"type": "string"
},
"productTypeId": {
"type": "string"
},
"businessLineId": {
"type": "string"
}
}
}
所以这将通过验证:
{
"fundRaiseId": "x"
}
这会失败,因为没有值存在:
{
"fundRaiseId": "",
"productTypeId": "",
"businessLineId": ""
}
我会尝试
{
"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
}
}
}
说明:要验证的 JSON 应符合 2 个根级架构,一个是您的原始定义(3 个字符串属性)。另一个包含 3 个额外的子模式,每个子模式将您的一个原始属性定义为非空字符串。它们包含在 "anyOf" 架构中,因此 至少其中一个 应该匹配,加上原始架构。
是否要求您允许值为空?如果您要求所有字符串都非空,则可以编写更简洁的模式。
{
"$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
},
}
}