Crates.io | non-blank-string-rs |
lib.rs | non-blank-string-rs |
version | 1.0.3 |
source | src |
created_at | 2023-02-26 16:50:05.773859 |
updated_at | 2023-08-06 07:20:56.860478 |
description | Non blank string type for 'parse, don't validate' approach |
homepage | https://github.com/lebe-dev/non-blank-string-rs |
repository | https://github.com/lebe-dev/non-blank-string-rs |
max_upload_size | |
id | 795306 |
size | 7,301 |
Non blank string type for approach suggested by Alexis King - "Parse, don't validate".
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).
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.