一种可能的解决方案是使用自定义验证器。
这是一个自定义验证器的完整工作示例,它检查任意映射的所有值是否都是单数类型的数组。
import colander
def values_are_singularly_typed_arrays(node, mapping):
for val in mapping.values():
if not isinstance(val, list):
raise colander.Invalid(node, "one or more value(s) is not a list")
if not len(set(map(type, val))) == 1:
raise colander.Invalid(node, "one or more value(s) is a list with mixed types")
class MySchema(colander.MappingSchema):
data = colander.SchemaNode(
colander.Mapping(unknown='preserve'),
validator=values_are_singularly_typed_arrays
)
def main():
valid_data = {
'data' : {
'numbers' : [1,2,3],
'reals' : [1.2,3.4,5.6],
}
}
not_list = {
'data' : {
'numbers' : [1,2,3],
'error_here' : 123
}
}
mixed_type = {
'data' : {
'numbers' : [1,2,3],
'error_here' : [123, 'for the watch']
}
}
schema = MySchema()
schema.deserialize(valid_data)
try:
schema.deserialize(not_list)
except colander.Invalid as e:
print(e.asdict())
try:
schema.deserialize(mixed_type)
except colander.Invalid as e:
print(e.asdict())
if __name__ == '__main__':
main()