问题描述
我需要验证一个在根级别上构造为字典(键值列表)的JSON文件。
该JSON文件最初是5年前的一次性文件,但是不幸的是,整个Web应用程序现在都基于该JSON文件构建,此时更改结构将是一项不平凡的工作。
结构如下:
{
"jetpack": {
"name": "jetpack","title": "Jetpack","description": "","currency": "eur","startDate": "2020-08-10","goal": 100,...
},"chile": {
"name": "chile",...
}
...
代表更多相似数据。
我想出了以下模式,但事实证明,它使用https://github.com/ajv-validator/ajv-cli版本3.2.1根本没有不验证JSON文件。结果始终是有效的JSON,即使缺少"properties"
。
{
"$schema": "http://json-schema.org/draft-07/schema#","title": "Campaigns","type": "object","properties": {
"campaignType": { "#ref": "#/deFinitions/campaign" }
},"deFinitions": {
"campaign": {
"type": "object","required": [ "endDate","goal","byline" ],"properties": {
"endDate": {
"type": "string","format": "date","description": "E.g. 2020-07-26"
},"goal": {
"type": "number","exclusiveMinimum": 0
},"byline": {
"type": "string"
}
}
}
}
}
我在SO或http://json-schema.org/learn/上找到的所有示例都没有一个在根目录下的字典。
编辑
问题/解答在这里:Dictionary-like JSON schema。
基本上,使用deFinitions
,#ref
和properties
是错误的。答案是仅使用additionalProperties
。
{
"$schema": "http://json-schema.org/draft-07/schema#","description": "Static data for all of our campaigns. Is also used to seed our DB.","additionalProperties": {
"type": "object","properties": {
"endDate": {
"type": "string","description": "E.g. 2020-07-26"
},"goal": {
"type": "number","exclusiveMinimum": 0
},"byline": {
"type": "string"
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)