🧠 Intelligent Context

Smart Validation System

EasyValidate's intelligent validation context system provides automatic dependency injection and type-safe access to current model instances. Build sophisticated validation logic with compile-time safety and analyzer enforcement.

4Examples
4Features

Smart Validation in Action

See how EasyValidate's intelligent context system provides automatic dependency injection and type-safe model access.

Automatic Service Provider Injection
EasyValidate automatically injects IServiceProvider into any property of your validation attributes.
Auto-InjectionAny Property NameMultiple ServicesZero Configuration
ValidationContext for Current Instance
Access the current model instance using ValidationContextAttribute with compile-time type enforcement.
ValidationContextInterface EnforcementBusiness LogicType Safety
Model with Interface Implementation
Models must implement interfaces used in ValidationContext properties - enforced by analyzers.
Interface ImplementationAnalyzer EnforcementReal UsageDependency Injection
Multiple ValidationContext Properties
Use multiple ValidationContext properties to access different aspects of the current instance.
Multiple ContextsInterface CompositionComplex ValidationType Safety

Smart System Features

EasyValidate's intelligent features make building complex validation logic simple and type-safe.

Automatic Service Provider Detection
EasyValidate automatically detects and injects IServiceProvider properties in validation attributes.
Any Property NameZero ConfigurationRuntime InjectionService Resolution
Smart ValidationContext Injection
Access current model instances through ValidationContextAttribute with compile-time type enforcement.
Type SafetyInterface EnforcementAnalyzer SupportMultiple Contexts
Compile-Time Analyzer Enforcement
Analyzers ensure models implement all interfaces used in ValidationContext properties.
Compile-Time ErrorsInterface ValidationType CheckingDeveloper Guidance
Flexible Architecture
Combine service injection and context access for powerful validation scenarios.
Service + ContextBusiness LogicExternal ServicesComplex Scenarios

Analyzer Enforcement

EasyValidate analyzers enforce type safety and guide developers to correct implementations.

Missing Interface Implementation
When a model doesn't implement an interface used in ValidationContext
// ❌ This will cause compiler error
public class Product : IGenerate  // Missing IProductContext!
{
    [ValidatePrice]  // Uses ValidationContext with IProductContext
    public decimal Price { get; set; }
}

// ✅ Correct implementation
public class Product : IGenerate, IProductContext
{
    [ValidatePrice]
    public decimal Price { get; set; }
    
    public string Category { get; set; }  // Required by IProductContext
    public bool IsOnSale { get; set; }    // Required by IProductContext
}

Analyzer Message:

Model must implement IProductContext interface required by ValidationContext in ValidatePriceAttribute

Type Mismatch
When ValidationContext property type doesn't match model capabilities
public class InvalidValidationAttribute : Attribute
{
    [ValidationContext]
    public ISomeInterface? Context { get; set; }  // Model must implement this
}

// ❌ Model doesn't implement ISomeInterface
public class MyModel : IGenerate
{
    [InvalidValidation]
    public string Value { get; set; }
}

Analyzer Message:

MyModel must implement or inherit ISomeInterface to use InvalidValidationAttribute