Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newtype #79

Merged
merged 7 commits into from
Nov 26, 2023
Merged

Newtype #79

merged 7 commits into from
Nov 26, 2023

Conversation

jprochazk
Copy link
Owner

Closes #50

Adds the #[garde(transparent)] attribute, which may be used to create easily re-usable newtypes:

#[derive(garde::Validate)]
#[garde(transparent)]
struct Username(#[garde(length(min = 3, max = 20))] String);

#[derive(garde::Validate)]
struct User {
    #[garde(dive)]
    username: Username,
}

#[garde(transparent)] is only allowed on tuples and structs with exactly one field. It will flatten the error path by one level, which means that User.username[0] will be just User.username instead, resulting in nicer error messages:

User {
  username: Username("")
}

username: length is lower than 3

@jprochazk jprochazk merged commit 8b1955e into main Nov 26, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support NewType pattern
1 participant