tailcall-valid 0.1.4

A Rust library for validating multiple inputs, collecting all possible errors instead of failing at the first error. Useful for scenarios where comprehensive feedback is required for user inputs or configuration settings.
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
use super::append::Append;
use super::Cause;

/// A validation type that can represent either a successful value of type `A`
/// or a collection of validation errors of type `E` with trace context `T`.
///
/// `Valid` is useful for accumulating multiple validation errors rather than
/// stopping at the first error encountered.
#[derive(Debug, PartialEq)]
pub struct Valid<A, E, T>(Result<A, Vec<Cause<E, T>>>);

/// Trait for types that can perform validation operations.
///
/// This trait provides a rich set of combinators for working with validations,
/// allowing you to chain, combine and transform validation results.
pub trait Validator<A, E, T>: Sized {
    /// Maps a function over the successful value, transforming it to a new type.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let valid = Valid::<i32, (), ()>::succeed(1);
    /// let result = valid.map(|x| x.to_string());
    /// assert_eq!(result, Valid::succeed("1".to_string()));
    /// ```
    fn map<A1>(self, f: impl FnOnce(A) -> A1) -> Valid<A1, E, T> {
        Valid(self.to_result().map(f))
    }

    /// Executes a side effect function if the validation is successful.
    /// The original value is preserved.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let mut sum = 0;
    /// let valid = Valid::<i32, (), ()>::succeed(5);
    /// valid.foreach(|x| sum += x);
    /// assert_eq!(sum, 5);
    /// ```
    fn foreach(self, mut f: impl FnMut(A)) -> Valid<A, E, T>
    where
        A: Clone,
    {
        match self.to_result() {
            Ok(a) => {
                f(a.clone());
                Valid::succeed(a)
            }
            Err(e) => Valid(Err(e)),
        }
    }

    /// Returns true if the validation is successful.
    fn is_succeed(&self) -> bool;

    /// Returns true if the validation contains errors.
    fn is_fail(&self) -> bool;

    /// Combines two validations, keeping the result of the second one if both succeed.
    /// If either validation fails, all errors are collected.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let v1 = Valid::<i32, &str, ()>::succeed(1);
    /// let v2 = Valid::<&str, &str, ()>::succeed("ok");
    /// assert_eq!(v1.and(v2), Valid::succeed("ok"));
    /// ```
    fn and<A1>(self, other: Valid<A1, E, T>) -> Valid<A1, E, T> {
        self.zip(other).map(|(_, a1)| a1)
    }

    /// Combines two validations into a tuple of their results.
    /// If either validation fails, all errors are collected.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let v1 = Valid::<i32, &str, ()>::succeed(1);
    /// let v2 = Valid::<&str, &str, ()>::succeed("ok");
    /// assert_eq!(v1.zip(v2), Valid::succeed((1, "ok")));
    /// ```
    fn zip<A1>(self, other: Valid<A1, E, T>) -> Valid<(A, A1), E, T> {
        match self.to_result() {
            Ok(a) => match other.0 {
                Ok(a1) => Valid(Ok((a, a1))),
                Err(e1) => Valid(Err(e1)),
            },
            Err(mut e1) => match other.0 {
                Ok(_) => Valid(Err(e1)),
                Err(e2) => {
                    e1.extend(e2);
                    Valid(Err(e1))
                }
            },
        }
    }

    /// Starts a fusion chain of validations. This allows combining multiple
    /// validation results using the `Append` trait.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let v1: Valid<Vec<i32>, (), ()> = Valid::succeed(vec![1, 2]);
    /// let v2: Valid<Vec<i32>, (), ()> = Valid::succeed(vec![3, 4]);
    /// let result = v1.fuse(v2);
    /// assert_eq!(result.to_result().unwrap(), (vec![1, 2], vec![3, 4]));
    /// ```
    fn fuse<A1>(self, other: Valid<A1, E, T>) -> Fusion<(A, A1), E, T> {
        Fusion(self.zip(other))
    }

    /// Adds trace context to any errors in the validation.
    /// Successful validations are unaffected.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let result = Valid::<(), &str, &str>::fail("error")
    ///     .trace("field_name")
    ///     .trace("form");
    /// ```
    fn trace(self, trace: impl Into<T> + Clone) -> Valid<A, E, T> {
        let valid = self.to_result();
        if let Err(error) = valid {
            return Valid(Err(error
                .into_iter()
                .map(|cause| cause.trace(trace.clone().into()))
                .collect()));
        }

        Valid(valid)
    }

    /// Handles both success and failure cases of a validation.
    ///
    /// - If successful, applies the `ok` function to the value
    /// - If failed, calls the `err` function and combines any new errors
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let valid = Valid::<i32, &str, ()>::succeed(1);
    /// let result = valid.fold(
    ///     |n| Valid::succeed(n + 1),
    ///     || Valid::succeed(0)
    /// );
    /// assert_eq!(result, Valid::succeed(2));
    /// ```
    fn fold<A1>(
        self,
        ok: impl FnOnce(A) -> Valid<A1, E, T>,
        err: impl FnOnce() -> Valid<A1, E, T>,
    ) -> Valid<A1, E, T> {
        match self.to_result() {
            Ok(a) => ok(a),
            Err(e) => Valid::<A1, E, T>(Err(e)).and(err()),
        }
    }

    /// Converts the validation into a Result.
    fn to_result(self) -> Result<A, Vec<Cause<E, T>>>;

    /// Chains a validation operation by applying a function to a successful value.
    /// If the original validation failed, the errors are propagated.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let valid = Valid::<i32, &str, ()>::succeed(1);
    /// let result = valid.and_then(|n| {
    ///     if n > 0 {
    ///         Valid::succeed(n * 2)
    ///     } else {
    ///         Valid::fail("must be positive")
    ///     }
    /// });
    /// assert_eq!(result, Valid::succeed(2));
    /// ```
    fn and_then<B>(self, f: impl FnOnce(A) -> Valid<B, E, T>) -> Valid<B, E, T> {
        match self.to_result() {
            Ok(a) => f(a),
            Err(e) => Valid(Err(e)),
        }
    }

    /// Converts a successful validation to `()`.
    /// Failed validations retain their errors.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let valid = Valid::<i32, &str, ()>::succeed(1);
    /// assert_eq!(valid.unit(), Valid::succeed(()));
    /// ```
    fn unit(self) -> Valid<(), E, T> {
        self.map(|_| ())
    }

    /// Wraps a successful value in Some(_).
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let valid = Valid::<i32, &str, ()>::succeed(1);
    /// assert_eq!(valid.some(), Valid::succeed(Some(1)));
    /// ```
    fn some(self) -> Valid<Option<A>, E, T> {
        self.map(Some)
    }

    /// Maps a successful validation to a constant value.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let valid = Valid::<i32, &str, ()>::succeed(1);
    /// assert_eq!(valid.map_to("ok"), Valid::succeed("ok"));
    /// ```
    fn map_to<B>(self, b: B) -> Valid<B, E, T> {
        self.map(|_| b)
    }

    /// Conditionally validates based on a predicate.
    /// If the predicate returns false, succeeds with ().
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let valid = Valid::<(), &str, ()>::fail("error");
    /// let result = valid.when(|| false);
    /// assert_eq!(result, Valid::succeed(()));
    /// ```
    fn when(self, f: impl FnOnce() -> bool) -> Valid<(), E, T> {
        if f() {
            self.unit()
        } else {
            Valid::succeed(())
        }
    }
}

impl<A, E, T> Valid<A, E, T> {
    /// Creates a new failed validation with a single error.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let result: Valid<(), i32, ()> = Valid::fail(1);
    /// assert!(result.is_fail());
    /// ```
    pub fn fail(e: E) -> Valid<A, E, T> {
        Valid(Err(vec![Cause {
            error: e,
            trace: Default::default(),
        }]))
    }

    /// Creates a new failed validation with an error and trace context.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let result = Valid::<(), &str, &str>::fail_at("error", "context");
    /// assert!(result.is_fail());
    /// ```
    pub fn fail_at(error: E, trace: T) -> Valid<A, E, T>
    where
        E: std::fmt::Debug,
    {
        let cause = Cause::new(error).trace(trace);
        Valid(Err(vec![cause]))
    }

    /// Creates a new successful validation containing the given value.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let result = Valid::<i32, (), ()>::succeed(42);
    /// assert!(result.is_succeed());
    /// ```
    pub fn succeed(a: A) -> Valid<A, E, T> {
        Valid(Ok(a))
    }

    /// Validates each item in an iterator using the provided validation function,
    /// collecting all errors that occur.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let numbers = vec![1, 2, 3];
    /// let result = Valid::from_iter(numbers, |n| {
    ///     if n % 2 == 0 {
    ///         Valid::<i32, String, ()>::succeed(n * 2)
    ///     } else {
    ///         Valid::<i32, String, ()>::fail(format!("{} is odd", n))
    ///     }
    /// });
    /// ```
    pub fn from_iter<B>(
        iter: impl IntoIterator<Item = A>,
        mut f: impl FnMut(A) -> Valid<B, E, T>,
    ) -> Valid<Vec<B>, E, T> {
        let mut values: Vec<B> = Vec::new();
        let mut errors: Vec<Cause<E, T>> = Vec::new();
        for a in iter.into_iter() {
            match f(a).to_result() {
                Ok(b) => values.push(b),
                Err(err) => errors.extend(err),
            }
        }

        if errors.is_empty() {
            Valid::succeed(values)
        } else {
            Valid::from(errors)
        }
    }

    /// Creates a new `Valid` from an `Option` value.
    /// If the option is `None`, creates a failed validation with the provided error.
    /// If the option is `Some`, creates a successful validation with the contained value.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let some_value = Some(42);
    /// let result: Valid<i32, &str, ()> = Valid::from_option(some_value, "error");
    /// assert_eq!(result, Valid::succeed(42));
    ///
    /// let none_value: Option<i32> = None;
    /// let result: Valid<i32, &str, ()> = Valid::from_option(none_value, "error");
    /// assert!(result.is_fail());
    /// ```
    pub fn from_option(option: Option<A>, e: E) -> Valid<A, E, T> {
        match option {
            Some(a) => Valid::succeed(a),
            None => Valid::fail(e),
        }
    }

    /// Creates a successful validation containing `None`.
    ///
    /// This is useful when you want to explicitly represent the absence of a value
    /// as a successful validation rather than an error condition.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::Valid;
    /// let result: Valid<Option<i32>, &str, ()> = Valid::none();
    /// assert_eq!(result, Valid::succeed(None));
    /// ```
    pub fn none() -> Valid<Option<A>, E, T> {
        Valid::succeed(None)
    }
}

impl<A, E, T> From<Cause<E, T>> for Valid<A, E, T> {
    /// Creates a failed validation from a single `Cause`.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator, Cause};
    /// let cause = Cause::new("error");
    /// let result: Valid<(), &str, ()> = Valid::from(cause);
    /// assert!(result.is_fail());
    /// ```
    fn from(value: Cause<E, T>) -> Self {
        Valid(Err(vec![value]))
    }
}

impl<A, E, T> From<Vec<Cause<E, T>>> for Valid<A, E, T> {
    /// Creates a failed validation from a vector of `Cause`s.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator, Cause};
    /// let causes = vec![Cause::new("error1"), Cause::new("error2")];
    /// let result: Valid<(), &str, ()> = Valid::from(causes);
    /// assert!(result.is_fail());
    /// ```
    fn from(value: Vec<Cause<E, T>>) -> Self {
        Valid(Err(value))
    }
}

impl<A, E, T> Validator<A, E, T> for Valid<A, E, T> {
    fn to_result(self) -> Result<A, Vec<Cause<E, T>>> {
        self.0
    }

    fn is_succeed(&self) -> bool {
        self.0.is_ok()
    }

    fn is_fail(&self) -> bool {
        self.0.is_err()
    }
}

/// A type that allows chaining multiple validations together while combining their results.
///
/// `Fusion` is particularly useful when you want to accumulate values from multiple
/// successful validations into a single composite value.
pub struct Fusion<A, E, T>(Valid<A, E, T>);
impl<A, E, T> Fusion<A, E, T> {
    /// Combines this fusion with another validation, using the `Append` trait to
    /// combine their successful values.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let v1: Valid<Vec<i32>, (), ()> = Valid::succeed(vec![1, 2]);
    /// let v2: Valid<Vec<i32>, (), ()> = Valid::succeed(vec![3, 4]);
    /// let fusion = v1.fuse(v2);
    /// let result = fusion.to_result().unwrap();
    /// assert_eq!(result, (vec![1, 2], vec![3, 4]));
    /// ```
    pub fn fuse<A1>(self, other: Valid<A1, E, T>) -> Fusion<A::Out, E, T>
    where
        A: Append<A1>,
    {
        Fusion(self.0.zip(other).map(|(a, a1)| a.append(a1)))
    }
}

impl<A, E, T> Validator<A, E, T> for Fusion<A, E, T> {
    fn to_result(self) -> Result<A, Vec<Cause<E, T>>> {
        self.0.to_result()
    }
    fn is_succeed(&self) -> bool {
        self.0.is_succeed()
    }
    fn is_fail(&self) -> bool {
        self.0.is_fail()
    }
}

impl<A, E, T> From<Result<A, Cause<E, T>>> for Valid<A, E, T> {
    /// Creates a `Valid` from a `Result` containing a single `Cause` as its error type.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator, Cause};
    /// let ok_result: Result<i32, Cause<&str, ()>> = Ok(42);
    /// let valid = Valid::from(ok_result);
    /// assert_eq!(valid, Valid::succeed(42));
    ///
    /// let err_result: Result<i32, Cause<&str, ()>> = Err(Cause::new("error"));
    /// let valid = Valid::from(err_result);
    /// assert!(valid.is_fail());
    /// ```
    fn from(value: Result<A, Cause<E, T>>) -> Self {
        match value {
            Ok(a) => Valid::succeed(a),
            Err(e) => Valid(Err(vec![e])),
        }
    }
}

impl<A, E, T> From<Result<A, Vec<Cause<E, T>>>> for Valid<A, E, T> {
    /// Creates a `Valid` from a `Result` containing multiple `Cause`s as its error type.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator, Cause};
    /// let ok_result: Result<i32, Vec<Cause<&str, ()>>> = Ok(42);
    /// let valid = Valid::from(ok_result);
    /// assert_eq!(valid, Valid::succeed(42));
    ///
    /// let err_result: Result<i32, Vec<Cause<&str, ()>>> = Err(vec![
    ///     Cause::new("error1"),
    ///     Cause::new("error2")
    /// ]);
    /// let valid = Valid::from(err_result);
    /// assert!(valid.is_fail());
    /// ```
    fn from(value: Result<A, Vec<Cause<E, T>>>) -> Self {
        match value {
            Ok(a) => Valid::succeed(a),
            Err(e) => Valid(Err(e)),
        }
    }
}

impl<A, E, T> From<Fusion<A, E, T>> for Valid<A, E, T> {
    /// Converts a `Fusion` back into a `Valid`.
    ///
    /// This is typically used at the end of a chain of `fuse` operations
    /// to convert the final result back into a `Valid`.
    ///
    /// # Examples
    /// ```
    /// use tailcall_valid::{Valid, Validator};
    /// let v1: Valid<Vec<i32>, (), ()> = Valid::succeed(vec![1]);
    /// let v2: Valid<Vec<i32>, (), ()> = Valid::succeed(vec![2]);
    /// let fusion = v1.fuse(v2);
    /// let result: Valid<(Vec<i32>, Vec<i32>), (), ()> = Valid::from(fusion);
    /// assert!(result.is_succeed());
    /// ```
    fn from(value: Fusion<A, E, T>) -> Self {
        Valid(value.to_result())
    }
}

impl<A, E, T> Clone for Valid<A, E, T>
where
    A: Clone,
    E: Clone,
    T: Clone,
{
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

#[cfg(test)]
mod tests {
    use super::{Cause, Valid, Validator};

    #[test]
    fn test_ok() {
        let result = Valid::<i32, (), ()>::succeed(1);
        assert_eq!(result, Valid::succeed(1));
    }

    #[test]
    fn test_fail() {
        let result = Valid::<(), i32, ()>::fail(1);
        assert_eq!(result, Valid::fail(1));
    }

    #[test]
    fn test_validate_or_both_ok() {
        let result1 = Valid::<bool, i32, ()>::succeed(true);
        let result2 = Valid::<u8, i32, ()>::succeed(3);

        assert_eq!(result1.and(result2), Valid::succeed(3u8));
    }

    #[test]
    fn test_validate_or_first_fail() {
        let result1 = Valid::<bool, i32, ()>::fail(-1);
        let result2 = Valid::<u8, i32, ()>::succeed(3);

        assert_eq!(result1.and(result2), Valid::fail(-1));
    }

    #[test]
    fn test_validate_or_second_fail() {
        let result1 = Valid::<bool, i32, ()>::succeed(true);
        let result2 = Valid::<u8, i32, ()>::fail(-2);

        assert_eq!(result1.and(result2), Valid::fail(-2));
    }

    #[test]
    fn test_validate_all() {
        let input: Vec<i32> = [1, 2, 3].to_vec();
        let result: Valid<Vec<i32>, i32, ()> = Valid::from_iter(input, |a| Valid::fail(a * 2));
        assert_eq!(
            result,
            Valid::from(vec![Cause::new(2), Cause::new(4), Cause::new(6)])
        );
    }

    #[test]
    fn test_validate_all_ques() {
        let input: Vec<i32> = [1, 2, 3].to_vec();
        let result: Valid<Vec<i32>, i32, ()> = Valid::from_iter(input, |a| Valid::fail(a * 2));
        assert_eq!(
            result,
            Valid::from(vec![Cause::new(2), Cause::new(4), Cause::new(6)])
        );
    }

    #[test]
    fn test_ok_ok_cause() {
        let option: Option<i32> = None;
        let result: Valid<i32, i32, ()> = Valid::from_option(option, 1);
        assert_eq!(result, Valid::from(vec![Cause::new(1)]));
    }

    #[test]
    fn test_trace() {
        let result = Valid::<(), i32, String>::fail(1)
            .trace("A")
            .trace("B")
            .trace("C");

        let expected = Valid::from(vec![Cause {
            error: 1,
            trace: vec!["C".to_string(), "B".to_string(), "A".to_string()].into(),
        }]);
        assert_eq!(result, expected);
    }

    #[test]
    fn test_validate_fold_err() {
        let valid = Valid::<(), i32, ()>::fail(1);
        let result = valid.fold(
            |_| Valid::<(), i32, ()>::fail(2),
            || Valid::<(), i32, ()>::fail(3),
        );
        assert_eq!(result, Valid::from(vec![Cause::new(1), Cause::new(3)]));
    }

    #[test]
    fn test_validate_fold_ok() {
        let valid = Valid::<i32, i32, i32>::succeed(1);
        let result = valid.fold(Valid::<i32, i32, i32>::fail, || {
            Valid::<i32, i32, i32>::fail(2)
        });
        assert_eq!(result, Valid::fail(1));
    }

    #[test]
    fn test_to_result() {
        let result = Valid::<(), i32, i32>::fail(1).to_result().unwrap_err();
        assert_eq!(result, vec![Cause::new(1)]);
    }

    #[test]
    fn test_validate_both_ok() {
        let result1 = Valid::<bool, i32, i32>::succeed(true);
        let result2 = Valid::<u8, i32, i32>::succeed(3);

        assert_eq!(result1.zip(result2), Valid::succeed((true, 3u8)));
    }
    #[test]
    fn test_validate_both_first_fail() {
        let result1 = Valid::<bool, i32, i32>::fail(-1);
        let result2 = Valid::<u8, i32, i32>::succeed(3);

        assert_eq!(result1.zip(result2), Valid::fail(-1));
    }
    #[test]
    fn test_validate_both_second_fail() {
        let result1 = Valid::<bool, i32, i32>::succeed(true);
        let result2 = Valid::<u8, i32, i32>::fail(-2);

        assert_eq!(result1.zip(result2), Valid::fail(-2));
    }

    #[test]
    fn test_validate_both_both_fail() {
        let result1 = Valid::<bool, i32, i32>::fail(-1);
        let result2 = Valid::<u8, i32, i32>::fail(-2);

        assert_eq!(
            result1.zip(result2),
            Valid::from(vec![Cause::new(-1), Cause::new(-2)])
        );
    }

    #[test]
    fn test_and_then_success() {
        let result = Valid::<i32, i32, i32>::succeed(1).and_then(|a| Valid::succeed(a + 1));
        assert_eq!(result, Valid::succeed(2));
    }

    #[test]
    fn test_and_then_fail() {
        let result =
            Valid::<i32, i32, i32>::succeed(1).and_then(|a| Valid::<i32, i32, i32>::fail(a + 1));
        assert_eq!(result, Valid::fail(2));
    }

    #[test]
    fn test_foreach_succeed() {
        let mut a = 0;
        let result = Valid::<i32, i32, i32>::succeed(1).foreach(|v| a = v);
        assert_eq!(result, Valid::succeed(1));
        assert_eq!(a, 1);
    }

    #[test]
    fn test_foreach_fail() {
        let mut a = 0;
        let result = Valid::<i32, i32, i32>::fail(1).foreach(|v| a = v);
        assert_eq!(result, Valid::fail(1));
        assert_eq!(a, 0);
    }

    #[test]
    fn test_trace_owned_referenced() {
        let trace_value = "inner".to_string();

        let valid: Valid<((), ()), &str, String> = Valid::fail("fail")
            .trace(&trace_value)
            .zip(Valid::fail("fail 2").trace(trace_value))
            .trace("outer");

        let causes = valid.to_result().unwrap_err();

        assert_eq!(causes.len(), 2);
        assert_eq!(causes[0].to_string(), "[outer, inner] fail");
        assert_eq!(causes[1].to_string(), "[outer, inner] fail 2");
    }
    #[test]
    fn test_from_result_vec_causes_ok() {
        let ok_result: Result<i32, Vec<Cause<&str, ()>>> = Ok(42);
        let valid = Valid::from(ok_result);
        assert_eq!(valid, Valid::succeed(42));
    }

    #[test]
    fn test_from_result_vec_causes_err() {
        let err_result: Result<i32, Vec<Cause<&str, ()>>> =
            Err(vec![Cause::new("error1"), Cause::new("error2")]);
        let valid = Valid::from(err_result);
        let expected = Valid::from(vec![Cause::new("error1"), Cause::new("error2")]);
        assert_eq!(valid, expected);
        assert!(valid.is_fail());
    }

    #[test]
    fn test_from_result_vec_causes_empty_err() {
        let err_result: Result<i32, Vec<Cause<&str, ()>>> = Err(vec![]);
        let valid = Valid::from(err_result);
        let expected = Valid::from(vec![]);
        assert_eq!(valid, expected);
        assert!(valid.is_fail());
    }
}