validator
Macros 1.1 custom derive to simplify struct validation inspired by marshmallow and Django validators.
The minimum supported version is Rust 1.42.
Installation:
[]
= { = "0.18", = ["derive"] }
A short example:
use Deserialize;
// A trait that the Validate derive will impl
use ;
match signup_data.validate ;
A validation on an Option<_>
field will be executed on the contained type if the option is Some
. The validate()
method returns a Result<(), ValidationErrors>
. In the case of an invalid result, the ValidationErrors
instance includes
a map of errors keyed against the struct's field names. Errors may be represented in three ways, as described by the
ValidationErrorsKind
enum:
In the simple example above, any errors would be of the Field(Vec<ValidationError>)
type, where a single
ValidationError
has the following structure:
The value of the field will automatically be added to the params with a key of value
.
The other two ValidationErrorsKind
types represent errors discovered in nested (vectors of) structs, as described in
this example:
use Deserialize;
// A trait that the Validate derive will impl
use Validate;
match signup_data.validate ;
Here, the ContactDetails
and Preference
structs are nested within the parent SignupData
struct. Because
these child types also derive Validate
, the fields where they appear can be tagged for inclusion in the parent
struct's validation method.
Any errors found in a single nested struct (the contact_details
field in this example) would be returned as a
Struct(Box<ValidationErrors>)
type in the parent's ValidationErrors
result.
Any errors found in a vector of nested structs (the preferences
field in this example) would be returned as a
List(BTreeMap<usize, Box<ValidationErrors>>)
type in the parent's ValidationErrors
result, where the map is keyed on
the index of invalid vector entries.
Usage
You will need to import the Validate
trait.
The validator
crate can also be used without the custom derive as it exposes all the
validation functions and types.
Validators
The crate comes with some built-in validators and you can have several validators for a given field.
Tests whether the String is a valid email according to the HTML5 regex, which means it will mark
some esoteric emails as invalid that won't be valid in a email
input as well.
This validator doesn't take any arguments: #[validate(email)]
.
url
Tests whether the String is a valid URL.
This validator doesn't take any arguments: #[validate(url)]
;
length
Tests whether a String or a Vec match the length requirement given. length
has 3 integer arguments:
- min
- max
- equal
Using equal
excludes the min
or max
and will result in a compilation error if they are found.
At least one argument is required with a maximum of 2 (having min
and max
at the same time).
Examples:
const MIN_CONST: u64 = 1;
const MAX_CONST: u64 = 10;
range
Tests whether a number is in the given range. range
takes 1 or 2 arguments, and they can be normal (min
and max
) or exclusive (exclusive_min
, exclusive_max
, unreachable limits).
These can be a number or a value path.
Examples:
const MAX_CONSTANT: i32 = 10;
const MIN_CONSTANT: i32 = 0;
must_match
Tests whether the 2 fields are equal. must_match
takes 1 string argument. It will error if the field
mentioned is missing or has a different type than the field the attribute is on.
Examples:
contains
Tests whether the string contains the substring given or if a key is present in a hashmap. contains
takes
1 string argument.
Examples:
does_not_contain
Pretty much the opposite of contains, provided just for ease-of-use. Tests whether a container does not contain
the substring given if it's a string or if a key is NOT present in a hashmap. does_not_contain
takes 1 string argument.
Examples:
regex
Tests whether the string matches the regex given. regex
takes
1 string argument: the path to a static Regex instance.
Examples:
use Lazy;
static RE_TWO_CHARS: = new;
credit_card
Test whether the string is a valid credit card number.
Examples:
custom
Calls one of your functions to perform a custom validation. The field reference will be given as a parameter to the function,
which should return a Result<(), ValidationError>
.
Examples:
You can also do your own validation by parsing the arguments from the validation function by setting context
for struct. Applying custom validation using the use_context
argument is accomplished by setting the use_context
parameter. Defining the context
parameter will implement the ValidateArgs
trait with the corresponding function types like this:
use ;
;
let test_struct: TestStruct = ;
let test_context: TestContext = ;
test_struct.validate_with_args.is_ok;
It is also possible to pass references by using the lifetime 'v_a
note that this lifetime should only be used for the function parameters like this:
// vvvv This is the lifetime for references
let mut database: Database = ;
let test_struct: TestStruct = ;
test_struct.validate_with_args.is_ok;
Custom validation with arguments doesn't work on nested validation. See validator_derive_tests/tests/custom.rs
and validator_derive_tests/tests/custom_args.rs
for more examples.
nested
Performs validation on a field with a type that also implements the Validate trait (or a vector of such types).
Examples:
non_control_character
Tests whether the String has any utf-8 control characters, fails validation if it does.
To use this validator, you must enable the unic
feature for the validator
crate.
This validator doesn't take any arguments: #[validate(non_control_character)]
;
required
Tests whether the Option<T>
field is Some
;
Struct level validation
Often, some error validation can only be applied when looking at the full struct, here's how it works here:
The function mentioned should return a Result<(), ValidationError>
and will be called after validation is done for all fields.
The skip_on_field_errors
defaults to true
if not present and will ensure that the function is not called
if an error happened while validating the struct fields.
Any error on the struct level validation will appear in the key __all__
of the hashmap of errors.
Message and code
Each validator can take 2 optional arguments in addition to their own arguments:
message
: a message to go with the error, for example if you want to do i18ncode
: each validator has a default error code (for example theregex
validator code isregex
) but it can be overridden if necessary, mainly needed for thecustom
validator
Note that these arguments can't be applied to nested validation calls with #[validate]
.
For example, the following attributes all work:
// code attribute
// message attribute
// both attributes