v0.6.0 -- Nested Validations
Nested Validations
This release adds support for nested validations, something that should have been there all along. Essentially, this allows a list of validations to contain a dictionary of yet more validations in order to accommodate writing validations for nested dictionaries. Here's an example:
validator = {
"foo": [Required, Equals(1)],
"bar": [Required, {
"baz": [Required, Equals(2)],
"qux": [Required, {
"quux": [Required, Equals(3)]
}]
}
]
}
test_case = {
"foo": 1,
"bar": {
"baz": 2,
"qux": {
"quux": 3
}
}
}
The above example says that the bar
key is required and also represents a dictionary that also has its own set of validations. For good measure, this example has yet another dictionary under the qux
key. As long as everything checks out, validate
will return the normal (True, {})
response indicating success.
In the event of failure, you get an appropriately nested error message like those produced by the If(Then()) conditional validator. Here's an example of what such an error might look like:
>>> validate(fails, test_case)
(False,
{'bar': [{'baz': ['must be equal to 3'],
'qux': [{'quux': ['must be equal to 4']}]}],
'foo': ['must be equal to 2']})
Changes to Not(validator)
Up until this release, all of the negated versions of validation errors were produced by the Not
validator. This was not a sensible long term solution. All validators are now responsible for their own negated error messages and as such now have their own not_message
attribute as an analogue of the err_message
attribute they already had. This will make it easier to extend the library's capabilities with custom, and it certainly makes the Not
validator less messy.
Generic Errors
Related to the change to negated error messages, all validators will now have generic error messages and negated error messages provided for them in the event they don't provide their own. This manifests as a simple "validation failed"
.