json - 使用 tv4 或任何其他验证器的 JSON Schema 验证
问题描述
我在数组中有某种 JSON 结构。要求是:
1. All the JSON object within the array can be optional.
2. Each JSON can have its own set of properties and can be complex and nested.
3. Each JSON object will have a set of mandatory attributes.
如何为此类 JSON 创建模式。使用 anyOf 或定义会有帮助吗?
更新:我有一个 JSON 对象数组,其中每个对象可以有不同的属性。唯一常见的属性是“类型”,其有效值为:电子产品、家具或金融。所以我的问题是如何派生模式?
例子
{
"list": [
{
"type": "electronics"
},
{
"type": "furniture"
},
{
"accessRights": "readOnly",
"rules": ['print','copy'],
"type": "finance"
}
}
解决方案
{
"properties": {
"list": {
"type": "array",
"items": {
"type": "object",
"required": ["type"],
"properties": {
"type": {
"type": "string",
"enum": ["electronics", "furniture", "finance"]
}
},
"anyOf": [{
"properties": {
"type": {
"enum": ["electronics"]
}
}
}, {
"properties": {
"type": {
"enum": ["furniture"]
}
}
}, {
"properties": {
"type": {
"enum": ["finance"]
},
"accessRights": {
"type": "string"
},
"rules": {
"type": "array"
}
}
}]
}
}
}
}
解决方案
推荐阅读
- docker - 如何在 gitlab 管道中转发 .gitlab-ci ssh-add 命令?
- scala - Spark(scala)数据框 - 检查列中的字符串是否存在于另一个数据框的列中
- javascript - 如果折扣有效,lodash 按 discountPrice 排序数组
- java - 在 Google Dataproc Spark 中链接原生 .so
- python - Pandas 从列表中设置 dtype
- c++ - 是否可以将通用 lambda 作为非模板参数传递
- c++ - 如何使用 gdb 调试 g++ 分段错误?
- python - 处理列名中的括号
- python - Seaborn 图在 ipython 单元格中显示乱序
- php - 我的分页在 wordpress 自定义主题中不起作用