Inspired by the validator crate. The Sanitizer crate is a collection of methods and a macro to sanitize struct fields, leveraging the macros of rust, it follows the elegant approach by the validator crate.
rust
[dependencies]
sanitizer = { version = "0.1", features = ["derive"] }
Then to use the crate
```rust use sanitizer::prelude::*;
struct SignupData { #[sanitize(trim, lower_case)] mail: String, #[sanitize(clamp(1, 60))] age: u8, #[sanitize] user: User, }
struct User { id: u64, #[sanitize(trim, clamp(50))] name: String, }
fn main() { let instance = SignupData::new(); instance.sanitize(); } ```
Removes whitespace from ends.
Removes any character that is not a numeric.
Removes any character that is not an alphanumeric.
Converts string input to lowercase.
Converts string input to UPPERCASE.
Converts string input to camelCase.
Converts string input to snake_case.
Converts string input to SCREAMINGSNAKECASE using the Inflector crate.
Converts string input to E164 International Phone Number format. This panics if the phone number is not a valid one.
Limit an valid integer field with the given min and max.
Limit a string input length to the following number
```rust
struct First { #[sanitize(trim)] name: String, #[sanitize] info: OtherInfo, }
struct OtherInfo { #[sanitize(numeric)] id: String, #[sanitize(lower_case, trim)] email: String, }
```
The sanitize
method of First
will call the sanitizer method of OtherInfo
automatically,
if you would like to individually snaitize OtherInfo
then you can just call snaitize
on one of its instance.
dashxhq/sanitizer is licensed under the MIT License.