您可以"additionalProperties": False
在对象架构中使用该属性来只允许一组精确的键。
首先,让我们拥有有效的模式结构:
type_one = {
"type": "object",
"additionalProperties": False,
"properties": {
"one": {"type": "string"}
}
}
type_two = {
"type": "object",
"additionalProperties": False,
"properties": {
"one": {"type": "string"},
"two": {"type": "string"}
}
}
general_type = {
"type": [type_one, type_two]
}
注意:您问题的架构是"one": "string"
,应该是"one": {"type": "string"}
这是我们的输入数据:
data_one = {
"one": "blablabla"
}
data_two = {
"one": "blablabla",
"two": "blablabla"
}
这是验证:
import validictory
# additional property 'two' not defined by 'properties' are not allowed
validictory.validate(data_two, type_one)
# Required field 'two' is missing
validictory.validate(data_one, type_two)
# All valid
validictory.validate(data_one, type_one)
validictory.validate(data_two, type_two)
validictory.validate(data_one, general_type)
validictory.validate(data_two, general_type)
我希望这有帮助。