如何使 json 架构中的 patternProperties 字段成为强制性字段?
How to make a patternProperties field in a json schema mandatory?
这应该很快就能回答是或否,但我无法在 SO 或其他地方找到答案。我想制作一组模式,其中包含一个植根于 entity
模式的良好分层依赖模式。我想要的模式是
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "entity",
"type": "object",
"patternProperties": {
"^.+Id$": {
"type": "string"
}
},
"required": [
"^.+Id$"
]
}
基本上,我希望每个实体,例如一个人,都需要一个名为 somethingId
的字段(对于人,可能是 personId
)。但是,以这种方式使用 "required" 似乎强制一个名为 "^.+Id$"
的实际字段,而不是对象必须有一个与模式匹配的字段。有没有办法在这里做我想做的事?
提前致谢。
正如您在评论中提到的,您不能在必需的子句中强制执行动态 属性 键。
但是如果允许嵌套定义,则可以按如下方式对标识符建模:
{
"properties" : {
"identifier" : {
"additionalProperties" : false,
"minProperties" : 1,
"maxProperties" : 1,
"patternProperties" : "^.+Id$ "
}
},
"required": ["identifier"]
}
这样您的标识符必须具有唯一的 属性 以及验证您的正则表达式的密钥。
无需更改此架构的数据结构即可实现:
{
"patternProperties" : {
"^.+Id$": {
"type": "string"
}
},
"minProperties": 1,
"additionalProperties": false
}
这应该很快就能回答是或否,但我无法在 SO 或其他地方找到答案。我想制作一组模式,其中包含一个植根于 entity
模式的良好分层依赖模式。我想要的模式是
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "entity",
"type": "object",
"patternProperties": {
"^.+Id$": {
"type": "string"
}
},
"required": [
"^.+Id$"
]
}
基本上,我希望每个实体,例如一个人,都需要一个名为 somethingId
的字段(对于人,可能是 personId
)。但是,以这种方式使用 "required" 似乎强制一个名为 "^.+Id$"
的实际字段,而不是对象必须有一个与模式匹配的字段。有没有办法在这里做我想做的事?
提前致谢。
正如您在评论中提到的,您不能在必需的子句中强制执行动态 属性 键。
但是如果允许嵌套定义,则可以按如下方式对标识符建模:
{
"properties" : {
"identifier" : {
"additionalProperties" : false,
"minProperties" : 1,
"maxProperties" : 1,
"patternProperties" : "^.+Id$ "
}
},
"required": ["identifier"]
}
这样您的标识符必须具有唯一的 属性 以及验证您的正则表达式的密钥。
无需更改此架构的数据结构即可实现:
{
"patternProperties" : {
"^.+Id$": {
"type": "string"
}
},
"minProperties": 1,
"additionalProperties": false
}