首页 > 解决方案 > 如何生成 JSON 模式?

问题描述

我想从 json 对象生成模式。

 var GenerateSchema = require('generate-schema')
 var schema = GenerateSchema.json(request.body);

请求正文

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          102,
          0.5
        ]
      },
      "properties": {
        "prop0": "value0"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            102,
            0
          ],
          [
            103,
            1
          ],
          [
            104,
            0
          ],
          [
            105,
            1
          ]
        ]
      },
      "properties": {
        "prop0": "value0",
        "prop1": 0
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              100,
              0
            ],
            [
              101,
              0
            ],
            [
              101,
              1
            ],
            [
              100,
              1
            ],
            [
              100,
              0
            ]
          ]
        ]
      },
      "properties": {
        "prop0": "value0",
        "prop1": {
          "this": "that"
        }
      }
    }
  ]
}

从 request.body 生成的模式

{
  "$id": "http://json-schema.org/draft-04/schema#",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Product",
  "type": "object",
  "properties": {
    "type": {
      "type": "string"
    },
    "features": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string"
          },
          "geometry": {
            "type": "object",
            "properties": {
              "type": {
                "type": "string"
              },
              "coordinates": {
                "type": "array",
                "items": {
                  "oneOf": [
                    {
                      "type": "number"
                    },
                    {
                      "type": "number"
                    },
                    {
                      "type": "number"
                    },
                    {
                      "type": "number"
                    },
                    {
                      "type": "number"
                    }
                  ],
                  "type": "array"
                }
              }
            }
          },
          "properties": {
            "type": "object",
            "properties": {
              "prop0": {
                "type": "string"
              },
              "prop1": {
                "type": "object",
                "properties": {
                  "this": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "required": [
          "type",
          "geometry",
          "properties"
        ]
      }
    }
  }
}

使用 Ajv 进行模式验证

[
  {
    keyword: 'type',
    dataPath: '.features[0].geometry.coordinates[0]',
    schemaPath: '#/properties/features/items/properties/geometry/properties/coordinates/items/type',
    params: { type: 'array' },
    message: 'should be array'
  }
]

为什么 Ajv 检测到问题?

标签: node.jsajv

解决方案


我用 QuickType 库找到了答案


推荐阅读