我想定义一个 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"
}
}
}
有没有人有元模式和自定义关键字的经验,可以确认我是否做得对?