Skip to content

Releases: jprochazk/garde

v0.15.0

09 Sep 11:33
Compare
Choose a tag to compare

Breaking changes

  • garde::error has been completely overhauled.
  • garde::Validate has a new validate_into method, which is the primary entrypoint for custom implementations now.
  • garde::Validate::validate is now implemented by default by calling validate_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

Full Changelog: v0.14.1...v0.15.0

v0.14.1

25 Aug 09:30
Compare
Choose a tag to compare

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

01 Aug 15:35
Compare
Choose a tag to compare

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 a Regex (such as once_cell::sync::Lazy<regex::Regex>). This change meant changing the Pattern 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

28 Jul 10:42
Compare
Choose a tag to compare

What's Changed

  • Fixed range rule on Option fields #55
  • Disable unnecessary/unused regex features by @jirutka in #54

New Contributors

Full Changelog: v0.12.0...v0.13.0

v0.12.0

27 Jul 19:39
Compare
Choose a tag to compare

What's Changed

  • removed blanket impls of AsRef<str>, replaced with garde::rules::AsStr
  • removed blanket impls of AsRef<[u8]>, replaced with garde::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 the Unvalidated newtype
  • added #[garde(required)] for validating Option 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 is None
  • 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

09 Apr 18:33
Compare
Choose a tag to compare

Fixes

  • length rule's min and max may now be equal (#38)

Full Changelog: v0.11.1...v0.11.2

v0.11.1

03 Apr 17:56
Compare
Choose a tag to compare

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 to v0.3.2+8.13.9

New Contributors

Full Changelog: v0.10.0...v0.11.1

v0.10.0

01 Apr 21:06
Compare
Choose a tag to compare

Breaking changes

  • Errors now has a new Nested variant.
  • Display now uses the output of flatten instead of only recursively printing errors.

Fixes

  • Result aliases declared in the same scope as the output of the Validate 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

Full Changelog: https://github.com/jprochazk/garde/compare/v0.9.2..v0.10.0

v0.9.2

31 Mar 21:16
Compare
Choose a tag to compare

What's Changed

New Contributors

Full Changelog: v0.9.1...v0.9.2

v0.9.1

29 Mar 19:55
Compare
Choose a tag to compare

What's Changed

  • Added byte_length rule to list of validation rules in the readme and lib.rs.

Full Changelog: https://github.com/jprochazk/garde/compare/v0.9.0..v0.9.1