【发布时间】:2021-10-06 06:43:36
【问题描述】:
有没有办法让 json 模式验证器来验证 json 对象的 keys?例如:
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": { // here properties is an object containing user-defined keys that should be alphanumeric or underscore.
"values": {
"type": "object",
"patternProperties": {
"[a-zA-Z0-9_]+": {
"description": "Foo"
}
}
}
}
}
下面是一些应该和不应该通过的示例:
// Should pass:
{
"values": {
"myKey1": { "foo": 1 },
"myKey2_": "bar"
}
}
// Should fail but no error is shown saying this doesnt match schema:
{
"values": {
"m\1[]": 123
}
}
我尝试使用patternProperties,但任何与我的正则表达式不匹配的内容都会被忽略,在违规行上不会引发验证错误。
这与 patternProperties 上的文档一致:
https://json-schema.org/understanding-json-schema/reference/object.html#pattern-properties
有没有办法做我正在寻找的东西?谢谢
【问题讨论】:
标签: javascript json regex jsonschema json-schema-validator