Phema.Validation 3.0.4

C# strongly typed validation library for .NET

There is a newer version of this package available.
See the version list below for details.
Install-Package Phema.Validation -Version 3.0.4
dotnet add package Phema.Validation --version 3.0.4
<PackageReference Include="Phema.Validation" Version="3.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Phema.Validation --version 3.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Phema.Validation

Build Status
Nuget

C# strongly typed expression-based validation library for .NET

Installation

$> dotnet add package Phema.Validation

Usage (example)

// Add `IValidationContext` as scoped service
services.AddValidation(options => ...);

// Get or inject
var validationContext = serviceProvider.GetRequiredService<IValidationContext>();

// Use with a lot of validation rules (Check for Phema.Validation.Conditions namespace)
validationContext.When(person, p => p.Name)
  .IsNullOrWhitespace()
  .Is(name => ...Custom checks...)
  .AddError("Name must be set");

// Get validation details. Null if valid
var details = validationContext.When(person, p => p.Age)
  .IsNull()
  .AddError("Age must be set");

// 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

// Validate collections
validationContext.When(person, p => p.Children)
  .IsEmpty()
  .AddError("You have no children");

// Check if context is valid
validationContext.IsValid();
validationContext.EnsureIsValid(); // If invalid throw ValidationContextException

// Check concrete validation details
validationContext.IsValid(person, p => p.Age);

// Create nested validationContext
// It will be `Child.*ValidationPart*` validation key
ValidateChild(validationContext.CreateFor(parent, p => p.Child))

// Combine paths
// It will be `Address.Locations[0].*ValidationPart*` validation key
ValidateLocation(validationContext.CreateFor(person, p => p.Address.Locations[0]))

// It will be `Address.Locations[0].Latitude`
validationContext.When(person, p => p.Address.Locations[0].Latitude)
  .Is(latitude => ...)
  .AddError("Some custom check failed");
  
// Override validation parts with `DataMemberAttribute`
[DataMember(Name = "age")]
public int Age { get; set; }

Performance

  • Simpler expression = less costs
  • Try to use non-expression extensions in hot paths
  • Use CreateFor to not to repeat chained member calls (x =&gt; x.Property1.Property2[0].Property3)
  • Expression-based When extensions use expression compilation to get value
  • Composite indexers x =&gt; x.Collection[indexProvider.Parsed.Index] use expression compilation
validationContext.When("key", value)
  .IsNull()
  .AddError("Value is null");

validationContext.CreateFor("key");

validationContext.IsValid("key");
validationContext.EnsureIsValid("key");

Phema.Validation

Build Status
Nuget

C# strongly typed expression-based validation library for .NET

Installation

$> dotnet add package Phema.Validation

Usage (example)

// Add `IValidationContext` as scoped service
services.AddValidation(options => ...);

// Get or inject
var validationContext = serviceProvider.GetRequiredService<IValidationContext>();

// Use with a lot of validation rules (Check for Phema.Validation.Conditions namespace)
validationContext.When(person, p => p.Name)
  .IsNullOrWhitespace()
  .Is(name => ...Custom checks...)
  .AddError("Name must be set");

// Get validation details. Null if valid
var details = validationContext.When(person, p => p.Age)
  .IsNull()
  .AddError("Age must be set");

// 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

// Validate collections
validationContext.When(person, p => p.Children)
  .IsEmpty()
  .AddError("You have no children");

// Check if context is valid
validationContext.IsValid();
validationContext.EnsureIsValid(); // If invalid throw ValidationContextException

// Check concrete validation details
validationContext.IsValid(person, p => p.Age);

// Create nested validationContext
// It will be `Child.*ValidationPart*` validation key
ValidateChild(validationContext.CreateFor(parent, p => p.Child))

// Combine paths
// It will be `Address.Locations[0].*ValidationPart*` validation key
ValidateLocation(validationContext.CreateFor(person, p => p.Address.Locations[0]))

// It will be `Address.Locations[0].Latitude`
validationContext.When(person, p => p.Address.Locations[0].Latitude)
  .Is(latitude => ...)
  .AddError("Some custom check failed");
  
// Override validation parts with `DataMemberAttribute`
[DataMember(Name = "age")]
public int Age { get; set; }

Performance

  • Simpler expression = less costs
  • Try to use non-expression extensions in hot paths
  • Use CreateFor to not to repeat chained member calls (x =&gt; x.Property1.Property2[0].Property3)
  • Expression-based When extensions use expression compilation to get value
  • Composite indexers x =&gt; x.Collection[indexProvider.Parsed.Index] use expression compilation
validationContext.When("key", value)
  .IsNull()
  .AddError("Value is null");

validationContext.CreateFor("key");

validationContext.IsValid("key");
validationContext.EnsureIsValid("key");

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.1.10 132 10/12/2019
3.1.9 197 9/27/2019
3.1.8 162 9/25/2019
3.1.7 131 9/15/2019
3.1.6 93 9/8/2019
3.1.5 76 9/7/2019
3.1.4 78 9/7/2019
3.1.3 97 9/7/2019
3.1.2 77 8/25/2019
3.1.1 77 8/24/2019
3.1.0 77 8/23/2019
3.0.9 77 8/23/2019
3.0.8 80 8/22/2019
3.0.7 79 8/16/2019
3.0.6 72 7/30/2019
3.0.5 78 7/29/2019
3.0.4 80 7/29/2019
3.0.3 79 7/28/2019
3.0.2 76 7/21/2019
Show less