是否有可能建立一个JSON模式,它仍然允许additionalProperties但不若一个非常特别的属性名存在匹配吗?换句话说,我需要知道是否可能与required声明完全相反.
架构:
{
"type": "object",
"properties": {
"x": { "type": "integer" }
},
"required": [ "x" ],
"ban": [ "z" ] // possible?
}
Run Code Online (Sandbox Code Playgroud)
比赛:
{ "x": 123 }
Run Code Online (Sandbox Code Playgroud)
比赛:
{ "x": 123, "y": 456 }
Run Code Online (Sandbox Code Playgroud)
难道不匹配:
{ "x": 123, "y": 456, "z": 789 }
Run Code Online (Sandbox Code Playgroud)
Jas*_*ers 23
您可以使用not关键字来实现您想要做的事情.如果not架构验证,则父架构将不会验证.
{
"type": "object",
"properties": {
"x": { "type": "integer" }
},
"required": [ "x" ],
"not": { "required": [ "z" ] }
}
Run Code Online (Sandbox Code Playgroud)
jru*_*ren 13
有一种更简单的方法.定义如果x存在则它不能满足任何模式.通过减少到荒谬x不能存在:
{
"properties" : {
"x" : {
"not" : {}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我通过禁止其他属性解决了这个问题,"additionalProperties": false但使用patternProperties允许除被禁止的属性名称之外的任何属性名称。
{
"type": "object",
"properties": {
"x": { "type": "integer" }
},
"required": [ "x" ],
"patternProperties": {
"^(?!^z$).*": {}
},
"additionalProperties": false
}
Run Code Online (Sandbox Code Playgroud)
要指定不存在字段,您可以预期其类型为null。
{
"type": "object",
"properties": {
"x": { "type": "integer" },
"z": { "type": "null" }
},
"required": [ "x" ]
}
Run Code Online (Sandbox Code Playgroud)