A Rust validation library
- Basic usage example
- Validation rules
- Length modes
- Inner type validation
- Newtypes
- Handling Option
- Custom validation
- Context/Self access
- Implementing rules
- Implementing
Validate
- Rule adapters
- Integration with web frameworks
- Feature flags
- Why
garde
?
To get started, install garde
:
And attach the Validate
derive to your type. garde
will generate an implementation of the Validate
trait for you,
allowing you to call the validate
method.
Here’s what that looks like in full:
,
password: "not_a_very_good_password",
};
if let Err(e) = user.validate() {
println!("invalid user: {e}");
}
Garde can also validate enums:
use garde::{Validate, Valid}; #[derive(Validate)] enum Data { Struct { #[garde(range(min=-10, max=10))] field: i32, }, Tuple( #[garde(ascii)] String ), } let data = Data::Struct { field: 100 }; if let Err(e) = data.validate() { println!("invalid data: {e}"); }
name | format | validation | feature flag |
---|---|---|---|
required | #[garde(required)] |
is value set | – |
ascii | #[garde(ascii)] |
only contains ASCII | – |
alphanumeric | #[garde(alphanumeric)] |
only letters and digits | – |
#[garde(email)] |
an email according to the HTML5 spec1 | email |
|
url | #[garde(url)] |
a URL | url |
ip | #[garde(ip)] |
an IP address (either IPv4 or IPv6) | – |
ipv4 | #[garde(ipv4)] |
an IPv4 address | – |
ipv6 | #[garde(ipv6)] |
an IPv6 address | – |
credit card | #[garde(credit_card)] |
a credit card number | credit-card |
phone number | #[garde(phone_number)] |
a phone number | phone-number |
length | #[garde(length( |
a container with length in min..=max or equal |
– |
matches | #[garde(matches( |
a field matches another field | – |
range | #[garde(range(min= |
a number in the range min..=max or equal |
– |
contains | #[garde(contains( |
a string-like value containing a substring | – |
prefix | #[garde(prefix( |
a string-like value prefixed by some string | – |
suffix | #[garde(suffix( |
a string-like value suffixed by some string | – |
pattern | #[garde(pattern(" |
a string-like value matching some regular expression | regex |
pattern | #[garde(pattern( |
a string-like value matched by some Matcher | – |
dive | #[garde(dive)] |
nested validation, calls validate on the value |
– |
skip | #[garde(skip)] |
skip validation | – |
custom | #[garde(custom( |
a custom validator | – |
Additional notes:
required
is only available forOption
fields.dive
accepts an optional context:#[garde(dive(self.other_field))]
- The
argument forlength
is explained here - For
length
andrange
:- If
equal
is defined,min
andmax
must be omitted. - Assuming
equal
is omitted, eithermin
ormax
may be omitted, but not both. min
andmax
use an inclusive upper bound (min..=max
). Settingmin == max
is equivalent to usingequal
.
- If
- For
contains
,prefix
, andsuffix
, the pattern must be a string literal, because thePattern
API is currently unstable. - Garde does not enable the default features of the
regex
crate – if you need extra regex features (e.g. Unicode) or better performance, add a dependency onregex = "1"
to yourCargo.toml
.
If most of the fields on your struct are annotated with #[garde(skip)]
, you may use #[garde(allow_unvalidated)]
instead:
Leave a comment