[英]JSON schema for anyOf properties
在一个非常简化的形式中,我想定义一个 JSON 模式,它允许
{ "a" : 1 }
首先,属性"reqired"
是错误的,它需要是"required"
。
此外,当您定义属性的类型时,您可以一次指定一个或多个类型:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"a": {
"type": ["integer", "object"]
}
},
"required": ["a"]
}
如果属性 a 是一个对象并且您想要特定内容,您还可以添加其他定义:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"a": {
"type": ["integer", "object"],
"properties": {
"b": {
"type": "integer"
}
},
"required": ["b"]
}
},
"required": ["a"]
}
因此,如果您使用https://www.jsonschemavalidator.net/来验证您的架构,您将通过以下方式:
{"a": 1}
{"a": {"b": 1}}
但这会失败: {"a": 1, "b": 1}
{"a": {"b": 1}, "c":1}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.