jsonschema - 是否需要在 AdditionalProperties 中添加dependentSchemas 属性
问题描述
我正在尝试编写一个模式,如果存在属性 A,那么属性 B 或 C 也应该使用依赖模式构造来存在,并且在我的模式中,我已经关闭了任何附加属性。
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "PropertiesSchema.json",
"type": "object",
"properties": {
"A": { "type": "boolean" }
},
"additionalProperties": false,
"dependentSchemas": {
"A": {
"anyOf": [
{ "required": [ "B" ] },
{ "required": [ "C" ] }
],
"properties": {
"B": { "type": "boolean" },
"C": { "type": "boolean" }
}
}
}
}
但由于 additionalProperties 构造,它目前在以下输入中失败
{“A”:真,“B”:假}或{“A”:真,“C”:假}
那么在 AdditionalProperties 关闭时是否允许dependentSchemas 属性?
因此有效的输入应该是
{“A”:真,“B”:假}或{“A”:真,“C”:假}
但是对于任何其他属性说D,它应该失败-
{“A”:真,“D”:假}
解决方案
additionalProperties
只能在相同的模式对象级别考虑properties
(和)。patternProperties
如果您将其更改为,unevaluatedProperties
您会发现它可以正常工作。
unevaluatedProperties
可以“看穿”涂抹器关键字,例如dependentSchemas
. 它必须等待其他关键字首先被解析,最后被解析。
推荐阅读
- swift - 如何在不从 UIView 子类化的情况下创建自定义视图
- c# - 如何在 F# 中使用 Lookup MongoDB
- performance - 链表上意外的缓慢元素下降
- apache-spark-sql - 结构爆炸数组,spark-sql 为 orc 文件格式返回 null
- python - 编写井字游戏程序
- r - 如何限制 igraph/R 中最短路径的数量
- android - 移动应用程序的 Python REST API 登录/身份验证/授权
- javascript - 使用 Cypress 进行测试时如何拒绝警告框?
- python - 如何用 0 替换数组的某些元素?
- mysql - XAMPP 3.2.4 MySQL 启动后崩溃