#rest #type #string #dont #validate #parse #blank

non-blank-string-rs

Non blank string type for 'parse, don't validate' approach

4 stable releases

new 1.0.4 Dec 21, 2024
1.0.3 Aug 6, 2023
1.0.2 Mar 9, 2023
1.0.1 Feb 26, 2023

#2750 in Parser implementations

MIT/Apache

8KB
137 lines

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.4", features = ["utils"] }

Functions:

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

Thanks

Dependencies

~0.3–1MB
~25K SLoC