我想使用文件名作为 json 文件中属性的值,并对它们进行智能感知。
使用 json-schema 中的博客文章示例。
{
"title": "New Blog Post",
"content": "This is the content of the blog post...",
"publishedDate": "2023-08-25T15:00:00Z",
"author": {
"username": "authoruser",
"email": "[email protected]"
},
"tags": ["Technology", "Programming"]
}
我希望标签的允许值是标签文件夹中的文件名,因此在这种情况下,我将拥有
tags/Technology.json
和 tags/Programming.json
并可能显示文件中的一些信息(例如描述)。例如,Technology.json
可能看起来像这样。
{
"name": "Technology (Modern)",
"description": "Posts about technology created after the year 2000"
}
根据我的发现,我似乎必须从这些 json 文件生成一个架构文件,例如
{
"type": "array",
"items": {
"anyOf": [
{
"const": "Technology",
"description": "Posts about technology created after the year 2000"
},
{
"const": "Programming",
"description": "Posts about any kind of programming"
}
]
},
"uniqueItems": true
}
并将其用作我想要使用它的模式标签中的
$ref
。
有没有我缺少的更简单的方法可以做到这一点?我知道这已经很简单了,但我觉得我只是忽略了一些东西。
文件名可以像字典一样映射键,使用
propertyNames
和 pattern
作为键。然后使用 additionalProperties
和模式来定义所需的模式
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["tags"],
"properties": {
"tags": {
"type": "object",
"propertyNames": {"pattern": ".json$"},
"additionalProperties": {
"$ref": "#/definitions/metadata"
}
}
},
"definitions": {
"metadata": {
"type": "object",
"required": ["name", "description"],
"properties": {
"name": {"type": "string"},
"description": {"type": "string"}
}
}
}
}
{
"tags": {
"technology.json": {
"name": "Technology (Modern)",
"description": "Posts about technology created after the year 2000"
},
"programming.json": {
"name": "Technology (Modern)",
"description": "Posts about technology created after the year 2000"
},
"coding.json": {
"name": "Technology (Modern)",
"description": "Posts about technology created after the year 2000"
},
"crying.json": {
"name": "Technology (Modern)",
"description": "Posts about technology created after the year 2000"
}
}
}