json 架构 - 父数组(对象数组)项具有具有给定值的内部数组(基元数组)



请随时更新此问题的合适标题。

我正在努力实现这样的事情。输入 JSON/数据 JSON 必须具有所有者和CHARGE_TO帐户,但它们不必位于同一帐户上(即一个帐户可以是 OWNER,另一个帐户可以是 OWNER,另一个帐户可以是CHARGE_TO(,并且不得包含具有任何其他角色的帐户。

注意:需要定义JSON模式,该模式应该易于维护,即应该易于在条件中添加新角色。我的有效和无效 JSON 是,

*************************** VALID JSON1 *********************************
{
"user": [
{
"name": "user1",
"roles": ["OWNER"]
},
{
"name": "user2",
"roles": ["ANY_OTHER_ROLE"]
},
{
"name": "user3",
"roles": ["CHARGE_TO"]
}]  
}
*************************** VALID JSON2 *********************************
{
"user": [
{
"name": "user1",
"roles": ["OWNER", "CHARGE_TO"]
},
{
"name": "user2",
"roles": ["ANY_OTHER_ROLE"]
}]  
}
*************************** INVALID JSON *********************************
{
"user": [
{
"name": "user1",
"roles": ["OWNER"]
},
{
"name": "user2",
"roles": ["ANY_OTHER_ROLE"]
}]  
}

如果JSON具有具有角色的用户("OWNER">和"CHARGE_TO"(,或者具有角色的用户(user1 - "OWNER",user3-"CHARGE_TO",其他用户具有任何其他角色(,则JSON有效。

以下是我尝试过的 JSON 架构 (draft_07(。这不是一个有效的架构

{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Complex inner array",
"type": "object",
"properties": {
"user": {
"type": "array",
"contains": {
"type": "object",
"properties": {
"name": { "type": "string" },
"orderRoles": {
"type": "array",
"minItems": 1,
"items": { "type": "string" }
}
},
"oneOf": [
{ "properties": { "roles": { "enum": ["OWNER", "CHARGE_TO"] }}},
{ "properties": { "roles": { "enum": ["OWNER"] }}},
{ "properties": { "roles": { "enum": ["CHARGE_TO"] }}}
]
},
"items": {
"type": "object",
"properties": {
"name": { "type": "string" },
"orderRoles": {
"type": "array",
"minItems": 1,
"items": { "type": "string" }
}
}
}
}
}
}

下面是我的工作模式。这不是一个优雅的解决方案,因为如果添加新角色("ADMIN"(,模式就会变得很大,即模式条件将An input JSON/data JSON must have an OWNER, ADMIN and a CHARGE_TO Account, but these don't have to be on the same Account (I.e. one Account can be OWNER, one Account can be ADMIN & another can be CHARGE_TO) and must not contain account with any other roles。请随时改进这一点。谢谢。

{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Complex inner array",
"type": "object",
"allOf": [
{
"properties": {
"account": {
"type": "array",
"contains": {
"type": "object",
"properties": {
"roles": {
"type": "array",
"minItems": 1,
"contains": { "enum": ["OWNER"] }
}
}
}
}
}
},
{
"properties": {
"account": {
"type": "array",
"contains": {
"type": "object",
"properties": {
"roles": {
"type": "array",
"minItems": 1,
"contains": { "enum": ["CHARGE_TO"] }
}
}
}
}
}
},
{
"properties": {
"account": {
"type": "array",
"items": {
"type": "object",
"properties": {
"roles": {
"type": "array",
"minItems": 1,
"items":{
"enum": ["CHARGE_TO", "OWNER"]
}
}
}
}
}
}
}
]
}

您需要隔离架构中检查特定角色的部分。然后你可以把它们与allOf.重复越少,您可以轻松地添加另一个约束。

{
"type": "object",
"properties": {
"user": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": { "type": "string" },
"roles": {
"type": "array",
"items": { "type": "string" }
}
}
},
"allOf": [
{ "$ref": "#/definitions/require-role-owner" },
{ "$ref": "#/definitions/require-role-charge-to" }
]
}
},
"definitions": {
"require-role-owner": {
"contains": {
"properties": {
"roles": {
"contains": { "const": "OWNER" },
"allOf": [{ "$ref": "#/definitions/not-other-role" }]
}
}
}
},
"require-role-charge-to": {
"contains": {
"properties": {
"roles": {
"contains": { "const": "CHARGE_TO" },
"allOf": [{ "$ref": "#/definitions/not-other-role" }]
}
}
}
},
"not-other-role": {
"items": { "enum": ["OWNER", "CHARGE_TO"] }
}
}
}

最新更新