如何解决如何定义自己的JSON Schema关键字和词汇表?
我想定义一个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","$comment": "Webstorm IntelliSense works and pk documentation is displayed","pk": false,"properties": {
"invoiceNumber": {
"$comment": "No webstorm IntelliSense and no documentation displayed","pk": true,"invoiceIssueDate": {
"type": "string"
}
}
}
任何人都具有使用元方案和自定义关键字的经验,并且可以确认我是否做对了吗?
解决方法
好像WebStorm尚不支持JSON模式的2019-09草案。我使用this online validator来检查我的元架构,并据我所见达到预期的效果。
考虑到我的问题的评论,最终结果如下:
{
"$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
}
}
}
]
}
感谢您的宝贵意见!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。