Validator

Trait Validator 

Source
pub trait Validator<A, E, T>: Sized {
Show 15 methods // Required methods fn is_succeed(&self) -> bool; fn is_fail(&self) -> bool; fn to_result(self) -> Result<A, Vec<Cause<E, T>>>; // Provided methods fn map<A1>(self, f: impl FnOnce(A) -> A1) -> Valid<A1, E, T> { ... } fn foreach(self, f: impl FnMut(A)) -> Valid<A, E, T> where A: Clone { ... } fn and<A1>(self, other: Valid<A1, E, T>) -> Valid<A1, E, T> { ... } fn zip<A1>(self, other: Valid<A1, E, T>) -> Valid<(A, A1), E, T> { ... } fn fuse<A1>(self, other: Valid<A1, E, T>) -> Fusion<(A, A1), E, T> { ... } fn trace(self, trace: impl Into<T> + Clone) -> Valid<A, E, T> { ... } fn fold<A1>( self, ok: impl FnOnce(A) -> Valid<A1, E, T>, err: impl FnOnce() -> Valid<A1, E, T>, ) -> Valid<A1, E, T> { ... } fn and_then<B>(self, f: impl FnOnce(A) -> Valid<B, E, T>) -> Valid<B, E, T> { ... } fn unit(self) -> Valid<(), E, T> { ... } fn some(self) -> Valid<Option<A>, E, T> { ... } fn map_to<B>(self, b: B) -> Valid<B, E, T> { ... } fn when(self, f: impl FnOnce() -> bool) -> Valid<(), E, T> { ... }
}
Expand description

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.

Required Methods§

Source

fn is_succeed(&self) -> bool

Returns true if the validation is successful.

Source

fn is_fail(&self) -> bool

Returns true if the validation contains errors.

Source

fn to_result(self) -> Result<A, Vec<Cause<E, T>>>

Converts the validation into a Result.

Provided Methods§

Source

fn map<A1>(self, f: impl FnOnce(A) -> A1) -> Valid<A1, E, T>

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()));
Source

fn foreach(self, f: impl FnMut(A)) -> Valid<A, E, T>
where A: Clone,

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);
Source

fn and<A1>(self, other: Valid<A1, E, T>) -> Valid<A1, E, T>

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"));
Source

fn zip<A1>(self, other: Valid<A1, E, T>) -> Valid<(A, A1), E, T>

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")));
Source

fn fuse<A1>(self, other: Valid<A1, E, T>) -> Fusion<(A, A1), E, T>

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]));
Source

fn trace(self, trace: impl Into<T> + Clone) -> Valid<A, E, T>

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");
Source

fn fold<A1>( self, ok: impl FnOnce(A) -> Valid<A1, E, T>, err: impl FnOnce() -> Valid<A1, E, T>, ) -> Valid<A1, E, T>

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));
Source

fn and_then<B>(self, f: impl FnOnce(A) -> Valid<B, E, T>) -> Valid<B, 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));
Source

fn unit(self) -> Valid<(), E, T>

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(()));
Source

fn some(self) -> Valid<Option<A>, E, T>

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)));
Source

fn map_to<B>(self, b: B) -> Valid<B, E, T>

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"));
Source

fn when(self, f: impl FnOnce() -> bool) -> Valid<(), E, T>

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(()));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<A, E, T> Validator<A, E, T> for Fusion<A, E, T>

Source§

impl<A, E, T> Validator<A, E, T> for Valid<A, E, T>