JSON 对数组中特定对象属性的模式条件检查
JSON schema conditional check on certain object attribute within an array
我想通过 AJV - JSON 模式验证或自定义关键字(最好我会这样做)做的事情:数组可以有 1 或 2 个 JSON 对象,类型为 'admin' 和 'guest'。 "type":"guest" 对象将始终存在,"type":"admin" 对象是可选的。
补充说明:
-对象本身可能包含附加属性和嵌套对象,以后
-其他有效枚举是超级管理员、管理员、用户和来宾
-数组中的类型顺序为:superadmin、admin、user、guest。是否可以检查顺序? (虽然它是可选的)
-'guest'类型的对象会一直存在,并且会有唯一类型的对象。如果再次出现任何对象类型(例如超级管理员、管理员、用户和来宾),那么它是一个错误
//架构如下:
{
"type": "object",
"properties": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": { "enum": ["guest", "admin"]
},
"rights": {"type": "string"},
"hyperLink": {"type": "string", "format": "uri"}
}
}
}
}
我需要在 json 的某处添加 'checkTypeAndValue' 标志,以便我可以获取完整的 JSON 对象和相应的属性来进行程序检查?
const checkTypeAndValue = function (schema, completeJSONObj) {
//
};
ajv.addKeyword('checkTypeAndValue', {
validate: checkTypeAndValue,
errors: true
});
下面是一些有效和无效的例子:
/Valid 1: As type is 'admin' and so 'rights' SHOULD NOT be in 'guest' object
{
[
{
"type":"admin",
"hyperLink": "http://www.someguest.com",
"rights":"all"
},
{
"type":"guest",
"hyperLink": "http://www.someadmin.com"
}
]
}
//Valid 2: You can have a SINGLE 'guest' object. 'admin' object is not required all the time
{
[
{
"type":"guest",
"hyperLink": "http://www.someadmin.com",
"rights": "limited" //MANDATORY or REQUIRED Attribute
}
]
}
//InValid
{
[
{
"type":"admin",
"hyperLink": "http://www.someguest.com",
"rights":"all"
},
{
"type":"guest",
"hyperLink": "http://www.someadmin.com",
"rights":"limited"
//Error ==> As rights=all is there in 1st object, you cannot set 'rights' to any value including blank even having 'rights' attribute is not valid.
}
]
}
下面是我需要整理的if else条件:
//Assuming admin object exist with rights....
if( type == admin && rights != ""){
if(type == guest && rights attribute is there && rights != ""){
//The 'guest' object will always be there....
//error: guest 'rights' cannot have a value if type is 'admin' and rights is 'all' or any other value.
}
}else{
//Assuming mandatory guest object exist with rights....
if(guest.rights does not exist OR guest.rights == "")
//Error: 'rights' is MANDATORY attribute in guest block and error if its empty
else
//Everything is fine
}
另外,有没有什么方法可以让我们在数组中检查只有一对特定类型的对象?
例如:只有一个'客人,'admin'类型。错误,如果'guest'或'admin'
不止一种
//完整示例
{
[
{
"type":"superadmin",
"hyperLink": "http://www.superadmin.com"
},
{
"type":"admin",
"hyperLink": "http://www.admin.com",
"rights":"all"
},
{
"type":"user",
"hyperLink": "http://www.user.com"
},
{
"type":"guest",
"hyperLink": "http://www.guest.com"
}
]
}
使用 JSON 模式执行此操作似乎有点棘手,但这是可能的。
您需要能够有条件地检查否定条件。
因此,您需要结合使用 if
和 then
,以及 not
。
首先条件:是否有管理员用户。使用 if
,我们可以有条件地应用进一步的检查规则。
接下来,检查:guest 用户不能有 rights
.
then
在 if
通过验证时应用。然后我们需要使用 not
.
否定 guest
有 rights
的检查
您可以看到此正确失败的验证示例,您使用 https://jsonschema.dev 提供的预期无效 JSON 数据(link 预加载了此架构和您提供的实例数据。顺便说一句,它在浏览器中使用 AJV。)
更新:我已经更新了架构以满足您的额外要求。
我还更新了上面的演示 link。
架构现在只允许管理员在管理员存在时拥有权限,如果管理员在场,则其他用户类型可能不具有权限。数组中的一项也必须具有权限(根据您的要求)。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"if": {
"contains": {
"required": [
"type"
],
"properties": {
"type": {
"const": "admin"
}
}
}
},
"then": {
"not": {
"contains": {
"required": [
"type",
"rights"
],
"properties": {
"type": {
"not": {
"const": "admin"
}
}
}
}
}
},
"contains": {
"type": "object",
"required": ["rights"]
},
"items": {
"type": "object",
"properties": {
"type": {
"enum": [
"guest",
"admin"
]
},
"rights": {
"type": "string"
},
"hyperLink": {
"type": "string",
"format": "uri"
}
}
}
}
我想通过 AJV - JSON 模式验证或自定义关键字(最好我会这样做)做的事情:数组可以有 1 或 2 个 JSON 对象,类型为 'admin' 和 'guest'。 "type":"guest" 对象将始终存在,"type":"admin" 对象是可选的。
补充说明:
-对象本身可能包含附加属性和嵌套对象,以后
-其他有效枚举是超级管理员、管理员、用户和来宾
-数组中的类型顺序为:superadmin、admin、user、guest。是否可以检查顺序? (虽然它是可选的)
-'guest'类型的对象会一直存在,并且会有唯一类型的对象。如果再次出现任何对象类型(例如超级管理员、管理员、用户和来宾),那么它是一个错误
//架构如下:
{
"type": "object",
"properties": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": { "enum": ["guest", "admin"]
},
"rights": {"type": "string"},
"hyperLink": {"type": "string", "format": "uri"}
}
}
}
}
我需要在 json 的某处添加 'checkTypeAndValue' 标志,以便我可以获取完整的 JSON 对象和相应的属性来进行程序检查?
const checkTypeAndValue = function (schema, completeJSONObj) {
//
};
ajv.addKeyword('checkTypeAndValue', {
validate: checkTypeAndValue,
errors: true
});
下面是一些有效和无效的例子:
/Valid 1: As type is 'admin' and so 'rights' SHOULD NOT be in 'guest' object
{
[
{
"type":"admin",
"hyperLink": "http://www.someguest.com",
"rights":"all"
},
{
"type":"guest",
"hyperLink": "http://www.someadmin.com"
}
]
}
//Valid 2: You can have a SINGLE 'guest' object. 'admin' object is not required all the time
{
[
{
"type":"guest",
"hyperLink": "http://www.someadmin.com",
"rights": "limited" //MANDATORY or REQUIRED Attribute
}
]
}
//InValid
{
[
{
"type":"admin",
"hyperLink": "http://www.someguest.com",
"rights":"all"
},
{
"type":"guest",
"hyperLink": "http://www.someadmin.com",
"rights":"limited"
//Error ==> As rights=all is there in 1st object, you cannot set 'rights' to any value including blank even having 'rights' attribute is not valid.
}
]
}
下面是我需要整理的if else条件:
//Assuming admin object exist with rights....
if( type == admin && rights != ""){
if(type == guest && rights attribute is there && rights != ""){
//The 'guest' object will always be there....
//error: guest 'rights' cannot have a value if type is 'admin' and rights is 'all' or any other value.
}
}else{
//Assuming mandatory guest object exist with rights....
if(guest.rights does not exist OR guest.rights == "")
//Error: 'rights' is MANDATORY attribute in guest block and error if its empty
else
//Everything is fine
}
另外,有没有什么方法可以让我们在数组中检查只有一对特定类型的对象? 例如:只有一个'客人,'admin'类型。错误,如果'guest'或'admin'
不止一种//完整示例
{
[
{
"type":"superadmin",
"hyperLink": "http://www.superadmin.com"
},
{
"type":"admin",
"hyperLink": "http://www.admin.com",
"rights":"all"
},
{
"type":"user",
"hyperLink": "http://www.user.com"
},
{
"type":"guest",
"hyperLink": "http://www.guest.com"
}
]
}
使用 JSON 模式执行此操作似乎有点棘手,但这是可能的。
您需要能够有条件地检查否定条件。
因此,您需要结合使用 if
和 then
,以及 not
。
首先条件:是否有管理员用户。使用 if
,我们可以有条件地应用进一步的检查规则。
接下来,检查:guest 用户不能有 rights
.
then
在 if
通过验证时应用。然后我们需要使用 not
.
guest
有 rights
的检查
您可以看到此正确失败的验证示例,您使用 https://jsonschema.dev 提供的预期无效 JSON 数据(link 预加载了此架构和您提供的实例数据。顺便说一句,它在浏览器中使用 AJV。)
更新:我已经更新了架构以满足您的额外要求。 我还更新了上面的演示 link。
架构现在只允许管理员在管理员存在时拥有权限,如果管理员在场,则其他用户类型可能不具有权限。数组中的一项也必须具有权限(根据您的要求)。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"if": {
"contains": {
"required": [
"type"
],
"properties": {
"type": {
"const": "admin"
}
}
}
},
"then": {
"not": {
"contains": {
"required": [
"type",
"rights"
],
"properties": {
"type": {
"not": {
"const": "admin"
}
}
}
}
}
},
"contains": {
"type": "object",
"required": ["rights"]
},
"items": {
"type": "object",
"properties": {
"type": {
"enum": [
"guest",
"admin"
]
},
"rights": {
"type": "string"
},
"hyperLink": {
"type": "string",
"format": "uri"
}
}
}
}