CodingFlow.FluentValidation
0.2.2
See the version list below for details.
dotnet add package CodingFlow.FluentValidation --version 0.2.2
NuGet\Install-Package CodingFlow.FluentValidation -Version 0.2.2
<PackageReference Include="CodingFlow.FluentValidation" Version="0.2.2" />
<PackageVersion Include="CodingFlow.FluentValidation" Version="0.2.2" />
<PackageReference Include="CodingFlow.FluentValidation" />
paket add CodingFlow.FluentValidation --version 0.2.2
#r "nuget: CodingFlow.FluentValidation, 0.2.2"
#:package CodingFlow.FluentValidation@0.2.2
#addin nuget:?package=CodingFlow.FluentValidation&version=0.2.2
#tool nuget:?package=CodingFlow.FluentValidation&version=0.2.2
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.
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>
| 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
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on CodingFlow.FluentValidation:
| Package | Downloads |
|---|---|
|
CodingFlow.FluentValidation.VogenExtensions
Vogen extensions for CodingFlow Fluent Validation. |
GitHub repositories
This package is not used by any popular GitHub repositories.
0.2.2
- Fix minor documentation issue.
0.2.1
- Include documentation comments in nuget package.
0.2.0
- Added several validators.
- Added documentation.
- Reduced expose of internally used properties and methods.
0.1.0
Initial release.