[英]Cerberus and validating a list containing dicts
我正在嘗試驗證以下文檔。
document = {
'days': {
'Monday': [{
'address': 'my address',
'city': 'my town'
}],
'Tuesday': [{
'address': 'my address',
'city': 'my town'
}]
}
}
使用以下架構。
schema = {
'days': {
'type': 'dict',
'allowed': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
'schema': {
'type': 'list',
'schema': {
'address': {
'type': 'string'
},
'city': {
'type': 'string', 'required': True
}
}
}
}
}
v = Validator(schema)
if not v.validate(document, schema):
raise Exception("Configuration file is not valid", v.errors)
我收到以下錯誤:{days: ['must be of dict type']}
我無法弄清楚如何驗證列表中包含的字典。
你非常親近。 您可以使用valuesrules
來表示“無論鍵是什么,這都是值的規則”。 然后,在列表中,您需要一個模式,說明該列表具有 dicts,然后是元素內部的模式。 可能有一種更簡單的方法可以做到這一點,但這會傳遞您的文檔。
schema = {
'days': {
'type': 'dict',
'allowed': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
'valuesrules': {
'type': 'list',
'schema': {
'type': 'dict',
'schema': {
'address': {
'type': 'string'
},
'city': {
'type': 'string', 'required': True
}
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.