non-blank-string-rs

Crates.ionon-blank-string-rs
lib.rsnon-blank-string-rs
version1.0.3
sourcesrc
created_at2023-02-26 16:50:05.773859
updated_at2023-08-06 07:20:56.860478
descriptionNon blank string type for 'parse, don't validate' approach
homepagehttps://github.com/lebe-dev/non-blank-string-rs
repositoryhttps://github.com/lebe-dev/non-blank-string-rs
max_upload_size
id795306
size7,301
Eugene Lebedev (lebe-dev)

documentation

README

Non Blank String type

Non blank string type for approach suggested by Alexis King - "Parse, don't validate".

How to use:

Add dependency:

cargo add non-blank-string-rs

Use:

let username = NonBlankString::from_str("Hellow")?;
let username: NonBlankString = "Hellow".parse()?;

// fn login(username: &str)
login(&username)

Useful for REST API Endpoints, i.e. /api/register accepts:

#[derive(Deserialize)]
struct UserRegistrationRequest {
    pub username: NonBlankString,
    ...
}

Incoming JSON with blank value in username field will raise deserialization error (Serde).

Util functions

Add to Cargo.toml:

[dev-dependencies]
non-blank-string-rs = { version = "1.0.3", features = ["utils"] }

Functions:

  • get_random_nonblank_string() - return random NonBlankString. Useful for tests.

Thanks

Commit count: 19

cargo fmt