Skip to content

lebe-dev/non-blank-string-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Non blank string type

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages