我正在尝试制作用于验证策略语言的模式。简短:
策略与断言相关联。此断言可以是运算符(和,或不),并包含其他断言的列表。断言也可以是原语(叶节点)。
我做了一个UML设计,让事情更容易理解:
{
"policy": {
"name": "test",
"expression": {
"operator": "all",
"value": [
{
"primitive": "encrypt",
"preference": 12345,
"usage": "required"
},
{
"primitive": "sign",
"preference": 12345,
"usage": "required"
}
],
"preference": 12345,
"usage": "required"
}
}
}
这是我目前制定的计划:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"minProperties": 1,
"additionalProperties": {
"$ref": "#/definitions/policy"
},
"definitions": {
"policy": {
"title": "Policy",
"type": "object",
"required": [
"name",
"expression"
],
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"expression": {
"$ref": "#/definitions/assertion"
}
}
},
"operator": {
"properties": {
"value": {
"type": "array",
"items": {
"$ref": "#/definitions/assertion"
}
},
"operator": {
"enum": [
"allOne",
"all"
]
}
}
},
"primitive": {
"properties": {
"primitive": {
"enum": [
"encrypt",
"sign"
]
}
}
},
"assertion": {
"type": "object",
"additionalProperties": false,
"oneOf": [
{
"$ref": "#/definitions/operator"
},
{
"$ref": "#/definitions/primitive"
}
],
"properties": {
"preference": {
"type": "integer",
"minimum": 0,
"exclusiveMinimum": true
},
"usage": {
"enum": [
"required",
"rejected",
"optional",
"observed",
"ignored"
]
}
}
}
}
}
使用"oneOf"
我试图使用运算符或基元的规范。但是我不确定这是否可行,因为我收到了以下错误:
"消息" :"对象实例具有架构不允许的属性:[\" operator \",\" value \"]"
错误消息会抱怨未定义的额外属性(因为"additionalProperties": false
)。但是,这些是在定义中定义的......
答案 0 :(得分:1)
oneOf
,anyOf
,...关键字不能用于引用其他定义。它们适用于required
。解决方案是声明所有属性,并且只需要上下文中所需的属性。我的例子如下:
"assertion": {
"type": "object",
"additionalProperties": false,
"oneOf": [
{
"required": [
"operator"
]
},
{
"required": [
"primitive"
]
}
],
"properties": {
"operator": {
"$ref": "#/definitions/operator"
},
"primitive": {
"$ref": "#/definitions/primitive"
},
"preference": {
"type": "integer",
"minimum": 0,
"exclusiveMinimum": true
},
"usage": {
"enum": [
"required",
"rejected",
"optional",
"observed",
"ignored"
]
}
}
}