【发布时间】:2021-10-26 21:11:44
【问题描述】:
我想以一种非常简化的形式定义一个 JSON 模式,以允许
{ "a" : 1 }
和
{ "a" : {} }
但不是
{ "a" : 1, "b" : true }
而不是
{ "a" : true }
我想出了以下内容。
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties" : false,
"anyOf" : [
{
"properties" : {
"a" : {"type":"object"}
}
},
{
"properties" : {
"a" : {"type":"integer"}
}
}
],
"reqired" : ["a"]
}
但是,根据https://www.jsonschemavalidator.net/,除非我删除我绝对想要的"additionalProperties" : false,否则这是行不通的。什么是正确的定义方式?
【问题讨论】: