我的目标是根据属性的存在来更改架构。
这些是我的模式:
sample = {
"type": "object",
"properties": {
"description": {"type": "string", "minLength": 1, "pattern": "^.*\w+.*$"},
"owner_id": {"type": "string"},
"privacy_setting": {"type": "string", "enum": ["private","group","account","public"]},
"iam": iam,
"timestamp": {"type": "integer", "minimum": 1000000000000, "maximum": 9999999999999},
"protocols": {"type": "array", "items": protocol},
"measurements": {"type": "array", "items": measurement}
},
"required": ["description","owner_id"],
"additionalProperties": False
}
existing_sample = {
"type": "object",
"properties": {
"id": {"type": "string"},
"protocols": {"type": "array", "items": protocol},
"measurements": {"type": "array", "items": measurement}
},
"required": ["id"],
"additionalProperties": False
}
# final schema
final = {
"type": "object",
"patternProperties": {
"^.+$": {
"if": {
"properties": {
"id": {"type": "string"}
}
},
"then": existing_sample,
"else": sample
}
},
"additionalProperties": False
}
这个想法是在“id”属性存在的情况下遵循“existing_sample”模式,并在它丢失的情况下遵循“sample”模式。
当“id”存在时,它会按预期工作,这意味着如果出现未指定的属性,它将引发异常:
不允许附加属性
但如果“id”属性丢失,它将返回异常:
'id' 是必需的属性
好像它仍然遵循“existing_sample”模式。
有任何想法吗?