Skip to content

Releases: greyblake/nutype

Nutype 0.4.2

10 Apr 06:14
Compare
Choose a tag to compare

Changes

  • Support no_std ( the dependency needs to be declared as nutype = { default-features = false } )
  • Support integration with arbitrary crate (see arbitrary feature).
    • Support Arbitrary for integer types
    • Support Arbitrary for float types
    • Support Arbitrary for string inner types
    • Support Arbitrary for any inner types
  • Possibility to specify boundaries (greater, greater_or_equal, less, less_or_equal, len_char_min, len_char_max) with expressions or named constants.
  • Add #[inline] attribute to trivial functions
  • Improve error messages

Highlights

Here is an example of nutype and arbitrary playing together:

use nutype::nutype;
use arbtest::arbtest;
use arbitrary::Arbitrary;

#[nutype(
    derive(Arbitrary, AsRef),
    sanitize(trim),
    validate(
        not_empty,
        len_char_max = 100,
    ),
)]
pub struct Title(String);

fn main() {
    arbtest(|u| {
        // Generate an arbitrary valid Title
        let title = Title::arbitrary(u)?;

        // The inner string is guaranteed to be non-empty
        assert!(!title.as_ref().is_empty());

        // The inner string is guaranteed not to exceed 100 characters
        assert!(title.as_ref().chars().count() <= 100);
        Ok(())
    });
}

As you can see the derived implementation of Arbitrary respects the validation rules.
In the similar way Arbitrary can be derived for integer and float based types.

Links

Nutype 0.4.0

21 Nov 20:24
Compare
Choose a tag to compare

Acknowledgements

A heartfelt thanks to Daniyil Glushko for his invaluable assistance and exceptional work on this release. Daniyil, located in Zaporizhzhia, Ukraine, is a proficient Rust developer open to remote opportunities. I highly recommend reaching out to him for Rust development roles.

Changes

  • [FEATURE] Support of arbitrary inner types with custom sanitizers and validators.
  • [FEATURE] Add numeric validator greater
  • [FEATURE] Add numeric validator less
  • [BREAKING] Removal of asterisk derive
  • [BREAKING] Use commas to separate high level attributes
  • [BREAKING] Traits are derived with #[nutype(derive(Debug))]. The regular #[derive(Debug)] syntax is not supported anymore.
  • [BREAKING] Validator with has been renamed to predicate to reflect the boolean nature of its range
  • [BREAKING] String validator min_len has been renamed to len_char_min to reflect that is based on UTF8 chars.
  • [BREAKING] String validator max_len has been renamed to len_char_max to reflect that is based on UTF8 chars.
  • [BREAKING] Rename numeric validator max to less_or_equal
  • [BREAKING] Rename numeric validator min to greater_or_equal
  • [BREAKING] Rename error variants to follow the following formula: <ValidationRule>Violated. This implies the following renames:
    • TooShort -> LenCharMinViolated
    • TooLong -> LenCharMaxViolated
    • Empty -> NotEmptyViolated
    • RegexMismatch -> RegexViolated
    • Invalid -> PredicateViolated
    • TooBig -> LessOrEqualViolated
    • TooSmall -> GreaterOrEqualViolated
    • NotFinite -> FiniteViolated
  • Better error messages: in case of unknown attribute, validator or sanitizer the possible values are listed.
  • [FIX] Make derived Deserialize work with RON format

Feature highlights

Arbitrary inner type

Previously #[nutype] worked only with String, integers and floats.
Now it's possible to use it with any arbitrary type (e.g. Vec<String>):

#[nutype(
    validate(predicate = |friends| !friends.is_empty() ),
)]
pub struct Frieds(Vec<String>);

New numeric validators

Instead of former min and max integers and floats can now be validated with:

  • greater_or_equal - Inclusive lower bound
  • greater - Exclusive lower bound
  • less_or_equal - Inclusive upper bound
  • less - Exclusive upper bound

Example:

#[nutype(
    validate(
        greater_or_equal = 0,
        less_or_equal = 59,
    ),
)]
pub struct Minute(u8);

Derive

Deriving of traits now has to be done explicitly with #[nutype(derive(...))] instead of #[derive(...)]:

Example:

#[nutype(
    validate(with = |n| n % 2 == 1),
    derive(Debug, Clone, Copy)
)]
pub struct OddNumber(u64);

This makes it clear, that deriving is fully handled by #[nutype] and prevents a potential confusion.

Links

Nutype 0.3.1

30 Jun 05:04
Compare
Choose a tag to compare

Changes

  • Add ability to derive Deref on String, integer and float based types.

Examples

use nutype::nutype;

#[nutype]
#[derive(Deref)]
struct Email(String);

let email = Email::new("foo@bar.com")

// Call .len() which is delegated to the inner String due to the deref-coercion mechanism
assert_eq!(email.len(), 11);

Nutype 0.3.0

25 Jun 09:55
Compare
Choose a tag to compare

Changes

  • [BREAKING] min_len and max_len validators run against number of characters in a string (val.chars().count()), not number of bytes (val.len()).
  • Add finite validation for float types which checks against NaN and infinity.
  • Support deriving of Default
  • Support deriving of Eq and Ord on float types (if finite validation is present)
  • Support deriving of TryFrom for types without validation (in this case Error type is std::convert::Infallible)

Feature Highlights:

  • Deriving Eq and Ord on f32 and f64 types: The new release addresses the limitation in Rust where f32 and f64 types cannot implement the Ord and Eq traits due to the presence of NaN values. Nutype introduces finite validation, which allows the correct implementation of Eq and Ord traits for float-based newtypes.
use nutype::nutype;

#[nutype(validate(finite))]
#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct Distance(f64);
  • Deriving Default: Nutype 0.3.0 introduces support for deriving the Default trait. This allows users to derive the Default trait for their custom types with validation logic. Nutype also generates a unit test to ensure the validity of the default value.
use nutype::nutype;

#[nutype(
    validate(with = |n| n % 2 == 1)
    default = 1
)]
#[derive(Debug, Default)]
pub struct OddNumber(u64);

Please note that dynamic validation makes it impossible to guarantee the validity of the default value at compile time. Panics will occur if an invalid default value is obtained.

For more details, refer to the Nutype documentation.

Blog post: Nutype 0.3.0 released

Nutype 0.2.0

13 Apr 19:30
98aa30b
Compare
Choose a tag to compare

Changes

  • [BREAKING] Rename string validator present -> not_empty. Rename error variant Missing -> Empty.
  • [BREAKING] Rename feature serde1 to serde.
  • Support string validation with regex (requires regex feature).
  • Introduce new_unchecked feature flag, that allows to bypass sanitization and validation.
  • Support derive of JsonSchema of schemars crate (requires schemars08 feature).

Blog post

Please consider reading Nutype 0.2.0 is out! blog post, which covers in detail the regex and new_unchecked features.