validify_derive 2.0.0

Derive macros for the validify crate
Documentation
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use super::parser::ValueOrPath;
use chrono::{NaiveDate, NaiveDateTime};
use proc_macro_error::abort;
use syn::{meta::ParseNestedMeta, spanned::Spanned, Lit};

#[derive(Debug)]
pub struct SchemaValidation {
    pub function: syn::Path,
}

/// Trait implemented by validators to output validation codes and messages.
pub trait Describe {
    fn code(&self) -> &str;

    fn message(&self) -> Option<&str>;
}

/// Contains all the validators that can be used
#[derive(Debug)]
pub enum Validator {
    Iter(Vec<Self>),
    Email(Email),
    Url(Url),
    CreditCard(CreditCard),
    Phone(Phone),
    Custom(Custom),
    Range(Range),
    Length(Length),
    NonControlCharacter(NonControlChar),
    Required(Required),
    Regex(Regex),
    Contains(Contains),
    Time(Time),
    In(In),
    Ip(Ip),
    Nested,
}

/// Shortcut for creating simple validation structs.
///
/// ```ignore
/// validation! {
///     StructName : "code"
///     [, OptionalDerive]*;
///     [field: T,]*
/// }
/// ```
///
/// All structs will have a `code` and `message` field
macro_rules! validation {
    ($id:ident : $code:literal $(,)? $($der:path),* ; $($key:ident : $typ:ty $(,)?),*) => {
        #[derive(Debug, $($der),*)]
        pub struct $id {
            $(pub $key:$typ,)*
            pub code: Option<String>,
            pub message: Option<String>,
        }

        impl $crate::validate::validation::Describe for $id {
            fn code(&self) -> &str {
                if let Some(ref code) = self.code {
                   code
                } else {
                    $code
                }
            }

            fn message(&self) -> Option<&str> {
                self.message.as_deref()
            }
        }
    };
}

validation!(
    Email : "email",
    Default;
);

validation!(
    Url : "url",
    Default;
);

validation!(
    Phone : "phone",
    Default;
);

validation!(
    CreditCard : "credit_card",
    Default;
);

validation!(
    NonControlChar : "non_control_char",
    Default;
);

validation!(
    Length : "length",
    Default;
    min: Option<ValueOrPath<u64>>,
    max: Option<ValueOrPath<u64>>,
    equal: Option<ValueOrPath<u64>>
);

validation!(
    Range : "range",
    Default;
    min: Option<ValueOrPath<f64>>,
    max: Option<ValueOrPath<f64>>
);

validation!(
    Nested : "nested",
    Default;
);

validation!(
    Required : "required",
    Default;
);

validation!(
    Custom : "custom";
    path: syn::Path
);

impl Custom {
    pub fn new(f: syn::Path) -> Self {
        Self {
            path: f,
            code: None,
            message: None,
        }
    }
}

validation!(
    Regex : "regex";
    path: syn::Path
);

impl Regex {
    pub fn new(path: syn::Path) -> Self {
        Self {
            path,
            code: None,
            message: None,
        }
    }
}

validation!(
    Ip : "ip",
    Default;
    format: Option<IpFormat>
);

#[derive(Debug)]
pub enum IpFormat {
    V4,
    V6,
}

#[derive(Debug)]
pub struct In {
    pub not: bool,
    pub expr: Option<syn::Expr>,
    pub code: Option<String>,
    pub message: Option<String>,
}

impl In {
    pub fn new(not: bool) -> Self {
        Self {
            not,
            expr: None,
            code: None,
            message: None,
        }
    }
}

impl Describe for In {
    fn code(&self) -> &str {
        if let Some(ref code) = self.code {
            code
        } else if self.not {
            "not_in"
        } else {
            "in"
        }
    }

    fn message(&self) -> Option<&str> {
        self.message.as_deref()
    }
}

#[derive(Debug, Default)]
pub struct Contains {
    pub not: bool,
    pub value: Option<ValueOrPath<Lit>>,
    pub code: Option<String>,
    pub message: Option<String>,
}

impl Describe for Contains {
    fn code(&self) -> &str {
        if let Some(ref code) = self.code {
            code
        } else if self.not {
            "contains_not"
        } else {
            "contains"
        }
    }

    fn message(&self) -> Option<&str> {
        self.message.as_deref()
    }
}

impl Contains {
    pub fn new(value: ValueOrPath<Lit>, not: bool) -> Self {
        Self {
            not,
            value: Some(value),
            ..Default::default()
        }
    }
}

#[derive(Debug, Default)]
pub struct Time {
    pub op: TimeOp,
    pub target: Option<ValueOrPath<String>>,
    pub duration: Option<ValueOrPath<i64>>,
    pub format: Option<String>,
    pub inclusive: bool,
    pub code: Option<String>,
    pub message: Option<String>,

    /// Used in case a path is used for the duration. We have to keep track of which chrono::Duration method to call.
    pub multiplier: TimeMultiplier,

    /// Whether to use chrono date or datetime
    pub has_time: bool,
}

#[derive(Debug)]
pub enum TimeMultiplier {
    Seconds,
    Minutes,
    Hours,
    Days,
    Weeks,
    None,
}

impl Default for TimeMultiplier {
    fn default() -> Self {
        Self::None
    }
}

impl Describe for Time {
    fn code(&self) -> &str {
        if let Some(ref code) = self.code {
            code
        } else {
            match self.op {
                TimeOp::BeforeNow => "before_now",
                TimeOp::AfterNow => "after_now",
                TimeOp::Before if self.inclusive => "before_or_equal",
                TimeOp::Before => "before",
                TimeOp::After if self.inclusive => "after_or_equal",
                TimeOp::After => "after",
                TimeOp::BeforeFromNow => "before_from_now",
                TimeOp::AfterFromNow => "after_from_now",
                TimeOp::InPeriod => "in_period",
                TimeOp::None => unreachable!(),
            }
        }
    }

    fn message(&self) -> Option<&str> {
        self.message.as_deref()
    }
}

impl Time {
    pub fn assert(&self, meta: &ParseNestedMeta) -> Result<(), syn::Error> {
        if matches!(self.target, Some(ValueOrPath::Value(_))) && self.format.is_none() {
            return Err(meta.error("string literal targets must contain a format"));
        }

        if matches!(self.target, Some(ValueOrPath::Path(_))) && self.format.is_some() {
            return Err(meta.error("path targets cannot contain formats"));
        }

        let no_multiplier = matches!(self.target, Some(ValueOrPath::Path(_)))
            && matches!(self.multiplier, TimeMultiplier::None);

        if let (Some(ValueOrPath::Value(date_str)), Some(format)) = (&self.target, &self.format) {
            let dt = NaiveDateTime::parse_from_str(date_str, format);
            let d = NaiveDate::parse_from_str(date_str, format);
            if dt.is_err() && d.is_err() {
                abort!(
                    meta.path.span(),
                    "The target string does not match the provided format"
                )
            }
        }

        match self.op {
            TimeOp::BeforeNow => {
                if self.duration.is_some() {
                    abort!(meta.path.span(), "before_now cannot have interval");
                }
                if self.target.is_some() {
                    abort!(meta.path.span(), "before_now cannot have target");
                }
            }
            TimeOp::AfterNow => {
                if self.duration.is_some() {
                    abort!(meta.path.span(), "after_now cannot have interval");
                }
                if self.target.is_some() {
                    abort!(meta.path.span(), "after_now cannot have target");
                }
            }
            TimeOp::Before => {
                if self.target.is_none() {
                    abort!(meta.path.span(), "before must have a target");
                }
                if self.duration.is_some() {
                    abort!(meta.path.span(), "before cannot have interval");
                }
            }
            TimeOp::After => {
                if self.target.is_none() {
                    abort!(meta.path.span(), "after must have target");
                }
                if self.duration.is_some() {
                    abort!(meta.path.span(), "after cannot have interval");
                }
            }
            TimeOp::BeforeFromNow => {
                if no_multiplier {
                    abort!(meta.path.span(), "path targets must have an interval")
                }
                if self.target.is_some() {
                    abort!(meta.path.span(), "before_from_now cannot have target");
                }
                if self.duration.is_none() {
                    abort!(meta.path.span(), "before_from_now must have interval");
                }
                if let Some(value) = self.duration.as_ref().unwrap().peek_value() {
                    if *value < 0 {
                        abort!(
                            meta.path.span(),
                            "before_from_now must have a positive duration, if you need to validate after use after_from_now"
                        );
                    }
                }
            }
            TimeOp::AfterFromNow => {
                if no_multiplier {
                    abort!(meta.path.span(), "path targets must have an interval")
                }
                if self.target.is_some() {
                    abort!(meta.path.span(), "after_from_now cannot have target");
                }
                if self.duration.is_none() {
                    abort!(meta.path.span(), "after_from_now must have interval");
                }
                if let Some(value) = self.duration.as_ref().unwrap().peek_value() {
                    if *value < 0 {
                        abort!(
                            meta.path.span(),
                            "after_from_now must have a positive duration, if you need to validate before use before_from_now"
                        );
                    }
                }
            }
            TimeOp::InPeriod => {
                if no_multiplier {
                    abort!(meta.path.span(), "path targets must have an interval")
                }
                if self.target.is_none() {
                    abort!(meta.path.span(), "in_period must have target");
                }
                if self.duration.is_none() {
                    abort!(meta.path.span(), "in_period must have interval");
                }
            }
            TimeOp::None => {
                abort!(meta.path.span(), "op must be specified")
            }
        };
        Ok(())
    }
}

#[derive(Debug)]
pub enum TimeOp {
    BeforeNow,
    AfterNow,
    Before,
    After,
    BeforeFromNow,
    AfterFromNow,
    InPeriod,
    None,
}

impl From<String> for TimeOp {
    fn from(value: String) -> Self {
        use TimeOp::*;

        match value.as_str() {
            "before" => Before,
            "after" => After,
            "before_now" => BeforeNow,
            "after_now" => AfterNow,
            "before_from_now" => BeforeFromNow,
            "after_from_now" => AfterFromNow,
            "in_period" => InPeriod,
            _ => Self::None,
        }
    }
}

impl Default for TimeOp {
    fn default() -> Self {
        Self::None
    }
}