Class PredicateValidatorRule<T, TProperty>

Namespace
REslava.Result
Assembly
REslava.Result.dll

A validation rule that uses a predicate function to validate a property of an entity. Supports lambda expressions for concise rule definitions.

public class PredicateValidatorRule<T, TProperty> : IValidatorRuleSync<T>, IValidatorRule<T>

Type Parameters

T

The type of entity to validate.

TProperty

The type of the property being validated.

Inheritance
PredicateValidatorRule<T, TProperty>
Implements
Inherited Members
Extension Methods

Examples

// Create a rule using lambda
var rule = new PredicateValidatorRule<User, string>(
    u => u.Email,
    "EmailRequired",
    "Email is required",
    email => !string.IsNullOrEmpty(email)
);

// Use the rule
var result = rule.Validate(user);

Constructors

PredicateValidatorRule(Func<T, TProperty>, string, string, Func<TProperty, bool>)

Initializes a new instance of the PredicateValidatorRule.

public PredicateValidatorRule(Func<T, TProperty> propertySelector, string ruleName, string errorMessage, Func<TProperty, bool> validator)

Parameters

propertySelector Func<T, TProperty>

Function to select the property to validate.

ruleName string

The name of the validation rule.

errorMessage string

The error message if validation fails.

validator Func<TProperty, bool>

The validation predicate function.

Examples

var rule = new PredicateValidatorRule<User, string>(
    u => u.Email,
    "EmailRequired",
    "Email is required",
    email => !string.IsNullOrEmpty(email)
);

Properties

ErrorMessage

Gets the default error message for this validation rule.

public string ErrorMessage { get; }

Property Value

string

Name

Gets the name of the validation rule.

public string Name { get; }

Property Value

string

Methods

Validate(T)

Validates the specified entity using the predicate function.

public ValidationResult<T> Validate(T entity)

Parameters

entity T

The entity to validate.

Returns

ValidationResult<T>

A ValidationResult indicating success or failure.

Examples

var user = new User("", 25);
var result = rule.Validate(user);

if (result.IsValid)
{
    Console.WriteLine("Validation passed");
}
else
{
    Console.WriteLine($"Validation failed: {result.ValidationErrors.First().Message}");
}