我们使用 JSON 来存储一些配置设置。例如:
{
"source1": {
"name": "source1",
"standalone": false
},
"source2": {
"name": "source2",
"standalone": true
},
"source3": {
"name": "source3",
"standalone": true
}
}
如您所见,源名称是可变的,并且为了方便在属性下的对象内部重复name
。
我们目前正在使用 JSON 模式验证这一点,如下所示:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"patternProperties": {
"^\\w[-\\w_]*$": { "$ref": "#/definitions/source" }
},
"additionalProperties": false,
"definitions": {
"source": {
"type": "object",
"properties": {
"name": { "type": "string" },
"standalone": { "type": "boolean" }
},
"required": ["name", "standalone"],
"additionalProperties": false
}
}
}
有没有办法要求属性名称与使用 JSON 模式的值匹配?换句话说,有没有办法确保以下示例无法验证?
{
"a": {
"name": "b",
"standalone": false
}
}