aboutsummaryrefslogtreecommitdiffstats
path: root/validator_derive/tests/non_control.rs
blob: 37fb15371902092635701c948b065d09dbd58e8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#[macro_use]
extern crate validator_derive;
extern crate validator;

use validator::Validate;

#[cfg(feature = "unic")]
#[test]
fn can_validate_utf8_ok() {
    #[derive(Debug, Validate)]
    struct TestStruct {
        #[validate(non_control_character)]
        val: String,
    }

    let s = TestStruct { val: "하늘".to_string() };

    assert!(s.validate().is_ok());
}

#[cfg(feature = "unic")]
#[test]
fn utf8_with_control_fails_validation() {
    #[derive(Debug, Validate)]
    struct TestStruct {
        #[validate(non_control_character)]
        val: String,
    }

    let s = TestStruct { val: "\u{009F}하늘".to_string() };
    let res = s.validate();
    assert!(res.is_err());
    let err = res.unwrap_err();
    let errs = err.field_errors();
    assert!(errs.contains_key("val"));
    assert_eq!(errs["val"].len(), 1);
    assert_eq!(errs["val"][0].code, "non_control_character");
}

#[cfg(feature = "unic")]
#[test]
fn can_specify_code_for_non_control_character() {
    #[derive(Debug, Validate)]
    struct TestStruct {
        #[validate(non_control_character(code = "oops"))]
        val: String,
    }
    let s = TestStruct { val: "\u{009F}하늘".to_string() };
    let res = s.validate();
    assert!(res.is_err());
    let err = res.unwrap_err();
    let errs = err.field_errors();
    assert!(errs.contains_key("val"));
    assert_eq!(errs["val"].len(), 1);
    assert_eq!(errs["val"][0].code, "oops");
    assert_eq!(errs["val"][0].params["value"], "\u{9F}하늘");
}

#[cfg(feature = "unic")]
#[test]
fn can_specify_message_for_non_control_character() {
    #[derive(Debug, Validate)]
    struct TestStruct {
        #[validate(non_control_character(message = "oops"))]
        val: String,
    }
    let s = TestStruct { val: "\u{009F}하늘".to_string() };
    let res = s.validate();
    assert!(res.is_err());
    let err = res.unwrap_err();
    let errs = err.field_errors();
    assert!(errs.contains_key("val"));
    assert_eq!(errs["val"].len(), 1);
    assert_eq!(errs["val"][0].clone().message.unwrap(), "oops");
}