valid8r_derive 0.2.2

Proc macro that provides `#[derive(Validate)]` for the `valid8r` 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! This module handles parsing and token generation for "prohibited" conditional validation attributes
//! like `#[validate(prohibited_if(...))]`, `#[validate(prohibited_with(...))]`, etc.
//!
//! These validators ensure that a field (typically an `Option` or type implementing
//! `valid8r::ValidateProhibited` is absent (e.g., `None`) if certain conditions involving other fields or functions are met.

use darling::FromMeta;
use proc_macro_error2::abort;
use proc_macro2::TokenStream as TokenStream2;
use quote::{ToTokens, quote};
use syn::{Attribute, Field, Ident, LitStr, Type, spanned::Spanned};

use crate::ValidateField;
use crate::utils::{
    CrateName, field_optionality, generate_is_prohibited_check, generate_presence_check, get_attr,
    is_optionish_type, quote_code, quote_message,
};

/// The field under validation must be None if the function returns true.
#[derive(Debug, Clone, FromMeta)]
pub struct ProhibitedIf {
    /// Optional custom error code for this validation.
    pub code: Option<String>,

    /// Optional custom error message for this validation.
    pub message: Option<String>,

    /// The function to call to determine if the prohibition condition is met.
    pub func: darling::Result<syn::Path>,
}

/// The field under validation must be None if any of the other specified fields are Some.
#[derive(Debug, Clone, FromMeta)]
pub struct ProhibitedWith {
    /// Optional custom error code for this validation.
    pub code: Option<String>,

    /// Optional custom error message for this validation.
    pub message: Option<String>,

    /// The other fields to check for presence.
    pub other_fields: Vec<LitStr>,
}

/// The field under validation must be None if all of the other specified fields are Some.
#[derive(Debug, Clone, FromMeta)]
pub struct ProhibitedWithAll {
    /// Optional custom error code for this validation.
    pub code: Option<String>,

    /// Optional custom error message for this validation.
    pub message: Option<String>,

    /// The other fields to check for presence.
    pub other_fields: Vec<LitStr>,
}

/// The field under validation must be None if any of the other specified fields are None.
#[derive(Debug, Clone, FromMeta)]
pub struct ProhibitedWithout {
    /// Optional custom error code for this validation.
    pub code: Option<String>,

    /// Optional custom error message for this validation.
    pub message: Option<String>,

    /// The other fields to check for absence.
    pub other_fields: Vec<LitStr>,
}

/// The field under validation must be Some if all of the other fields are None
#[derive(Debug, Clone, FromMeta)]
pub struct ProhibitedWithoutAll {
    /// Optional custom error code for this validation.
    pub code: Option<String>,

    /// Optional custom error message for this validation.
    pub message: Option<String>,

    /// The other fields to check for absence.
    pub other_fields: Vec<LitStr>,
}

pub fn check_prohibited_validator_form(
    field: &ValidateField,
    field_name: &str,
    field_attrs: &[Attribute],
    all_fields: &[&Field],
) {
    // Check for `prohibited_if` function path validity
    if let Some(prohibited_if) = &field.prohibited_if
        && let Err(e) = &prohibited_if.func
    {
        abort!(
            e.span(),
            "Invalid attribute #[validate(prohibited_if(...))] on field `{}`. `func` must be a valid path like `function_name` or `path::to::function_name`",
            field_name
        );
    }

    // Determine the error span for attributes like prohibited_with etc.
    // This provides a good fallback span for errors related to the attribute itself.
    let error_span = get_attr(field_attrs, "prohibited_with")
        .or_else(|| get_attr(field_attrs, "prohibited_with_all"))
        .or_else(|| get_attr(field_attrs, "prohibited_without"))
        .or_else(|| get_attr(field_attrs, "prohibited_without_all"))
        .map_or_else(
            || {
                // If no prohibited_*_with/without attributes, use the field's first attribute or call_site
                field_attrs
                    .first()
                    .map_or_else(proc_macro2::Span::call_site, |a| a.span())
            },
            |a| a.span(),
        );

    // Get the type of the main field being validated
    let main_field_type = &field.ty;

    // Helper closure to check and report if a field is not Option<T> or Delta<T>
    let check_option_type_and_abort = |span: proc_macro2::Span,
                                       current_field_name: &str,
                                       current_field_type: &Type,
                                       attribute_name: &str| {
        if !is_optionish_type(current_field_type) {
            abort!(
                span, // Use the specific attribute's span if available, otherwise the general error_span
                "`#[validate({}(...))]` for prohibited checks expects fields of type `Option<T>` or `Delta<T>`, but field `{}` is of type `{}`",
                attribute_name,
                current_field_name,
                current_field_type.to_token_stream()
            );
        }
    };

    // --- Check `prohibited_with` ---
    if let Some(prohibited_with) = &field.prohibited_with {
        // 1. Check the type of the main field
        check_option_type_and_abort(error_span, field_name, main_field_type, "prohibited_with");

        if prohibited_with.other_fields.is_empty() {
            abort!(
                error_span,
                "Invalid attribute #[validate(prohibited_with(...))] on field `{}`, other fields not specified. Specify other fields like #[validate(prohibited_with(other_field1, other_field2,...))]",
                field_name
            );
        } else {
            for other_field_litstr in &prohibited_with.other_fields {
                // other_field here is a LitStr
                let other_field_name = other_field_litstr.value(); // Get the string name

                // Find the syn::Field corresponding to `other_field_name`
                let other_field_syn_field = all_fields
                    .iter()
                    .find(|f| f.ident.as_ref().is_some_and(|i| i == &other_field_name));

                if let Some(f) = other_field_syn_field {
                    // 2. Check the type of the 'other field'
                    check_option_type_and_abort(
                        other_field_litstr.span(),
                        &other_field_name,
                        &f.ty,
                        "prohibited_with",
                    );
                } else {
                    abort!(
                        other_field_litstr.span(), // Point to the specific other_field in the attribute
                        "Invalid attribute for #[prohibited_with(...)] on field `{}`, unknown other field `{}`",
                        field_name,
                        other_field_name
                    );
                }
            }
        }
    }

    // --- Check `prohibited_with_all` ---
    if let Some(prohibited_with_all) = &field.prohibited_with_all {
        // 1. Check the type of the main field
        check_option_type_and_abort(
            error_span,
            field_name,
            main_field_type,
            "prohibited_with_all",
        );

        if prohibited_with_all.other_fields.is_empty() {
            abort!(
                error_span,
                "Invalid attribute #[validate(prohibited_with_all(...))] on field `{}`, other fields not specified. Specify other fields like #[validate(prohibited_with_all(other_field1, other_field2,...))]",
                field_name
            );
        } else {
            for other_field_litstr in &prohibited_with_all.other_fields {
                let other_field_name = other_field_litstr.value();

                let other_field_syn_field = all_fields
                    .iter()
                    .find(|f| f.ident.as_ref().is_some_and(|i| i == &other_field_name));

                if let Some(f) = other_field_syn_field {
                    // 2. Check the type of the 'other field'
                    check_option_type_and_abort(
                        other_field_litstr.span(),
                        &other_field_name,
                        &f.ty,
                        "prohibited_with_all",
                    );
                } else {
                    abort!(
                        other_field_litstr.span(),
                        "Invalid attribute for #[prohibited_with_all(...)] on field `{}`, unknown other field `{}`",
                        field_name,
                        other_field_name
                    );
                }
            }
        }
    }

    // --- Check `prohibited_without` ---
    if let Some(prohibited_without) = &field.prohibited_without {
        // 1. Check the type of the main field
        check_option_type_and_abort(
            error_span,
            field_name,
            main_field_type,
            "prohibited_without",
        );

        if prohibited_without.other_fields.is_empty() {
            abort!(
                error_span,
                "Invalid attribute #[validate(prohibited_without(...))] on field `{}`, other fields not specified. Specify other fields like #[validate(prohibited_without(other_field1, other_field2,...))]",
                field_name
            );
        } else {
            for other_field_litstr in &prohibited_without.other_fields {
                let other_field_name = other_field_litstr.value();

                let other_field_syn_field = all_fields
                    .iter()
                    .find(|f| f.ident.as_ref().is_some_and(|i| i == &other_field_name));

                if let Some(f) = other_field_syn_field {
                    // 2. Check the type of the 'other field'
                    check_option_type_and_abort(
                        other_field_litstr.span(),
                        &other_field_name,
                        &f.ty,
                        "prohibited_without",
                    );
                } else {
                    abort!(
                        other_field_litstr.span(),
                        "Invalid attribute for #[prohibited_without(...)] on field `{}`, unknown other field `{}`",
                        field_name,
                        other_field_name
                    );
                }
            }
        }
    }

    // --- Check `prohibited_without_all` ---
    if let Some(prohibited_without_all) = &field.prohibited_without_all {
        // 1. Check the type of the main field
        check_option_type_and_abort(
            error_span,
            field_name,
            main_field_type,
            "prohibited_without_all",
        );

        if prohibited_without_all.other_fields.is_empty() {
            abort!(
                error_span,
                "Invalid attribute #[validate(prohibited_without_all(...))] on field `{}`, other fields not specified. Specify other fields like #[validate(prohibited_without_all(other_field1, other_field2,...))]",
                field_name
            );
        } else {
            for other_field_litstr in &prohibited_without_all.other_fields {
                let other_field_name = other_field_litstr.value();

                let other_field_syn_field = all_fields
                    .iter()
                    .find(|f| f.ident.as_ref().is_some_and(|i| i == &other_field_name));

                if let Some(f) = other_field_syn_field {
                    // 2. Check the type of the 'other field'
                    check_option_type_and_abort(
                        other_field_litstr.span(),
                        &other_field_name,
                        &f.ty,
                        "prohibited_without_all",
                    );
                } else {
                    abort!(
                        other_field_litstr.span(),
                        "Invalid attribute for #[prohibited_without_all(...)] on field `{}`, unknown other field `{}`",
                        field_name,
                        other_field_name
                    );
                }
            }
        }
    }
}

pub fn prohibited_if_tokens(
    crate_name: &CrateName,
    field_name: &Ident,
    field_name_str: &str,
    params: ProhibitedIf,
    all_fields: &[&Field],
) -> TokenStream2 {
    let message = quote_message(params.message);
    let code = quote_code(crate_name, params.code, "prohibited_if");

    // Classify the optionality of the field being validated
    let optionality = field_optionality(field_name_str, all_fields);

    // Generate the "is prohibited" check for the field
    let is_prohibited_check = generate_is_prohibited_check(field_name, optionality);

    // It's ok to unwrap here, `check_consent_validator_form` should have any caught errors.
    let func_call = params.func.unwrap();

    quote! {
        // Field is prohibited (must have no value) if func_call is true.
        // For Option<Option<T>>, "has value" means Some(Some(_))
        if #func_call(&self) && !#is_prohibited_check {
            #code
            #message
            err.add_param(::std::borrow::Cow::from("value"), &self.#field_name);
            errors.add(#field_name_str, err);
        }
    }
}

pub fn prohibited_with_tokens(
    crate_name: &CrateName,
    field_name: &Ident,
    field_name_str: &str,
    params: ProhibitedWith,
    all_fields: &[&Field],
) -> TokenStream2 {
    let message = quote_message(params.message);
    let code = quote_code(crate_name, params.code, "prohibited_with");

    // Classify the optionality of the field being validated
    let optionality = field_optionality(field_name_str, all_fields);

    // Generate the individual checks for each "other" field.
    // Uses appropriate presence check based on Option nesting level.
    let other_field_presence_checks: Vec<TokenStream2> = params
        .other_fields
        .iter()
        .map(|other_field| {
            let other_field_str = other_field.value();
            let other_field_ident = Ident::new(&other_field_str, other_field.span());

            // Classify the other field's optionality and generate the appropriate presence check
            let other_optionality = field_optionality(&other_field_str, all_fields);
            generate_presence_check(&other_field_ident, other_optionality, true)
        })
        .collect();

    // Combine these checks with the logical OR (`||`) operator.
    // If `other_field_presence_checks` is empty (no other fields listed),
    // then `prohibition_condition_met` will be `false`.
    let prohibition_condition_met = if other_field_presence_checks.is_empty() {
        quote! { false }
    } else {
        quote! {
            (#(#other_field_presence_checks)||*)
        }
    };

    // Generate the "is prohibited" check for the field
    let is_prohibited_check = generate_is_prohibited_check(field_name, optionality);

    quote! {
        // prohibition_condition_met is true if any of the other fields have a value.
        // For Option<Option<T>>, "has value" means Some(Some(_))
        if #prohibition_condition_met && !#is_prohibited_check {
            #code
            #message
            err.add_param(::std::borrow::Cow::from("value"), &self.#field_name);
            errors.add(#field_name_str, err);
        }
    }
}

pub fn prohibited_with_all_tokens(
    crate_name: &CrateName,
    field_name: &Ident,
    field_name_str: &str,
    params: ProhibitedWithAll,
    all_fields: &[&Field],
) -> TokenStream2 {
    let message = quote_message(params.message);
    let code = quote_code(crate_name, params.code, "prohibited_with_all");

    // Classify the optionality of the field being validated
    let optionality = field_optionality(field_name_str, all_fields);

    // Generate individual checks for each "other" field.
    let other_field_presence_checks: Vec<TokenStream2> = params
        .other_fields
        .iter()
        .map(|other_field_litstr| {
            let other_field_str = other_field_litstr.value();
            let other_field_ident = Ident::new(&other_field_str, other_field_litstr.span());

            // Classify the other field's optionality and generate the appropriate presence check
            let other_optionality = field_optionality(&other_field_str, all_fields);
            generate_presence_check(&other_field_ident, other_optionality, true)
        })
        .collect();

    // Combine these checks with the logical AND (`&&`) operator.
    // The condition is true if ALL other fields have a value.
    let prohibition_condition_met = if other_field_presence_checks.is_empty() {
        // If no other fields are listed, this condition is vacuously true for 'all'
        // This means the main field would always be prohibited if the list is empty.
        // Usually, an empty list here would be an error caught by check_prohibited_validator_form.
        quote! { true }
    } else {
        quote! {
            (#(#other_field_presence_checks)&&*)
        }
    };

    // Generate the "is prohibited" check for the field
    let is_prohibited_check = generate_is_prohibited_check(field_name, optionality);

    quote! {
        // Field is prohibited if ALL other fields have a value AND field has a value.
        // For Option<Option<T>>, "has value" means Some(Some(_))
        if #prohibition_condition_met && !#is_prohibited_check {
            #code
            #message
            err.add_param(::std::borrow::Cow::from("value"), &self.#field_name);
            errors.add(#field_name_str, err);
        }
    }
}

pub fn prohibited_without_tokens(
    crate_name: &CrateName,
    field_name: &Ident,
    field_name_str: &str,
    params: ProhibitedWithout,
    all_fields: &[&Field],
) -> TokenStream2 {
    let message = quote_message(params.message);
    let code = quote_code(crate_name, params.code, "prohibited_without");

    // Classify the optionality of the field being validated
    let optionality = field_optionality(field_name_str, all_fields);

    // Generate individual checks for each "other" field.
    let other_field_absence_checks: Vec<TokenStream2> = params
        .other_fields
        .iter()
        .map(|other_field_litstr| {
            let other_field_str = other_field_litstr.value();
            let other_field_ident = Ident::new(&other_field_str, other_field_litstr.span());

            // Classify the other field's optionality and generate the appropriate absence check
            let other_optionality = field_optionality(&other_field_str, all_fields);
            generate_presence_check(&other_field_ident, other_optionality, false)
        })
        .collect();

    // Combine these checks with the logical OR (`||`) operator.
    // The condition is true if ANY other field is absent (has no value).
    let prohibition_condition_met = if other_field_absence_checks.is_empty() {
        // If no other fields are listed, this condition is false for 'any'
        // The main field would never be prohibited based on absence of other fields.
        quote! { false }
    } else {
        quote! {
            (#(#other_field_absence_checks)||*)
        }
    };

    // Generate the "is prohibited" check for the field
    let is_prohibited_check = generate_is_prohibited_check(field_name, optionality);

    quote! {
        // Main field is prohibited if ANY other field is absent, AND main field has a value.
        // For Option<Option<T>>, "absent" means !matches!(field, Some(Some(_)))
        if #prohibition_condition_met && !#is_prohibited_check {
            #code
            #message
            err.add_param(::std::borrow::Cow::from("value"), &self.#field_name);
            errors.add(#field_name_str, err);
        }
    }
}

pub fn prohibited_without_all_tokens(
    crate_name: &CrateName,
    field_name: &Ident,
    field_name_str: &str,
    params: ProhibitedWithoutAll,
    all_fields: &[&Field],
) -> TokenStream2 {
    let message = quote_message(params.message);
    let code = quote_code(crate_name, params.code, "prohibited_without_all");

    // Classify the optionality of the field being validated
    let optionality = field_optionality(field_name_str, all_fields);

    // Generate individual checks for each "other" field.
    let other_field_absence_checks: Vec<TokenStream2> = params
        .other_fields
        .iter()
        .map(|other_field_litstr| {
            let other_field_str = other_field_litstr.value();
            let other_field_ident = Ident::new(&other_field_str, other_field_litstr.span());

            // Classify the other field's optionality and generate the appropriate absence check
            let other_optionality = field_optionality(&other_field_str, all_fields);
            generate_presence_check(&other_field_ident, other_optionality, false)
        })
        .collect();

    // Combine these checks with the logical AND (`&&`) operator.
    // The condition is true if ALL other fields are absent (have no value).
    let prohibition_condition_met = if other_field_absence_checks.is_empty() {
        // If no other fields are listed, this condition is vacuously true for 'all'
        // This means the main field would always be prohibited if the list is empty.
        quote! { true }
    } else {
        quote! {
            (#(#other_field_absence_checks)&&*)
        }
    };

    // Generate the "is prohibited" check for the field
    let is_prohibited_check = generate_is_prohibited_check(field_name, optionality);

    quote! {
        // Main field is prohibited if ALL other fields are absent, AND main field has a value.
        // For Option<Option<T>>, "absent" means !matches!(field, Some(Some(_)))
        if #prohibition_condition_met && !#is_prohibited_check {
            #code
            #message
            err.add_param(::std::borrow::Cow::from("value"), &self.#field_name);
            errors.add(#field_name_str, err);
        }
    }
}