我正在处理一个项目,其中我需要将JSON文件添加到存储库以生成一些测试数据。我需要记录该文件的格式,以便每个新开发人员都可以依赖该文档来生成新文件。
我正在考虑使用json-schema来记录文档,我发现了一个非常有用的在线工具,叫做jsonschema (想必你们大多数人都已经知道了)。我需要知道的是,是否有什么工具可以完成相反的任务,即:基于模式,基于该模式生成模板JSON。例如,拥有以下模式
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
"checked",
"dimensions",
"id",
"name",
"price",
"tags"
],
"properties": {
"checked": {
"$id": "#/properties/checked",
"type": "boolean",
"title": "The Checked Schema",
"default": false,
"examples": [
false
]
},
"dimensions": {
"$id": "#/properties/dimensions",
"type": "object",
"title": "The Dimensions Schema",
"required": [
"width",
"height"
],
"properties": {
"width": {
"$id": "#/properties/dimensions/properties/width",
"type": "integer",
"title": "The Width Schema",
"default": 0,
"examples": [
5
]
},
"height": {
"$id": "#/properties/dimensions/properties/height",
"type": "integer",
"title": "The Height Schema",
"default": 0,
"examples": [
10
]
}
}
},
"id": {
"$id": "#/properties/id",
"type": "integer",
"title": "The Id Schema",
"default": 0,
"examples": [
1
]
},
"name": {
"$id": "#/properties/name",
"type": "string",
"title": "The Name Schema",
"default": "",
"examples": [
"A green door"
],
"pattern": "^(.*)$"
},
"price": {
"$id": "#/properties/price",
"type": "number",
"title": "The Price Schema",
"default": 0.0,
"examples": [
12.5
]
},
"tags": {
"$id": "#/properties/tags",
"type": "array",
"title": "The Tags Schema",
"items": {
"$id": "#/properties/tags/items",
"type": "string",
"title": "The Items Schema",
"default": "",
"examples": [
"home",
"green"
],
"pattern": "^(.*)$"
}
}
}
}运行这个工具,我应该会得到类似这样的结果
{
"checked": false,
"dimensions": {
"width": 0,
"height": 0
},
"id": 0,
"name": "",
"price": 0.0,
"tags": [
""
]
}即具有默认值的模板。最好是在线工具,但如果你能给我提供一些Node/Ruby实用工具,也是受欢迎的。
提前感谢您的回答/评论。诚挚的问候
发布于 2019-05-06 23:15:18
通过一些研究,我找到了json-schema-faker.js,它完成了对useDefaultValue选项的检查
https://stackoverflow.com/questions/56007847
复制相似问题