json - JSONSchema 验证包含特定项目的字符串数组
问题描述
我正在尝试指定一个必须包含特定属性的字符串数组。
我想指定验证所需的项目。
在此示例中:uuid、模板、createdOn、updatedOn。它必须是一个字符串数组。
我的架构如下所示:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "56544e3c-e197-4602-8457-2c01dc6b12c5",
"title": "The Root Schema",
"type": "object",
"additionalProperties": false,
"required": ["attributes"],
"properties": {
"attributes": {
"type": "array",
"uniqueItems": true,
"items": {
"type": "string",
"enum": ["uuid", "template", "createdOn", "updatedOn", "fields", "elements"]
},
}
}
}
并应对此进行验证:
{
"attributes": ["uuid", "template", "createdOn", "updatedOn"],
},
{
"attributes": ["uuid", "template", "createdOn", "updatedOn", "fields", "elements"],
}
但不是因为缺少项目“updatedOn”:
{
"attributes": ["uuid", "template", "createdOn", "fields", "elements"],
}
我已经尝试了很多都没有成功。任何人都可以给我一个提示吗?
解决方案
您可以使用contains
关键字来确保数组包含某个项目。
"allOf": [
{ "contains": { "const": "uuid" } },
{ "contains": { "const": "template" } },
...
]
推荐阅读
- mysql - 在特定条件下具有 CTE 和 GROUP BY 和 WITH ROLLUP 的邻接列表模型
- ffmpeg - 如何合并两个 hls(m3u8) 播放列表?
- javascript - 如何从 React js 向 Node js 发送请求来处理某些东西?
- python - 熊猫重采样方法
- error-handling - 处理外部库的恐慌
- node.js - TS2322:'(errors: ValidationError[]) => Error' 不能分配给类型'(errors: ValidationError[]) => any',为什么 Error 不能分配给任何
- windows - QThread::start: 创建线程失败(没有足够的内存资源来处理这个命令。)
- git - Git:当使用 fork 工作时,你是从源头还是上游变基?
- deep-learning - 使用预测分割掩码进行分水岭后处理的理想步骤是什么?
- swift - 组合:如何仅在用户输入后验证文本字段