Use with Garde
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
I've just come across derive_unsafe, though that still fails.
This code:
#[nutype::nutype(
sanitize(trim),
derive(Clone, Debug, PartialEq, FromStr, AsRef),
derive_unsafe(garde::Validate)
)]
pub struct UserID(#[garde(length(min = 1))] String);
Gives this error:
error: field has no validation, use `#[garde(skip)]` if this is intentional
--> src/modules/users/domain/user/user_id.rs:1:1
|
1 | / #[nutype::nutype(
2 | | sanitize(trim),
3 | | derive(Clone, Debug, PartialEq, FromStr, AsRef),
4 | | derive_unsafe(garde::Validate)
5 | | )]
| |__^
|
= note: this error originates in the attribute macro `nutype::nutype` (in Nightly builds, run with -Z macro-backtrace for more info)
@sazzer Thanks for being long term nutype user!
Yes, derive_unsafe (most likely will be soon renamed to derive_unchecked, see https://github.com/greyblake/nutype/issues/221), passed the name of trait into the inner module, where the type is defined.
However, the attributes for the field (like #[garde(length(min = 1))]) are ignored at the moment.
Yes, it can be fixed.
Thanks for reporting this!
I've created a dedicated issue for this, it falls into a more generic issue of bypassing macro attributes: https://github.com/greyblake/nutype/issues/229