我有一个正在接收 JSON 消息的系统,其中包含来自文件静态分析的元数据。这些字段的名称是通过扫描动态生成的,可以是任何有效的字符串,但值始终是有效的字符串。
例如
{
"filename": "hello.txt",
...
"meta": {
"some file property": "any string",
"some other file property": "another string",
...
}
}
Run Code Online (Sandbox Code Playgroud)
在收到消息之前我无法知道元中的密钥是什么,也不知道会有多少个密钥。有没有一种方法可以在 JSONSchema 中捕获,只要它们的值始终是字符串,存在什么键并不重要?
我想你正在寻找additionalProperties
使用“additionalProperties”进行验证仅适用于与“properties”中的任何名称不匹配且与“patternProperties”中的任何正则表达式不匹配的实例名称的子值。
extraProperties 的值可以是 JSON Schema,如下所示
...
"additionalProperties" : {
"type": "string"
}
...
Run Code Online (Sandbox Code Playgroud)
如果我的解释中遗漏了任何内容,请随时告诉我,或者提出任何进一步的问题。