Releases: jprochazk/garde
v0.15.0
Breaking changes
garde::error
has been completely overhauled.garde::Validate
has a newvalidate_into
method, which is the primary entrypoint for custom implementations now.garde::Validate::validate
is now implemented by default by callingvalidate_into
.
These changes should not have any significant effects on the usage of the library, if all you're doing is deriving Validate
and serializing errors using the Display
implementation on Errors
(now called Report
). If you're having trouble migrating, please open an issue.
New features
You can now easily select!
errors for specific fields on a given Report
:
#[derive(garde::Validate)]
struct Foo<'a> {
#[garde(dive)]
bar: Bar<'a>,
}
#[derive(garde::Validate)]
struct Bar<'a> {
#[garde(length(min = 1))]
value: &'a str,
}
let v = Foo { bar: Bar { value: "" } };
let report = v.validate(&()).unwrap_err();
println!("errors for `Foo.bar.value`");
for error in garde::error::select!(report, bar.value) {
println!("{error}");
}
Pull requests
- Flat error representation by @jprochazk in #67
Full Changelog: v0.14.1...v0.15.0
v0.14.1
What's Changed
#65 by @aumetra
You can now bind the context to a variable, and the length
and byte_length
rules min
and max
values may now contain arbitrary expressions. This means the following is now possible:
struct Config {
min_length: usize,
}
#[derive(garde::Validate)]
#[garde(context(Config as conf))]
struct Foo {
#[garde(length(
min = conf.min_length,
max = self.baz.len()
))]
bar: String,
#[garde(skip)]
baz: String,
}
New Contributors
Full Changelog: v0.14.0...v0.14.1
v0.14.0
New features
Thanks to @jirutka for contributing the following features.
Widening the scope of valid values for prefix
, suffix
, and contains
You can now use constants, functions, concat!
, or any other expression in prefix
, suffix
, and contains
:
const PREFIX: &str = "a";
const SUFFIX: &str = "b";
const INNER: &str = "c";
#[derive(garde::Validate)]
struct Test<'a> {
#[garde(prefix(PREFIX), contains(INNER), suffix(SUFFIX))]
v: &'a str,
}
Abstracting away regex
regex
is now an optional feature, and you can bring your own Regex
when using the pattern
rule:
use once_cell::sync::Lazy;
use regex::Regex;
static PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new(r"a|b").unwrap());
#[derive(garde::Validate)]
struct Test<'a> {
#[garde(pattern(PATTERN))]
v: &'a str,
}
Note that usage of pattern
with a string literal requires the regex
feature. It won't compile without it.
With the introduction of the Matcher
trait, you can now use other regex engines, such as regex-lite. All you need to do is wrap the Regex type in a newtype, and implement Matcher
on it.
Breaking changes
- The
pattern
rule now accepts any expression which derefs to aRegex
(such asonce_cell::sync::Lazy<regex::Regex>
). This change meant changing thePattern
trait, and introducing a new trait,Matcher
, to abstract the specific regex engine used.
Pull requests
- Allow pattern, contains, prefix and suffix accept expression by @jirutka in #56
- Abstract from Regex in pattern validator and add support for regex-lite by @jirutka in #57
Full Changelog: v0.13.0...v0.14.0
v0.13.0
v0.12.0
What's Changed
- removed blanket impls of
AsRef<str>
, replaced withgarde::rules::AsStr
- removed blanket impls of
AsRef<[u8]>
, replaced withgarde::rules::byte_length::AsByteSlice
- changed layout of
Errors
to support inner type validation - container-level attributes are now checked for duplicates
- added
#[serde(transparent)]
for theUnvalidated
newtype - added
#[garde(required)]
for validatingOption
fields - added
#[garde(allow_unvalidated)]
- implemented
inner
modifier
Handling Option
Every rule works on Option<T>
fields. The field will only be validated if it is Some
. If you additionally want to validate that the Option<T>
field is Some
, use the required
rule:
#[derive(garde::Validate)]
struct Test {
#[garde(required, ascii, length(min = 1))]
value: Option<String>,
}
The above type would fail validation if:
value
isNone
- the inner
value
is empty - the inner
value
contains non-ASCII characters
Inner type validation
If you need to validate the "inner" type of a container, such as the String
in Vec<String>
, then use the inner
modifier:
#[derive(garde::Validate)]
struct Test {
#[garde(
length(min = 1),
inner(ascii, length(min = 1)), // wrap the rule in `inner`
)]
items: Vec<String>,
}
The above type would fail validation if:
- the
Vec
is empty - any of the inner
String
elements is empty - any of the inner
String
elements contains non-ASCII characters
#[garde(allow_unvalidated)]
If most of the fields on your struct are annotated with #[garde(skip)]
, you may use #[garde(allow_unvalidated)]
instead:
#[derive(garde::Validate)]
struct Foo<'a> {
#[garde(length(min = 1))]
a: &'a str,
#[garde(skip)]
b: &'a str, // this field will not be validated
}
#[derive(garde::Validate)]
#[garde(allow_unvalidated)]
struct Bar<'a> {
#[garde(length(min = 1))]
a: &'a str,
b: &'a str, // this field will not be validated
// note the lack of `#[garde(skip)]`
}
Full Changelog: v0.11.2...v0.12.0
v0.11.2
v0.11.1
Highlights
garde
can now be easily integrated into axum
applications using axum_garde
. Huge thanks to @Altair-Bueno who contributed this!
Breaking changes
Validate
is now implemented for()
- Updated
phonenumber
tov0.3.2+8.13.9
New Contributors
- @Altair-Bueno made their first contribution in #30
- @Muscraft made their first contribution in #36
Full Changelog: v0.10.0...v0.11.1
v0.10.0
Breaking changes
Errors
now has a newNested
variant.Display
now uses the output offlatten
instead of only recursively printing errors.
Fixes
Result
aliases declared in the same scope as the output of theValidate
derive macro will no longer cause conflicts. (#24)
New features
dive
now works together with other rules:
#[derive(garde::Validate)]
struct Item {
#[garde(ascii)]
field: String,
}
#[derive(garde::Validate)]
struct Example {
#[garde(length(min=1), dive)]
items: Vec<Item>,
}
- each field may now have any number of
custom
rules:
#[derive(garde::Validate)]
struct Example {
#[garde(custom(a), custom(b), custom(c))]
items: String,
}
What's Changed
- Fix hygiene of
Result
in derivedValidate
impl by @jprochazk in #24 - Add CI by @jprochazk in #25
- Derive macro refactor by @jprochazk in #28
Full Changelog: https://github.com/jprochazk/garde/compare/v0.9.2..v0.10.0
v0.9.2
What's Changed
- Fix hygiene of
Result
in derivedValidate
impl by @jprochazk in #24 - Add CI by @jprochazk in #25
New Contributors
Full Changelog: v0.9.1...v0.9.2
v0.9.1
What's Changed
- Added
byte_length
rule to list of validation rules in the readme andlib.rs
.
Full Changelog: https://github.com/jprochazk/garde/compare/v0.9.0..v0.9.1