考虑下面的示例模式。
"Foo": {
"type": "object",
"properties": {
"num": {
"type": "integer",
"minimum": 1,
"maximum": 64
}
}
"Bla": {
"type": "object",
"properties": {
"base": {
"type": "object",
"allOf" : [
{"$ref": "#/definitions/Foo"},
{"num" : {"enum" : [64]} }
]
}
}
我想实现的是限制继承属性 "num "的值只能是64,而不是1和64之间的任何值。例如,我想让这个验证 。
"Bla" : {
"base" : {"num" : 64}
}
但不是这个
"Bla" : {
"base" : {"num" : 32}
}
为了完整起见,提供我之前的评论作为答案。
”num”
里面 allOf
在另 properties
你应该已经达到了你想要的目的。"const": 64
而不是 "enum": [64]
- 以提高其可读性。allOf
自从 $ref
然后允许伴有其他关键词,结果是这样的。{
"$schema": "https://json-schema.org/draft/2019-09/schema",
...
"$defs": {
"Foo": {
"type": "object",
"properties": {
"num": {
"type": "integer",
"minimum": 1,
"maximum": 64
}
}
},
"Bla": {
"type": "object",
"properties": {
"base": {
"$ref": "#/$defs/Foo",
"type": "object",
"properties": {
"num": {
"const" : 64
}
}
}
}
}
}
}