【发布时间】:2021-12-05 22:53:08
【问题描述】:
我有一个 JSON Schema,它允许对象具有一个或多个属性:
"properties": {
"alpha": {
"$ref": "#/$defs/alpha"
},
"beta": {
"$ref": "#/$defs/beta"
}
},
"anyOf": [
{
"required": [
"alpha"
]
},
{
"required": [
"beta"
]
}
]
使用 ajv compile 并启用 strict 模式来验证架构本身,验证失败。我该如何解决?
更新
看起来这是一个已知问题,目前无法解决:
我已尝试在 properties 上使用 anyOf,但虽然此架构验证,但它无法按预期工作:
"anyOf": [
{
"properties": {
"alpha": {
"$ref": "#/$defs/alpha"
}
},
"required": [
"alpha"
]
},
{
"properties": {
"beta": {
"$ref": "#/$defs/beta"
}
},
"required": [
"beta"
]
}
]
Ajv 命令
ajv compile --spec=draft2020 --validate-formats=true --verbose --all-errors --strict=true -c ajv-formats -s repro.json
Ajv 错误
schema repro.json is invalid
error: strict mode: required property "beta" is not defined at "repro.json#/anyOf/0" (strictRequired)
完整的 JSON 架构
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "#/$defs/root",
"$defs": {
"root": {
"type": "object",
"additionalProperties": false,
"properties": {
"alpha": {
"$ref": "#/$defs/alpha"
},
"beta": {
"$ref": "#/$defs/beta"
}
},
"anyOf": [
{
"required": [
"alpha"
]
},
{
"required": [
"beta"
]
}
]
},
"alpha": {
"type": "object",
"additionalProperties": false,
"properties": {
"foo": {
"type": "string"
}
}
},
"beta": {
"type": "object",
"additionalProperties": false,
"properties": {
"bar": {
"type": "string"
}
}
}
}
}
JSON 示例
{
"alpha": {
"foo": "xyz"
}
}
【问题讨论】:
标签: json jsonschema ajv