A lightweight and high-performance DataAnnotations validation extension for .NET.
This library provides a set of reusable property relationship validation attributes such as:
- Property comparison (
GreaterThan,LessThan,EqualTo, etc.) - Range validation (
Between,Within) - Time rules (
GreaterThanNow,BetweenNowAnd) - Conditional validation (
RequiredIf,RequiredIfAny, etc.)
It is designed to work with:
- ASP.NET Core
- WPF / MVVM
- WinForms
- Any project using
System.ComponentModel.DataAnnotations
• Compare values between properties
• Support nested property paths (Order.CreateTime)
• Null-safe property access (obj?.Order?.CreateTime)
• Cross-type numeric comparison (int, long, decimal, etc.)
• Enum comparison support
• High-performance compiled expression getter cache
• High-performance comparison delegate cache
• Works with standard DataAnnotations validation pipeline
Currently available as source integration.
Clone the repository and include the project in your solution:
git clone https://github.com/apachezy/ValidationRelations.NET.gitOr copy the ValidationRelations project into your solution.
Example model:
public class TimeRange
{
public DateTime Start { get; set; }
[GreaterThan(nameof(Start))]
public DateTime End { get; set; }
}Validation rule:
Start < End
public class RangeModel
{
public int Min { get; set; }
public int Max { get; set; }
[Between(nameof(Min), nameof(Max))]
public int Value { get; set; }
}Rule:
Min <= Value <= Max
Nested property paths are supported.
public class Shipment
{
public Order Order { get; set; }
[GreaterThan("Order.CreateTime")]
public DateTime ShipTime { get; set; }
}[GreaterThanNow]
public DateTime ExpireTime { get; set; }
[LessThanNow]
public DateTime CreatedTime { get; set; }
[BetweenNowAnd(nameof(ExpireTime))]
public DateTime ExecuteTime { get; set; }Require a property based on other properties.
[RequiredIf(nameof(Type), OrderType.Special)]
public string SpecialCode { get; set; }More rules:
RequiredIf
RequiredIfNot
RequiredIfAny
RequiredIfAll
GreaterThan
GreaterOrEqual
LessThan
LessOrEqual
EqualTo
NotEqualTo
DistinctFrom
Between
NotBetween
Within
BetweenNowAnd
GreaterThanNow
LessThanNow
EqualToAny
RequiredIf
RequiredIfNot
RequiredIfAny
RequiredIfAll
Utilities.Validation
│
├─ PropertyComparison
│ ├─ Attributes
│ ├─ Infrastructure
│ └─ Comparison helpers
│
└─ Conditional
└─ Conditional validation attributes
MIT License
Issues and pull requests are welcome.