【发布时间】:2017-02-14 14:27:17
【问题描述】:
我正在尝试编写一个架构来验证 AWS IAM 安全组不得指定传入的 IP 地址“0.0.0.0/0”可以连接到端口 22。
我使用 oneOf 运算符并定义了两组属性,我的直觉是,如果两个属性都满足,JSON 模式应该会失败,但它不会。
示例 JSON -
{
"ipPermissions": [
{
"toPort": -1,
"fromPort": -1,
"ipRanges": [
"10.0.0.0/16"
]
},
{
"toPort": 22,
"fromPort": 53,
"ipRanges": [
"0.0.0.0/0"
],
"ipProtocol": "tcp"
}
]
}
上述 JSON 应该失败,因为 ipPermission[1] 对象是-
{
"toPort": 22,
"fromPort": 53,
"ipRanges": [
"0.0.0.0/0"
],
"ipProtocol": "tcp"
}
当toPort 为22 时,ipRanges 的值为0.0.0.0/0
以下 JSON 文档应通过验证-
{
"ipPermissions": [
{
"toPort": 22,
"fromPort": -1,
"ipRanges": [
"10.0.0.0/16"
]
},
{
"toPort": 22,
"fromPort": 53,
"ipRanges": [
"somethingElse"
],
"ipProtocol": "tcp"
}
]
}
因为ipPermissions index[0] 对象的toPort 值为22 但ipRanges[0] 的值为10.0.0.0/16 而不是0.0.0.0/0
以下 JSON 不应通过验证 -
{
"ipPermissions": [
{
"toPort": 22,
"fromPort": -1,
"ipRanges": [
"10.0.0.0/16"
]
},
{
"toPort": 22,
"fromPort": 53,
"ipRanges": [
"somethingElse",
"0.0.0.0/0"
],
"ipProtocol": "tcp"
}
]
}
因为ipPermissions[1].ipRanges[1] 的值为0.0.0.0/0
我的 JSON 架构-
{
"$schema": "http://json-schema.org/draft-04/schema#",
"required": [
"ipPermissions"
],
"properties": {
"ipPermissions": {
"type": "array",
"items": {
"type": "object",
"properties": {
"oneOf": {
"ipRanges": {
"type": "array",
"items": {
"type": "string",
"value": "0.0.0.0/0"
}
},
"toPort": {
"type": "integer",
"minimum": 23
}
}
}
}
}
}
}
【问题讨论】:
-
我无法弄清楚您要表达什么。您能否扩展您的描述和/或添加更多应该通过和应该失败的示例?
-
@Json 我提供了更多示例。
标签: json schema jsonschema