Class ValidatorRuleSet<T>

Namespace
REslava.Result
Assembly
REslava.Result.dll

Immutable collection of validation rules for a specific entity type. Provides methods to validate entities against all rules in the set.

public class ValidatorRuleSet<T>

Type Parameters

T

The type of entity to validate.

Inheritance
ValidatorRuleSet<T>
Inherited Members
Extension Methods

Examples

var ruleSet = new ValidatorRuleBuilder<User>()
    .Rule(u => u.Email, "Required", "Email required", email => !string.IsNullOrEmpty(email))
    .Rule(u => u.Age, "MinAge", "Must be 18+", age => age >= 18)
    .Build();

var result = ruleSet.Validate(user);
if (result.IsValid)
{
    Console.WriteLine("User is valid");
}
else
{
    foreach (var error in result.ValidationErrors)
    {
        Console.WriteLine($"Error: {error.Message}");
    }
}

Constructors

ValidatorRuleSet(ImmutableList<IValidatorRule<T>>)

Initializes a new instance of the ValidatorRuleSet with the specified rules.

public ValidatorRuleSet(ImmutableList<IValidatorRule<T>> rules)

Parameters

rules ImmutableList<IValidatorRule<T>>

The immutable collection of validation rules.

Properties

Count

Gets the number of validation rules in this set.

public int Count { get; }

Property Value

int

Rules

Gets a read-only collection of all validation rules in this set.

public IReadOnlyList<IValidatorRule<T>> Rules { get; }

Property Value

IReadOnlyList<IValidatorRule<T>>

Methods

Validate(T)

Validates the specified entity against all rules in this set. Stops at first failure (fail-fast approach).

public ValidationResult<T> Validate(T entity)

Parameters

entity T

The entity to validate.

Returns

ValidationResult<T>

A ValidationResult indicating success or failure.

Examples

var result = ruleSet.Validate(user);
if (result.IsValid)
{
    Console.WriteLine("All validations passed");
}
else
{
    Console.WriteLine($"Validation failed: {result.ValidationErrors.First().Message}");
}

ValidateAll(T)

Validates the entity and collects all validation errors (not fail-fast).

public ValidationResult<T> ValidateAll(T entity)

Parameters

entity T

The entity to validate.

Returns

ValidationResult<T>

A ValidationResult with all validation errors.

Examples

var result = ruleSet.ValidateAll(user);
if (result.IsValid)
{
    Console.WriteLine("All validations passed");
}
else
{
    Console.WriteLine($"Found {result.ValidationErrors.Count} validation errors:");
    foreach (var error in result.ValidationErrors)
    {
        Console.WriteLine($"- {error.Message}");
    }
}

ValidateAsync(T)

Validates the specified entity against all rules in this set asynchronously.

public Task<ValidationResult<T>> ValidateAsync(T entity)

Parameters

entity T

The entity to validate.

Returns

Task<ValidationResult<T>>

A ValidationResult indicating success or failure.

Examples

var result = await ruleSet.ValidateAsync(user);
if (result.IsValid)
{
    Console.WriteLine("All validations passed");
}
else
{
    Console.WriteLine($"Validation failed: {result.ValidationErrors.First().Message}");
}