#type #string #validate #parse #blank #don-t #non

non-blank-string-rs

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

3 stable releases

1.0.3 Aug 6, 2023
1.0.2 Mar 9, 2023
1.0.1 Feb 26, 2023

#1694 in Parser implementations

30 downloads per month

MIT/Apache

8KB
133 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.3", features = ["utils"] }

Functions:

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

Thanks

Dependencies

~0.4–1.1MB
~25K SLoC