首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何基于json-schema生成模板JSON

如何基于json-schema生成模板JSON
EN

Stack Overflow用户
提问于 2019-05-06 23:06:07
回答 1查看 1.6K关注 0票数 2

我正在处理一个项目,其中我需要将JSON文件添加到存储库以生成一些测试数据。我需要记录该文件的格式,以便每个新开发人员都可以依赖该文档来生成新文件。

我正在考虑使用json-schema来记录文档,我发现了一个非常有用的在线工具,叫做jsonschema (想必你们大多数人都已经知道了)。我需要知道的是,是否有什么工具可以完成相反的任务,即:基于模式,基于该模式生成模板JSON。例如,拥有以下模式

代码语言:javascript
复制
{
  "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": "^(.*)$"
      }
    }
  }
}

运行这个工具,我应该会得到类似这样的结果

代码语言:javascript
复制
{
  "checked": false,
  "dimensions": {
    "width": 0,
    "height": 0
  },
  "id": 0,
  "name": "",
  "price": 0.0,
  "tags": [
    ""
  ]
}

即具有默认值的模板。最好是在线工具,但如果你能给我提供一些Node/Ruby实用工具,也是受欢迎的。

提前感谢您的回答/评论。诚挚的问候

EN

回答 1

Stack Overflow用户

发布于 2019-05-06 23:15:18

通过一些研究,我找到了json-schema-faker.js,它完成了对useDefaultValue选项的检查

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56007847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档