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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
pub use ;
pub use ;
pub use ;
pub use ;
/// Validates the struct/enum based on the provided `#[validate]` attributes.
/// Deriving [Validate] allows you to specify schema and field validation on structs using the `#[validate]` attribute.
/// See the [repository](https://github.com/biblius/validify#validators) for a full list of possible validations.
/// Modifies the struct/enum based on the provided `#[modify]` attributes.
/// Automatically implemented when deriving [Validify].
/// See the [repository](https://github.com/biblius/validify#modifiers) for a full list of possible modifiers.
/// Validates and modifies the struct/enum based on the provided `#[validate]` and `#[modify]` attributes.
/// Deriving [Validify] allows you to modify structs before they are validated by providing
/// out of the box validation implementations as well as the ability to write custom ones.
///
/// ### Example
///
/// ```
/// use validify::Validify;
///
/// #[derive(Debug, Clone, serde::Deserialize, Validify)]
/// struct Testor {
/// #[modify(lowercase, trim)]
/// #[validate(length(equal = 8))]
/// pub a: String,
/// #[modify(trim, uppercase)]
/// pub b: Option<String>,
/// #[modify(custom(do_something))]
/// pub c: String,
/// #[modify(custom(do_something))]
/// pub d: Option<String>,
/// #[validify]
/// pub nested: Nestor,
/// }
///
/// #[derive(Debug, Clone, serde::Deserialize, Validify)]
/// struct Nestor {
/// #[modify(trim, uppercase)]
/// #[validate(length(equal = 12))]
/// a: String,
/// #[modify(capitalize)]
/// #[validate(length(equal = 14))]
/// b: String,
/// }
///
/// fn do_something(input: &mut String) {
/// *input = String::from("modified");
/// }
///
/// let mut test = Testor {
/// a: " LOWER ME ".to_string(),
/// b: Some(" makemeshout ".to_string()),
/// c: "I'll never be the same".to_string(),
/// d: Some("Me neither".to_string()),
/// nested: Nestor {
/// a: " notsotinynow ".to_string(),
/// b: "capitalize me.".to_string(),
/// },
/// };
///
/// // The magic line
/// let res = test.validify();
///
/// assert!(matches!(res, Ok(_)));
///
/// // Parent
/// assert_eq!(test.a, "lower me");
/// assert_eq!(test.b, Some("MAKEMESHOUT".to_string()));
/// assert_eq!(test.c, "modified");
/// assert_eq!(test.d, Some("modified".to_string()));
/// // Nested
/// assert_eq!(test.nested.a, "NOTSOTINYNOW");
/// assert_eq!(test.nested.b, "Capitalize me.");
/// ```
/// Exposes validify functionality on generated [Payload] structs.
/// Creates a new field validation error.
/// Serves as a shorthand for writing out errors for custom functions
/// and schema validations.
/// Accepts:
///
/// - `("code")`
/// - `("code", "message")`
/// - `("field_name", "code", "message")`
///
/// ```rust
/// use validify::field_err;
///
/// let err = field_err!("foo");
/// assert_eq!(err.code(), "foo");
/// assert_eq!(err.location(), "");
/// assert!(err.message().is_none());
/// assert!(err.field_name().is_none());
///
/// let err = field_err!("foo", "bar");
/// assert_eq!(err.code(), "foo");
/// assert_eq!(err.location(), "");
/// assert_eq!(err.message().unwrap(), "bar");
/// assert!(err.field_name().is_none());
///
/// let err = field_err!("foo", "bar", "field");
/// assert_eq!(err.code(), "foo");
/// assert_eq!(err.message().unwrap(), "bar");
/// assert_eq!(err.field_name().unwrap(), "field");
/// assert_eq!(err.location(), "/field");
/// ```