#trim #case #struct-fields #validate #macro-derive #e164

sanitizer

A collection of methods and macros to sanitize struct fields

5 releases

0.1.6 Nov 4, 2021
0.1.5 May 20, 2021
0.1.4 May 19, 2021
0.1.2 Mar 7, 2021
0.1.0 Dec 21, 2020

#648 in Text processing

Download history 1488/week @ 2023-12-14 1403/week @ 2023-12-21 1195/week @ 2023-12-28 3383/week @ 2024-01-04 1752/week @ 2024-01-11 2668/week @ 2024-01-18 2367/week @ 2024-01-25 3189/week @ 2024-02-01 1828/week @ 2024-02-08 3366/week @ 2024-02-15 3642/week @ 2024-02-22 2920/week @ 2024-02-29 3370/week @ 2024-03-07 3679/week @ 2024-03-14 3175/week @ 2024-03-21 2780/week @ 2024-03-28

14,098 downloads per month

MIT license

15KB
237 lines

sanitizer

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.

Overview

[dependencies]
sanitizer = { version = "0.1", features = ["derive"] }

Then to use the crate

use sanitizer::prelude::*;

#[derive(Debug, Sanitize)]
struct SignupData {
    #[sanitize(trim, lower_case)]
    mail: String,
    #[sanitize(clamp(1, 60))]
    age: u8,
    #[sanitize]
    user: User,
}

#[derive(Debug, Sanitize)]
struct User {
    id: u64,
    #[sanitize(trim, clamp(50))]
    name: String,
}

fn main() {
    let instance = SignupData::new();
    instance.sanitize();
}

If you do not want to use the derive macro, then the sanitizer crate provides structures and methods for sanitizing ints

let int: u8 = 50;
let mut instance = IntSanitizer::from(int);
instance.clamp(99, 101);
assert_eq!(99, instance.get());

and strings

let mut sanitize = StringSanitizer::from("    some_string12 ");
sanitize.trim().numeric();
assert_eq!("12", sanitize.get());

Sanitizers

trim

Removes whitespace from ends.

numeric

Removes any character that is not a numeric.

alphanumeric

Removes any character that is not an alphanumeric.

lower_case

Converts string input to lowercase.

upper_case

Converts string input to UPPERCASE.

camel_case

Converts string input to camelCase.

snake_case

Converts string input to snake_case.

screaming_snake_case

Converts string input to SCREAMING_SNAKE_CASE using the Inflector crate.

e164

Converts string input to E164 International Phone Number format. This panics if the phone number is not a valid one.

clamp(min, max)

Limit an valid integer field with the given min and max.

clamp(max)

Limit a string input length to the following number

custom(function)

Use a custom function to sanitize a field differently. For example


#[derive(Sanitize)]
struct SanitizerTest {
    #[sanitize(custom(func_string))]
    field_string: String,
}

fn func_string(field: &str) -> String {
    let mut sanitizer = StringSanitizer::from(field);
    sanitizer.trim();
    sanitizer.get()
}

#[test]
fn sanitizer_check_custom_functions() {
    let mut instance = SanitizerTest {
        field_string: String::from("Hello    "),
    };
    instance.sanitize();
    assert_eq!(instance.field_string, String::from("Hello"));
}

nesting

#[derive(Sanitize)]
struct First {
    #[sanitize(trim)]
    name: String,
    #[sanitize]
    info: OtherInfo,
}

#[derive(Sanitize)]
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.

LICENSE

dashxhq/sanitizer is licensed under the MIT License.

Dependencies

~22MB
~159K SLoC