Phema.Validation
3.1.4
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Phema.Validation --version 3.1.4
NuGet\Install-Package Phema.Validation -Version 3.1.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Phema.Validation" Version="3.1.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Phema.Validation --version 3.1.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Phema.Validation, 3.1.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Phema.Validation as a Cake Addin #addin nuget:?package=Phema.Validation&version=3.1.4 // Install Phema.Validation as a Cake Tool #tool nuget:?package=Phema.Validation&version=3.1.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Phema.Validation
A simple, lightweight and extensible validation library for .NET Core with fluent interfaces built using extension methods only
Installation
$> dotnet add package Phema.Validation
Core concepts
IValidationContext
- Scoped service to store all validation detailsIValidationCondition
- Contains a validation checks (e.g.Is(() => ...)
)IValidationDetail
- WhenIValidationCondition
is not valid adds toIValidationContext.ValidationDetails
ValidationSeverity
- Validation error level, used inIValidationContext.ValidationSeverity
andIValidationDetail.ValidationSeverity
IValidationScope
- Is a nested validation context with validation path override
Usage (ASP.NET Core, HostedService examples)
// Add `IValidationContext` as scoped service
services.AddValidation(options => ...);
// Get or inject
var validationContext = serviceProvider.GetRequiredService<IValidationContext>();
// Validation key will be `Name` using default validation part provider
validationContext.When(person, p => p.Name)
.Is(name => name == null)
.AddError("Name must be set");
// Validation key will be `Address.Locations[0].Latitude` using default validation part provider
validationContext.When(person, p => p.Address.Locations[0].Latitude)
.Is(latitude => ...custom check...)
.AddError("Some custom check failed");
Validation conditions
- Monads are not composable, so
Is
andIsNot
,IsNull
andIsNotNull
... duplication
// Check for Phema.Validation.Conditions namespace
validationContext.When(person, p => p.Name)
.IsNullOrWhitespace()
.AddError("Name must be set");
// Use multiple conditions (joined with AND)
validationContext.When(person, p => p.Name)
.IsNotNull()
// AND
.HasLengthGreater(20)
// .IsNull()
// .IsEqual()
// .IsNotUrl()
// .IsNotEmail()
// .IsMatch(regex)
.AddError("Name should be less than 20");
Validation details
// Null if valid
var validationDetails = validationContext.When(person, p => p.Age)
// Validation condition is valid
.Is(() => false)
.AddError("Age must be set");
// Use deconstruction
var (key, message) = validationContext.When(person, p => p.Age)
.IsNull()
.AddError("Age must be set");
// More deconstruction
var (key, message, severity) = validationContext.When(person, p => p.Age)
.IsNull()
.AddError("Age must be set");
Check validation
// Override default ValidationSeverity
validationContext.ValidationSeverity = ValidationSeverity.Warning;
// Throw exception when details severity greater than ValidationContext.ValidationSeverity
validationContext.When(person, p => p.Address)
.IsNull()
.AddFatal("Address is not presented!!!"); // If invalid throw ValidationConditionException
// Check if context is valid
validationContext.IsValid();
validationContext.EnsureIsValid(); // If invalid throw ValidationContextException
// Check concrete validation details
validationContext.IsValid(person, p => p.Age);
validationContext.EnsureIsValid(person, p => p.Age);
Compose and reuse validation rules with extensions
- Call is allocation free
- Static checks
// Extensions
public static void ValidateCustomer(this IValidationContext validationContext, Customer customer)
{
// Some checks
}
validationContext.ValidateCustomer(customer);
- Write your own middleware or validation components/validators on top of
IValidationContext
Validation part resolvers
ValidationPartResolver
is a delegate, trying to getstring
valdiation part fromMemberInfo
- Use built in resolvers with
ValidationPartResolvers
static class:Default
,DataMember
,PascalCase
,CamelCase
// Configure DataMember validation part resolver
services.AddValidation(options =>
options.ValidationPartResolver = ValidationPartResolvers.DataMember);
// Override validation parts with `DataMemberAttribute`
[DataMember(Name = "name")]
public string Name { get; set; }
Validation scopes
- Use scopes when you need to have:
- Same nested validation path multiple times
- Empty validation details collection (syncing with parent context/scope)
- ValidationSeverity override
// Validation key will be `Child.*ValidationPart*`
ValidateChild(validationContext.CreateScope(parent, p => p.Child))
// Validation key will be `Address.Locations[0].*ValidationPart*`
ValidateLocation(validationContext.CreateScope(person, p => p.Address.Locations[0]))
// Override local scope ValidationSeverity
using (var scope = validationContext.CreateScope(person, p => p.Address))
{
scope.ValidationSeverity = ValidationSeverity.Warning;
// Some scope validation checks syncing with validationContext
}
High performance with non-expression constructions
validationContext.When("key", value)
.IsNull()
.AddError("Value is null");
validationContext.CreateScope("key");
validationContext.IsValid("key");
validationContext.EnsureIsValid("key");
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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.0.0-preview9.19423.4)
- Microsoft.Extensions.Options (>= 3.0.0-preview9.19423.4)
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 |
---|---|---|
3.1.10 | 1,024 | 10/12/2019 |
3.1.9 | 1,075 | 9/27/2019 |
3.1.8 | 995 | 9/25/2019 |
3.1.7 | 362 | 9/15/2019 |
3.1.6 | 323 | 9/8/2019 |
3.1.5 | 305 | 9/7/2019 |
3.1.4 | 305 | 9/7/2019 |
3.1.3 | 327 | 9/7/2019 |
3.1.2 | 310 | 8/25/2019 |
3.1.1 | 299 | 8/24/2019 |
3.1.0 | 300 | 8/23/2019 |
3.0.9 | 304 | 8/23/2019 |
3.0.8 | 316 | 8/22/2019 |
3.0.7 | 300 | 8/16/2019 |
3.0.6 | 334 | 7/30/2019 |
3.0.5 | 297 | 7/29/2019 |
3.0.4 | 321 | 7/29/2019 |
3.0.3 | 306 | 7/28/2019 |
3.0.2 | 303 | 7/21/2019 |