5

我想定义一个 JSON 模式词汇表,以使用与存储相关的元数据来扩展任何常规 JSON 模式。例如,我想定义一个pk关键字,它将任意 JSON 属性标记为主键。

我的元模式如下所示:

{
  "$schema": "http://json-schema.org/draft/2019-09/schema#",
  "$id": "https://myschema/meta/storage-schema",
  "$vocabulary": {
    "https://json-schema.org/draft/2019-09/vocab/core": true,
    "https://json-schema.org/draft/2019-09/vocab/applicator": true,
    "https://json-schema.org/draft/2019-09/vocab/validation": true,
    "https://json-schema.org/draft/2019-09/vocab/meta-data": true,
    "https://json-schema.org/draft/2019-09/vocab/format": false,
    "https://json-schema.org/draft/2019-09/vocab/content": true,
    "https://myschema/vocab/storage-schema": false
  },
  "$recursiveAnchor": true,

  "title": "JSON Storage-Schema",
  "allOf": [
    {"$ref": "https://json-schema.org/draft/2019-09/schema"},
    {
      "$recursiveAnchor": true,

      "title": "storage vocabulary meta-schema",

      "type": ["object", "boolean"],
      "properties": {
        "pk": {
          "description": "Marks a property as primary key",
          "type": "boolean",
          "default": false
        }
      }
    }
  ]
}

相应的 JSON 实例可能是:

{
  "$id": "https://myschema/invoice.schema.json",
  "$schema": "https://myschema/meta/storage-schema",
  "type": "object",
  "properties": {
    "invoiceNumber": {
      "pk": true,
      "type": "string"
    },
    "invoiceIssueDate": {
      "type": "string"
    }
  }
}

但是,我不确定我是否走在正确的轨道上。例如,WebStorm 的 IntelliSense 仅提供pk关键字作为顶级关键字,而不提供属性关键字:

{
  "$id": "https://myschema/invoice.schema.json",
  "$schema": "https://myschema/meta/storage-schema",
  "$comment": "Webstorm IntelliSense works and pk documentation is displayed",
  "pk": false,
  "type": "object",
  "properties": {
    "invoiceNumber": {
      "$comment": "No webstorm IntelliSense and no documentation displayed",
      "pk": true,
      "type": "string"
    },
    "invoiceIssueDate": {
      "type": "string"
    }
  }
}

有没有人有元模式和自定义关键字的经验,可以确认我是否做得对?

4

1 回答 1

3

看起来 WebStorm 还不支持 JSON 模式的草案 2019-09。我使用这个在线验证器来检查我的元模式,据我所知,它符合我的预期。

考虑到对我的问题的评论,最终结果如下所示:

{
  "$schema": "http://json-schema.org/draft/2019-09/schema#",
  "$id": "https://myschema/meta/storage-schema",
  "$vocabulary": {
    "https://json-schema.org/draft/2019-09/vocab/core": true,
    "https://json-schema.org/draft/2019-09/vocab/applicator": true,
    "https://json-schema.org/draft/2019-09/vocab/validation": true,
    "https://json-schema.org/draft/2019-09/vocab/meta-data": true,
    "https://json-schema.org/draft/2019-09/vocab/format": false,
    "https://json-schema.org/draft/2019-09/vocab/content": true,
    "https://myschema/vocab/storage-schema": false
  },
  "$recursiveAnchor": true,

  "title": "JSON Storage-Schema",
  "allOf": [
    {"$ref": "https://json-schema.org/draft/2019-09/schema"},
    {
      "title": "storage vocabulary meta-schema",

      "type": ["object", "boolean"],
      "properties": {
        "pk": {
          "description": "Marks a property as primary key",
          "type": "boolean",
          "default": false
        }
      }
    }
  ]
}

谢谢你的有用评论!

于 2020-10-01T07:22:40.843 回答