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 = from_str?;
let username: NonBlankString = "Hellow".parse?;
// fn login(username: &str)
login
Useful for REST API Endpoints, i.e. /api/register
accepts:
Incoming JSON with blank value in username
field will raise deserialization error (Serde).
Util functions
Add to Cargo.toml
:
[]
= { = "1.0.4", = ["utils"] }
Functions:
get_random_nonblank_string()
- return randomNonBlankString
. Useful for tests.
Thanks
- Alexis King, article - Parse, don't validate
- Justin Wernick, article - The Newtype Pattern In Rust