Lite.Validation
0.3.0-alpha
dotnet add package Lite.Validation --version 0.3.0-alpha
NuGet\Install-Package Lite.Validation -Version 0.3.0-alpha
<PackageReference Include="Lite.Validation" Version="0.3.0-alpha" />
<PackageVersion Include="Lite.Validation" Version="0.3.0-alpha" />
<PackageReference Include="Lite.Validation" />
paket add Lite.Validation --version 0.3.0-alpha
#r "nuget: Lite.Validation, 0.3.0-alpha"
#:package Lite.Validation@0.3.0-alpha
#addin nuget:?package=Lite.Validation&version=0.3.0-alpha&prerelease
#tool nuget:?package=Lite.Validation&version=0.3.0-alpha&prerelease
Lite.Validation
Библиотека валидации для .NET, заточенная под производительность и минимальное давление на GC. Fluent API, опциональный source generator, интеграции с ASP.NET Core (MVC, FastEndpoints) и DI.
NuGet: Lite.Validation
Бенчмарки (статья с цифрами): BENCHMARKS.md
Зачем это нужно
Валидация в веб-API вызывается на каждый запрос. В высоконагруженных сервисах это hot path: лишние наносекунды и аллокации складываются в миллисекунды и в постоянную нагрузку на сборщик мусора. Классические решения вроде FluentValidation или DataAnnotations удобны, но на каждый вызов тянут за собой рефлексию, скомпилированные делегаты, аллокации под результат и коллекции ошибок — в десятки раз больше времени и памяти, чем минимально необходимо.
Lite.Validation решает эту задачу иначе:
- Source generator генерирует код валидации на этапе компиляции. Никакой рефлексии и
Expression.Compileв рантайме — только прямой код, который JIT хорошо инлайнит. - Нулевые аллокации на успешной валидации: результат — структура, список ошибок создаётся только при наличии ошибок.
- Тот же привычный fluent-подход — правила описываются в коде через
RuleFor, цепочки правил, условия, вложенные валидаторы. Можно начать с runtime-варианта (LiteValidator+ билдер) и позже перейти на source-generated без смены API.
Итог: в бенчмарках при тех же правилах мы обходим FluentValidation по времени в десятки раз и многократно снижаем аллокации и число Gen0-сборок на 10 000 запросов. Подробные цифры, таблицы и комментарии — в BENCHMARKS.md.
Пакеты
| Пакет | Описание |
|---|---|
| Lite.Validation | Ядро: IValidator<T>, FluentValidator<T>, LiteValidator<T>, встроенные правила. |
| Lite.Validation.SourceGenerator | Roslyn source generator: генерация Validate()/ValidateAsync() из FluentValidator<T> при компиляции. |
| Lite.Validation.Rules.Inline | Дополнительные inline-правила (подключается ядром). |
| Lite.Validation.Integration.DependencyInjection | AddLiteValidatorsFromAssembly() и регистрация в IServiceCollection. |
| Lite.Validation.Integration.AspNetCore.Mvc | Интеграция с ASP.NET Core MVC. |
| Lite.Validation.Integration.AspNetCore.FastEndpoints | Интеграция с FastEndpoints. |
Быстрый старт
Вариант с ручной конфигурацией (LiteValidator)
Подходит, когда валидатор создаётся вручную или через DI с передачей билдера. Правила задаются в конструкторе.
public partial class CreateOrderValidator : LiteValidator<CreateOrderRequest>
{
public CreateOrderValidator(ValidationBuilder<CreateOrderRequest> b) : base(b)
{
b.RuleFor(x => x.ProductName)
.NotNull().WithDetails("Product name is required")
.NotEmpty().WithDetails("Product name must not be empty");
b.RuleFor(x => x.Quantity)
.GreaterThan(0).WithDetails("Quantity must be positive");
}
}
Вариант с source generator (FluentValidator)
Подключи пакет Lite.Validation.SourceGenerator. Правила описываются в статическом Configure; генератор создаёт реализацию валидатора в compile time — без рефлексии и лишних аллокаций.
public partial class OrderFluentValidator : FluentValidator<CreateOrderRequest>
{
static void Configure(ValidationBuilder<CreateOrderRequest> b)
{
b.RuleFor(x => x.ProductName).NotNull().NotEmpty();
b.RuleFor(x => x.Quantity).GreaterThan(0);
}
}
Регистрация в DI
services.AddLiteValidatorsFromAssemblyOf<OrderFluentValidator>(ServiceLifetime.Singleton);
Сборка и тесты
dotnet build Lite.Validation.sln
dotnet test Lite.Validation.sln --no-build
Бенчмарки
Подробная статья с замерами против FluentValidation и DataAnnotations, разбором по одному запросу и по 10 000 запросов (время, аллокации, оценка Gen0): BENCHMARKS.md.
Запуск бенчмарков локально:
dotnet run -c Release --project benchmarks/Lite.Validation.Benchmarks -- --filter "*SimpleValidation*"
dotnet run -c Release --project benchmarks/Lite.Validation.Benchmarks -- --filter "*HighVolume*"
Отчёты (Markdown/HTML) сохраняются в BenchmarkDotNet.Artifacts/results/.
Разработка: окружение и хуки
Установка (mise, Python venv, pre-commit, dotnet tools):
- Windows (PowerShell):
.\install.ps1 - Linux/macOS:
./install.sh(при необходимости:chmod +x install.sh)
При коммите запускаются форматтер (CSharpier) и сборка; при пуше — сборка и тесты.
Проверка хуков:
ls .git/hooks/pre-commit .git/hooks/pre-push
Ручной прогон:
pre-commit run --all-files
pre-commit run --hook-stage push --all-files
Если хуки не срабатывают: из корня выполни pre-commit install и pre-commit install --hook-type pre-push.
В .vscode/ — рекомендуемые расширения и настройки (CSharpier, format on save).
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Lite.Validation.Rules.Inline (>= 0.3.0-alpha)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Lite.Validation:
| Package | Downloads |
|---|---|
|
Lite.Validation.Integration.AspNetCore.FastEndpoints
FastEndpoints integration for Lite.Validation. Register validators and validate request DTOs with FastEndpoints. |
|
|
Lite.Validation.Integration.DependencyInjection
Microsoft.Extensions.DependencyInjection integration for Lite.Validation. AddLiteValidatorsFromAssembly() to register LiteValidator and FluentValidator subclasses. |
|
|
Lite.Validation.Integration.AspNetCore.Mvc
ASP.NET Core MVC integration for Lite.Validation. Automatic model validation and filter support. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.3.0-alpha | 115 | 3/16/2026 |