Expand description
§Enforcing Correctness
This crate tries to enforce correctness as at compile/testing time, so that badly set up validators do not make their way into runtime.
The Validator trait holds a method called check_consistency which checks if the inputs inside of it make sense or were caused by an error. All provided validators implement this method and perform a variety of checks on their inputs, and all custom validators can optionally implement it too. For example, the StringValidator will:
- Check if
max_lenis less thanmin_len - Check if
prefixorsuffixmatch thenot_containsrule - Check if an item is both in the lists of allowed and forbidden values
- …and many others
Whereas the EnumValidator will perform checks like ensuring that all the values in the in rule are actually defined in the target enum.
All provided validators as a whole will:
- Check the validity of CEL expressions used in them
- Check if a custom error message is defined but never used
- Check if the
construle is used along with other rules (which would be ignored)
There is a particular emphasis on checking the validity of CEL expressions because they are written as plain text and are very easy to get wrong, and they will panic at initialization if they fail to compile.
Unless specified otherwise with the skip_checks attribute, the macros will generate a test that calls this method for every validator assigned to a message/oneof, and panics on failure.
use protify::*;
let bad_validator = StringValidator::builder().min_len(10).max_len(3).build();
assert!(bad_validator.check_consistency().is_err());