ValiCraft 1.0.5
dotnet add package ValiCraft --version 1.0.5
NuGet\Install-Package ValiCraft -Version 1.0.5
<PackageReference Include="ValiCraft" Version="1.0.5" />
<PackageVersion Include="ValiCraft" Version="1.0.5" />
<PackageReference Include="ValiCraft" />
paket add ValiCraft --version 1.0.5
#r "nuget: ValiCraft, 1.0.5"
#:package ValiCraft@1.0.5
#addin nuget:?package=ValiCraft&version=1.0.5
#tool nuget:?package=ValiCraft&version=1.0.5
ValiCraft
A high-performance validation library for .NET that uses source generators to craft fast, allocation-free validation logic at compile time.
Why ValiCraft?
ValiCraft takes a fundamentally different approach to validation. Instead of interpreting validation rules at runtime, ValiCraft uses C# source generators to analyze your validator definitions at compile time and generate highly optimized, native validation code.
Key Benefits:
- 🚀 Extreme Performance — Validation executes in nanoseconds with zero allocations for valid models
- 📦 Zero Runtime Overhead — No reflection, no expression tree compilation, no runtime code generation
- 🔧 Compile-Time Safety — Validation logic is generated and type-checked at build time
- ✨ Clean API — Fluent builder pattern with IntelliSense support
- 🔌 DI Ready — Source-generated dependency injection with no reflection
Quick Start
1. Define Your Model
public class User
{
public required string Username { get; set; }
public string? Email { get; set; }
public int Age { get; set; }
}
2. Create a Validator
using ValiCraft;
using ValiCraft.Attributes;
[GenerateValidator]
public partial class UserValidator : Validator<User>
{
protected override void DefineRules(IValidationRuleBuilder<User> builder)
{
builder.Ensure(x => x.Username)
.IsNotNullOrWhiteSpace()
.HasMinLength(3)
.HasMaxLength(50);
builder.Ensure(x => x.Email)
.IsNotNullOrWhiteSpace()
.IsEmailAddress();
builder.Ensure(x => x.Age)
.IsGreaterOrEqualThan(0)
.IsLessThan(150);
}
}
The [GenerateValidator] attribute tells the source generator to create the validation implementation. The partial keyword is required to allow the generated code to extend your type.
Optional Configuration:
The [GenerateValidator] attribute supports the following properties:
IncludeDefaultMetadata(default:false) — Whentrue, includes default metadata (RequestTypeandValidationCount) in the generatedValidationErrorsobject.
[GenerateValidator(IncludeDefaultMetadata = true)]
public partial class UserValidator : Validator<User>
{
// ...
}
3. Run Validation
var validator = new UserValidator();
var user = new User { Username = "john", Email = "john@example.com", Age = 30 };
ValidationErrors? result = validator.Validate(user);
if (result is null)
{
Console.WriteLine("User is valid!");
}
else
{
Console.WriteLine($"Validation failed: {result.Message}");
}
Performance
ValiCraft delivers exceptional performance compared to traditional validation libraries. Here's a snapshot (full benchmarks in docs/performance.md):
Simple Validation - Valid Model
| Method | Mean | Gen0 | Allocated |
|---|---|---|---|
| ValiCraft | 19.1820 ns | - | - |
| ValiCraftWithMetaData | 12.7780 ns | - | - |
| FluentValidation | 168.0000 ns | 0.1097 | 688 B |
Simple Validation - Invalid Model
| Method | Mean | Gen0 | Allocated |
|---|---|---|---|
| ValiCraft | 62.7240 ns | 0.0854 | 536 B |
| ValiCraftWithMetaData | 98.6020 ns | 0.1236 | 776 B |
| FluentValidation | 2,035.4290 ns | 1.0681 | 6,712 B |
Note: ValiCraft validators have near-zero instantiation cost (~3ns, 24B for the class instance) because the source generator produces validation logic that requires no runtime initialization. See full benchmark results for collection, complex, and instantiation benchmarks.
Installation
dotnet add package ValiCraft
Documentation
| Topic | Description |
|---|---|
| Core Concepts | Defining validators, running validation, and working with results |
| Built-in Rules | 50+ built-in rules for strings, numbers, collections, dates, and more |
| Advanced Features | Error customization, failure modes, conditional validation, collection/nested/polymorphic validation, async, static validators, and custom rules |
| Dependency Injection | AOT-friendly DI registration, service lifetimes, and multi-project solutions |
| Performance | Full benchmark results and comparisons |
Diagnostics
ValiCraft emits helpful compiler diagnostics when there are issues with your validators:
| Code | Description |
|---|---|
VALC201 |
Missing partial keyword on validator class |
VALC202 |
Missing Validator<T> or AsyncValidator<T> base class |
VALC203 |
Invalid rule invocation — use .Is() or a valid extension method |
VALC204 |
Invalid lambda in EnsureEach — expects a lambda as the last parameter |
VALC205 |
Cannot retrieve parameter name from lambda definition |
VALC206 |
Invalid builder argument used in scope |
VALC207 |
Missing [MapToValidationRule] attribute on extension method |
VALC208 |
Invalid statement in DefineRules — only builder invocations are allowed |
VALC301 |
Static validator has parameterized constructor |
VALC302 |
Static validator has instance field |
VALC303 |
Static validator has instance property |
VALC304 |
Static validator has instance method |
VALC402 |
Disallowed RunValidation call — RunValidation is for internal use by generated code only |
These diagnostics appear directly in your IDE with exact source locations.
Requirements
- .NET 8.0 or higher
- C# 12 or higher
License
MIT — see LICENSE for details.
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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 is compatible. 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 is compatible. 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. |
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.5 | 85 | 3/21/2026 |
| 1.0.3 | 76 | 3/20/2026 |
| 0.30.0 | 93 | 3/12/2026 |
| 0.29.0 | 89 | 3/12/2026 |
| 0.28.0 | 95 | 3/12/2026 |
| 0.27.0 | 87 | 3/11/2026 |
| 0.26.0 | 93 | 2/24/2026 |
| 0.25.0 | 84 | 2/23/2026 |
| 0.24.0 | 99 | 2/22/2026 |
| 0.23.0 | 98 | 2/21/2026 |
| 0.22.0 | 110 | 2/10/2026 |
| 0.21.0 | 91 | 2/9/2026 |
| 0.20.0 | 95 | 2/9/2026 |
| 0.17.0 | 119 | 1/1/2026 |
| 0.16.0 | 106 | 1/1/2026 |
| 0.15.0 | 117 | 12/31/2025 |
| 0.13.0 | 105 | 12/31/2025 |
| 0.12.0 | 132 | 12/21/2025 |
| 0.11.0 | 180 | 11/23/2025 |
| 0.10.0 | 178 | 7/13/2025 |