我如何使用 JSONSchema 接受任何对象字符串值,而不考虑其键?

How do I use JSONSchema to accept any object string value, regardless of its key?

我有一个系统正在接收 JSON 消息,其中包含来自文件静态分析的元数据。这些字段的名称是根据扫描动态生成的,可以是任何有效的字符串,但值始终是有效的字符串。

例如

{
    "filename": "hello.txt",
    ...
    "meta": {
        "some file property": "any string",
        "some other file property": "another string",
        ...
    }
}

在收到消息之前,我无法知道meta中的键是什么,也不知道会有多少个键。有没有一种方法可以在 JSONSchema 中捕获存在什么键并不重要,只要它们的值始终是字符串?

我想你正在寻找 additionalProperties

Validation with "additionalProperties" applies only to the child values of instance names that do not match any names in "properties", and do not match any regular expression in "patternProperties".

additionalProperties 的值可以是一个 JSON Schema,就像这样

...
"additionalProperties" : {
  "type": "string"
}
...

如果我在解释中遗漏了任何内容,请随时告诉我,或者提出任何其他问题。