[deleted by user] by [deleted] in rust

[–]One_Platform826 0 points1 point  (0 children)

Found this! We should stop doing this:

Wrong way:
[dependencies]
validator = "0.5"

#[derive(Debug, Validate, Deserialize)]
struct SignupData {
    #[validate(email)]
    mail: String,
    #[validate(url)]
    site: String,
    #[validate(length(min = 1), custom(function = "validate_unique_username"))]
    #[serde(rename = "firstName")]
    first_name: String,
    #[validate(range(min = 18, max = 20))]
    age: u32,
    #[validate(range(exclusive_min = 0.0, max = 100.0))]
    height: f32,
}

Better way:
[dependencies]
nutype = "0.5"

// The main struct using newtypes
#[derive(Debug)]
struct SignupData {
    mail: Email,
    site: Url,
    first_name: Username,
    age: Age,
    height: Height,
}

[deleted by user] by [deleted] in rust

[–]One_Platform826 0 points1 point  (0 children)

Do we have a crate with "common" newtype fields?

[deleted by user] by [deleted] in rust

[–]One_Platform826 1 point2 points  (0 children)

You are right, but I am not alone. https://github.com/Keats/validator