Skip to main content

arolariu.Backend.Common.Validators

arolariu.Backend.Common

arolariu.Backend.Common.Validators Namespace

Classes

Validator Class

Provides generic validation utilities for objects with custom predicate logic and exception handling. This static class enables type-safe validation with configurable exception types.

public static class Validator

Inheritance System.Object 🡒 Validator

Example

// Validate a non-null object
Validator.ValidateAndThrow<string, ArgumentNullException>(
value,
v => !string.IsNullOrEmpty(v),
"Value cannot be null or empty");

// Validate business rules
Validator.ValidateAndThrow<Order, InvalidOperationException>(
order,
o => o.Total > 0,
"Order total must be greater than zero");

Remarks

The Validator class supports: - Generic object validation with custom predicates - Configurable exception types for different validation scenarios - Null safety with built-in null checks - Dynamic exception creation with custom messages

Methods

Validator.ValidateAndThrow<TObject,TException>(TObject, Func<TObject,bool>, string) Method

Validates an object using custom logic and throws a specified exception type on failure. This is the main public entry point for object validation with configurable exception handling.

public static void ValidateAndThrow<TObject,TException>(TObject? @object, System.Func<TObject?,bool> predicate, string message)
where TException : System.Exception, new();

Type parameters

TObject

The type of object being validated.

TException

The exception type to throw on validation failure. Must inherit from Exception and have a string constructor.

Parameters

object TObject

The object to validate against the predicate.

predicate System.Func<TObject,System.Boolean>

A function that defines the validation logic. Returns true if valid, false otherwise.

message System.String

The error message to use when creating the exception.

Exceptions

System.ArgumentNullException
Thrown when predicate is null.

Example

// Parameter validation
Validator.ValidateAndThrow<string, ArgumentException>(
email,
e => e.Contains("@"),
"Invalid email format");

// Business rule validation
Validator.ValidateAndThrow<User, InvalidOperationException>(
user,
u => u.IsActive,
"User account is not active");

// Range validation
Validator.ValidateAndThrow<int, ArgumentOutOfRangeException>(
age,
a => a >= 0 and a less than 120,
"Age must be between 0 and 120");

Remarks

This method enables flexible validation scenarios: - Input parameter validation with ArgumentException - Business rule validation with custom exceptions - State validation with InvalidOperationException - Security validation with UnauthorizedAccessException

Validator.ValidateObjectAndThrow<TObject,TException>(TObject, Func<TObject,bool>, string) Method

Validates an object against a predicate and throws a specified exception type if validation fails. This method performs null checking before applying the custom validation logic.

private static void ValidateObjectAndThrow<TObject,TException>(TObject? @object, System.Func<TObject?,bool> predicate, string message)
where TException : System.Exception, new();

Type parameters

TObject

The type of object to validate.

TException

The type of exception to throw on validation failure. Must have a constructor that accepts a string message.

Parameters

object TObject

The object to validate. Can be null.

predicate System.Func<TObject,System.Boolean>

The validation logic to apply. Must not be null.

message System.String

The error message to include in the exception if validation fails.

Exceptions

System.ArgumentNullException
Thrown when predicate is null.

Remarks

Validation logic: 1. Checks if the object is null 2. If not null, applies the predicate function 3. Throws TException with the provided message if either check fails 4. Uses Activator.CreateInstance to create the exception dynamically

// was this page useful?