#error #valid #first #success #validation #input #tailcall

tailcall-valid

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.

2 releases

new 0.1.1 Oct 17, 2024
0.1.0 Oct 17, 2024

#190 in Configuration

Download history 310/week @ 2024-10-14

310 downloads per month

Apache-2.0

24KB
629 lines

tailcall-valid

This crate helps to collect all possible errors instead of returning the first error encountered. This is useful when you want to know all the errors in a single go.

Valid could be initiated with an Option, Result, success, or a failure.

Examples

From success

use tailcall_valid::*;

fn main() {
    let result: Valid<i32, &str> = Valid::succeed(1);
    assert_eq!(result, Valid::succeed(1));
}

From failure

use tailcall_valid::*;

fn main() {
    let err = "Expected a value";
    let result: Valid<i32, &str> = Valid::fail(err);
    assert_eq!(result, Valid::from_vec_cause(vec![Cause::new(err)]));
}

From Option

use tailcall_valid::*;

fn main() {
    // Case when Option is None
    let err = "Expected a value";
    let option: Option<i32> = None;
    let result = Valid::from_option(option, err);
    assert_eq!(result, Valid::from_vec_cause(vec![Cause::new(err)]));

    // Case when Option is Some
    let option: Option<i32> = Some(1);
    let result = Valid::from_option(option, err);
    assert_eq!(result, Valid::succeed(1));
}

From Result

use tailcall_valid::*;

fn main() {
    // Case when Result is Err
    let err = "Expected a value";
    let result: Result<i32, &str> = Err(err);
    let result = result.map_err(ValidationError::new);
    let result = Valid::from(result);
    assert_eq!(result, Valid::from_vec_cause(vec![Cause::new(err)]));

    // Case when Result is Ok
    let result: Result<i32, &str> = Ok(1);
    let result = result.map_err(ValidationError::new);
    let result = Valid::from(result);
    assert_eq!(result, Valid::succeed(1));
}

Dependencies

~5–7MB
~127K SLoC