Open NiceRath opened 11 months ago
May I suggest the following ?
sch = Schema({
'nameservers': Or(
And(str, valid_ip, Use(lambda x: [x])), # manage list conversion when string is provided
And([str], lambda x: all([valid_ip(it) for it in x])) # check that all ip are valid in the list in case of list. Use Const( lambda ... , error='nice error message') to display a usefull error message
)})
print(sch.validate({'nameservers': ['192.168.0.1']}))
print(sch.validate({'nameservers': ['192.168.0.1', '192.168.0.2']}))
print(sch.validate({'nameservers': '192.168.0.1'}))
Resulting in:
{'nameservers': ['192.168.0.1']}
{'nameservers': ['192.168.0.1', '192.168.0.2']}
{'nameservers': ['192.168.0.1']}
Would it fit your needs ?
Greetings!
We have the use-case that some values should be translated to lists if only one value is supplied.
Example:
Is there a clean way to validate the return-values that were translated by calling
Use()
?As a workaround we can, of course, create a separate
Use
function for every different use-case. Per example:Use(list_of_ips)