CodingFlow.FluentValidation.VogenExtensions
0.3.0
dotnet add package CodingFlow.FluentValidation.VogenExtensions --version 0.3.0
NuGet\Install-Package CodingFlow.FluentValidation.VogenExtensions -Version 0.3.0
<PackageReference Include="CodingFlow.FluentValidation.VogenExtensions" Version="0.3.0" />
<PackageVersion Include="CodingFlow.FluentValidation.VogenExtensions" Version="0.3.0" />
<PackageReference Include="CodingFlow.FluentValidation.VogenExtensions" />
paket add CodingFlow.FluentValidation.VogenExtensions --version 0.3.0
#r "nuget: CodingFlow.FluentValidation.VogenExtensions, 0.3.0"
#:package CodingFlow.FluentValidation.VogenExtensions@0.3.0
#addin nuget:?package=CodingFlow.FluentValidation.VogenExtensions&version=0.3.0
#tool nuget:?package=CodingFlow.FluentValidation.VogenExtensions&version=0.3.0
CodingFlow Fluent Validation
Minimal, easy to use fluent validations API inspired by FluentValidation.
When you need to validate any type, even primitives in an easy and direct way, this library fits the bill. FluentValidation by Jeremy Skinner requires creating a separate validator class to register validation rules, and then instantiating the validator class. This library on the other hand, let's you add validation directly. This library is also ~ 20% faster performance-wise (See performance benchmark).
Usage
After installing the nuget package from Nuget.org, add this using statement to the file where you want to validate:
<a id='snippet-StandardUsing'></a>
using static CodingFlow.FluentValidation.Validations;
<sup><a href='/Examples/BasicExample.cs#L2-L4' title='Snippet source file'>snippet source</a> | <a href='#snippet-StandardUsing' title='Start of snippet'>anchor</a></sup>
Then you can add validation like this:
<a id='snippet-BasicExample'></a>
var input = 11;
var result = RuleFor(input)
.BetweenInclusive(4, 6)
.Result();
// Check results
bool isValid = result.IsValid;
var errors = result.Errors;
<sup><a href='/Examples/BasicExample.cs#L12-L22' title='Snippet source file'>snippet source</a> | <a href='#snippet-BasicExample' title='Start of snippet'>anchor</a></sup>
Validators
There are several built-in validators available out-of-the-box. You can also provide your own validation logic via the predicate validator (aka Must).
NotEmpty Validator
Ensures the value is not null for reference types or a default value for value types. For strings, ensures it is not null, an empty string, or only whitespace.
<a id='snippet-NotEmptyExample'></a>
RuleFor(input)
.NotEmpty()
.Result();
<sup><a href='/Examples/NotEmptyExample.cs#L12-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-NotEmptyExample' title='Start of snippet'>anchor</a></sup>
BetweenInclusive Validator
Ensures a number of any type (int, float, double, etc.) is greater than or equal to a minimum and less than or equal to a maximum.
<a id='snippet-BetweenInclusiveExample'></a>
RuleFor(input)
.BetweenInclusive(6, 14)
.Result();
<sup><a href='/Examples/BetweenInclusiveExample.cs#L12-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-BetweenInclusiveExample' title='Start of snippet'>anchor</a></sup>
BetweenExclusive Validator
Ensures a number of any type (int, float, double, etc.) is greater than a minimum and less than a maximum.
<a id='snippet-BetweenExclusiveExample'></a>
RuleFor(input)
.BetweenExclusive(6, 14)
.Result();
<sup><a href='/Examples/BetweenExclusiveExample.cs#L12-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-BetweenExclusiveExample' title='Start of snippet'>anchor</a></sup>
Equal Validator
Ensures the input is considered equal to the provided value. For reference types it checks if the two references are to the same instance (reference equality). For value types, it checks it the types and values are the same (value equality).
<a id='snippet-EqualExample'></a>
RuleFor(input)
.Equal(8)
.Result();
<sup><a href='/Examples/EqualExample.cs#L12-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-EqualExample' title='Start of snippet'>anchor</a></sup>
MinimumLength Validator
Ensures the string has a minimum length.
<a id='snippet-MinimumLengthExample'></a>
RuleFor(input)
.MinimumLength(5) // Must be at least 5 characters long.
.Result();
<sup><a href='/Examples/MinimumLengthExample.cs#L12-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-MinimumLengthExample' title='Start of snippet'>anchor</a></sup>
MaximumLength Validator
Ensures the string has a maximum length.
<a id='snippet-MaximumLengthExample'></a>
RuleFor(input)
.MaximumLength(5) // Must be at most 5 characters long.
.Result();
<sup><a href='/Examples/MaximumLengthExample.cs#L12-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-MaximumLengthExample' title='Start of snippet'>anchor</a></sup>
Regular Expression Validator
Aka Matches, ensure the string passes a regular expression test.
<a id='snippet-MatchesExample'></a>
RuleFor(input)
.Matches("cat")
.Result();
<sup><a href='/Examples/MatchesExample.cs#L12-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-MatchesExample' title='Start of snippet'>anchor</a></sup>
Predicate Validator
The predicate (aka Must) validator allows you to provide your own validation logic by providing a delegate.
<a id='snippet-MustExample'></a>
RuleFor(input)
.Must(input => input == 7)
.Result();
<sup><a href='/Examples/MustExample.cs#L12-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-MustExample' title='Start of snippet'>anchor</a></sup>
IsGuid Validator
Ensures the string can be parsed into a valid GUID.
<a id='snippet-IsGuidExample'></a>
RuleFor(input)
.IsGuid()
.Result();
<sup><a href='/Examples/IsGuidExample.cs#L12-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-IsGuidExample' title='Start of snippet'>anchor</a></sup>
Customizing
Custom Error Messages
The WithMessage method can be used to change the validation error message for a validator.
<a id='snippet-WithMessageExample'></a>
RuleFor(input)
.Equal(8).WithMessage("The two numbers are not equal.")
.Result();
<sup><a href='/Examples/WithMessageExample.cs#L13-L17' title='Snippet source file'>snippet source</a> | <a href='#snippet-WithMessageExample' title='Start of snippet'>anchor</a></sup>
Integrations
Vogen
Extensions to integrate with Vogen validation methods.
To get started, install the Vogen extensions nuget package, CodingFlow.FluentValidation.VogenExtensions.
To get the final result of the fluent validation chain, call VogenResult() instead of Result():
<a id='snippet-VogenExample'></a>
[ValueObject]
public readonly partial struct Age
{
public static Validation Validate(int value)
{
return RuleFor(value)
.BetweenInclusive(0, 200)
.VogenResult();
}
}
<sup><a href='/Examples/VogenExamples/Age.cs#L8-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-VogenExample' title='Start of snippet'>anchor</a></sup>
Performance Benchmark Comparison with FluentValidation
Benchmark of inclusive between validator shows this library is ~ 20 - 25% faster than FluentValidation in .NET 9 and .NET 10.
BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.7462/25H2/2025Update/HudsonValley2)
Intel Core Ultra 5 245KF 4.20GHz, 1 CPU, 14 logical and 14 physical cores
.NET SDK 10.0.101
[Host] : .NET 10.0.1 (10.0.1, 10.0.125.57005), X64 RyuJIT x86-64-v3
.NET 10.0 : .NET 10.0.1 (10.0.1, 10.0.125.57005), X64 RyuJIT x86-64-v3
.NET 8.0 : .NET 8.0.22 (8.0.22, 8.0.2225.52707), X64 RyuJIT x86-64-v3
.NET 9.0 : .NET 9.0.11 (9.0.11, 9.0.1125.51716), X64 RyuJIT x86-64-v3
| Method | Job | Runtime | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|---|---|---|---|---|---|---|---|---|
| CodingFlow | .NET 10.0 | .NET 10.0 | 34.70 ns | 0.338 ns | 0.316 ns | 0.0235 | - | 296 B |
| FluentValidation | .NET 10.0 | .NET 10.0 | 46.42 ns | 0.228 ns | 0.213 ns | 0.0471 | 0.0001 | 592 B |
| CodingFlow | .NET 8.0 | .NET 8.0 | 47.44 ns | 0.538 ns | 0.503 ns | 0.0287 | - | 360 B |
| FluentValidation | .NET 8.0 | .NET 8.0 | 52.02 ns | 0.387 ns | 0.323 ns | 0.0471 | 0.0001 | 592 B |
| CodingFlow | .NET 9.0 | .NET 9.0 | 37.48 ns | 0.149 ns | 0.139 ns | 0.0287 | - | 360 B |
| FluentValidation | .NET 9.0 | .NET 9.0 | 48.27 ns | 0.499 ns | 0.467 ns | 0.0471 | 0.0001 | 592 B |
Benchmark code:
<a id='snippet-PerformanceBenchmarkBetweenInclusive'></a>
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net10_0)]
[SimpleJob(RuntimeMoniker.Net90)]
[SimpleJob(RuntimeMoniker.Net80)]
public class BetweenBenchmark
{
private readonly int input = 5;
private readonly IntegerValidator validator = new();
[Benchmark]
public bool CodingFlow()
{
return RuleFor(input)
.BetweenInclusive(1, 7)
.Result()
.IsValid;
}
[Benchmark]
public bool FluentValidation()
{
return validator.Validate(input)
.IsValid;
}
}
<sup><a href='/PerformanceBenchmarks/BetweenBenchmark.cs#L8-L35' title='Snippet source file'>snippet source</a> | <a href='#snippet-PerformanceBenchmarkBetweenInclusive' title='Start of snippet'>anchor</a></sup>
FluentValidation validator:
<a id='snippet-IntegerValidator'></a>
internal class IntegerValidator : AbstractValidator<int>
{
public IntegerValidator()
{
RuleFor(x => x).InclusiveBetween(1, 7);
}
}
<sup><a href='/PerformanceBenchmarks/IntegerValidator.cs#L5-L13' title='Snippet source file'>snippet source</a> | <a href='#snippet-IntegerValidator' title='Start of snippet'>anchor</a></sup>
| 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 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. |
-
net8.0
- CodingFlow.FluentValidation (>= 0.3.1)
- Vogen (>= 8.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
0.3.0
- Update to latest version of base library.
- Internal refactoring.