Skip to main content

Crate fluentval

Crate fluentval 

Source
Expand description

FluentVal - A fluent validation library for Rust

This library provides a builder pattern for creating validators in a readable, chainable style, inspired by FluentValidation in .NET.

§Example

use fluentval::{ValidatorBuilder, RuleBuilder, Validator};

struct User {
    name: String,
    email: String,
    age: i32,
}

let validator = ValidatorBuilder::<User>::new()
    .rule_for("name", |u| &u.name,
        RuleBuilder::for_property("name")
            .not_empty(None)
            .min_length(2, None))
    .rule_for("email", |u| &u.email,
        RuleBuilder::for_property("email")
            .email(None))
    .rule_for("age", |u| &u.age,
        RuleBuilder::for_property("age")
            .greater_than_or_equal(18, Some("Must be 18 or older")))
    .build();

let user = User { name: "".into(), email: "invalid".into(), age: 15 };
let result = validator.validate(&user);

if !result.is_valid() {
    for error in result.errors() {
        println!("{}: {}", error.property, error.message);
    }
}

Structs§

RuleBuilder
Builder for creating validation rules in a fluent style
ValidationError
Represents a validation error with a property name and error message
ValidationResult
Result of validation containing errors if validation failed
ValidatorBuilder
Helper struct to build validators in a fluent style

Traits§

Numeric
Trait for types that can be treated as numeric values
OptionLike
Trait for types that can be treated as Option-like
Validator
Trait for defining validators

Functions§

validate
Helper function to validate an instance with a validator

Type Aliases§

Rule
Rule function type that validates a value and returns an optional error message