jsonschema - 在 IF 条件中引用内部属性 - JSON Schema
问题描述
以下是描述问题的示例架构
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"definitions": {
"person": {
"type": "object",
"properties": {
"age": {
"type": "string"
}
}
}
},
"properties": {
"child": {
"$ref": "#/definitions/person"
}
},
"required": [
"child"
],
"if": {
"properties": {
"person/age": {
"const": "3"
}
}
},
"then": {
"properties": {
"guardian": {
"$ref": "#/definitions/person"
}
},
"required": [
"guardian"
]
}
}
有没有办法在 person 对象中引用年龄?
{"child":{"age":"3"}}
. 由于缺少监护人标签,应该会失败
由于缺少监护人对象,上述数据应该会失败。
解决方案
请记住,这if
只是针对实例验证的常规模式。就像嵌套properties
任何嵌套对象结构一样。
{
"type": "object",
"properties": {
"child": {
"type": "object",
"properties": {
"age": { "const": "3" }
},
"required": ["age"]
}
},
"required": ["child"]
}
请注意,type
andrequired
关键字对于不会无意中触发then
架构是必要的。例如,如果没有它们,这些将导致then
在您可能不希望触发时触发。
{}
{ "child": null }
{ "child": {} }
推荐阅读
- c++ - C++)如何从一个类中使用其他类的方法?
- javascript - 如何使用 JavaScript 或 Node.js 删除 PDF 注释?
- vue.js - Vue js 生成未定义的新表格行数据数组
- google-chrome - ASP.NET Core 3 应用程序不刷新浏览器中的 HTML/CSS 更改
- java - Unicode 字符错误的 java.net.URI 非标准编码
- python - Django 实时更新字段并验证
- bash - 用户无法从 bash 脚本中找到 npm
- reactjs - 用于链式函数调用的 Jest 单元测试
- excel - 找到两个单元格之间的最大值,但不包括单元格
- ios - 如何在 SnapKit 中按值增加视图高度?