Skip to content

Use with Garde #228

@sazzer

Description

@sazzer

I've been using Nutype for ages now with a lot of success. The fact that I can define value types that are guaranteed to be valid is a huge win for me.

However, I've just started using Garde for validation of other types in my system, and it would be nice to be able to use that for all validation. However, it turns out it doesn't work well with Nutype.

For example, this is what I started with:

#[nutype::nutype(sanitize(trim), validate(not_empty), derive(Clone, Debug, PartialEq, FromStr, AsRef))]
pub struct UserID(String);

And that works fine.

If I try to swap to Garde I get either this:

#[nutype::nutype(sanitize(trim), derive(Clone, Debug, PartialEq, FromStr, AsRef, garde::Validate))]
pub struct UserID(#[garde(length(min = 1))] String);

With this error:

error: #[nutype] does not know how to derive `garde` trait.
 --> src/modules/users/domain/user/user_id.rs:1:82
  |
1 | #[nutype::nutype(sanitize(trim), derive(Clone, Debug, PartialEq, FromStr, AsRef, garde::Validate))]

Or I get this:

#[derive(garde::Validate)]
#[nutype::nutype(sanitize(trim), derive(Clone, Debug, PartialEq, FromStr, AsRef))]
pub struct UserID(#[garde(length(min = 1))] String);

With the error:

error[E0603]: tuple struct constructor `modules::users::domain::user::user_id::__nutype_UserID__::UserID` is private
 --> src/modules/users/domain/user/user_id.rs:1:10
  |
1 | #[derive(garde::Validate)]
  |          ^^^^^^^^^^^^^^^
  |
  = note: this error originates in the derive macro `garde::Validate` (in Nightly builds, run with -Z macro-backtrace for more info)

Or I get this:

#[nutype::nutype(sanitize(trim), derive(Clone, Debug, PartialEq, FromStr, AsRef))]
#[derive(garde::Validate)]
pub struct UserID(#[garde(length(min = 1))] String);

With the error:

error: #[derive(..)] macro is not allowed to be used with #[nutype]. If you want to derive traits use `derive(..) attribute within #[nutype] macro:
       
           #[nutype(
               derive(Debug, Clone, AsRef)
           )]

is there anything that can be done to use these two together?

Cheers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions