WebAPIservices

Validation framework for .NET


The Validation framework is the first product that we released to provide developers with web-API's and supporting libraries. It is fully documented and free to use!

The Validation library provides an extensible data validation framework that allows for reusable validation rules.
It can be used to validate a wide variety of data types ranging from simple value types to complex object graphs.
Included in the library is a fluent API that eliminates complex code constructions and increases code readability.
Below an overview of the main features:

  • Allows validation of simple value types, collections and complex object graphs.
  • Inline parameter, variable and object validation.
  • Supports delta validation (old vs new value) of your data.
  • Conditional validation that allows you to decide under which condition a validation rule must be carried out.
  • Includes several built in validators for validating your data.
  • Extensible with your own reusable validators and error object providers.
  • Language and culture neutral.
  • Error objects can be of any type that are meaningful to your application..
  • Validation process can return if a failure was detected, throw an exception of report back all the validation results.
  • Includes a fluent API that eliminates complex code constructions and increases code readability.
  • Build with the task based asynchronous patters (TAP)
  • Supports the .net 4.5 framework and above.
  • Supports .net standard 1.5 and above.
Read more, find examples and explore the API at the documentation site.

Motivation

For our upcomming web-API's we needed to be able to decouple the validation(business) rules of a class. We wanted to:
  • Reuse validation rules.
    For example, a zipcode validator.
  • Reuse (data)classes with different validation rules.
    For example, a Person class used by customer X should be able to have different validation rules then the Person class used by customer Y.
  • Dynamically create validation rules.
    For example, parse a JSON Schema document and create the validation rules.

Installation

The Validation framework is available as a nuget package. To install the package into your project you can use the NuGet tools or use Visual Studio.

Install-Package Xploration.Validation
More information on how to install the package can be found here.

Inline validation

Inline validation is a convenient way to validate small pieces of data, for example the parameters of a method.
With the inline validation functionality you can validate your data "on the fly".
// This example checks that the length of the firstName variable.
string firstName = "Danny";
// valid becomes true since the length of firstName is <= 5. 
bool valid = await firstName.Check().OnMaxLength(5).ValidateAsync();

firstName = "Shirley"; 
// valid becomes false since the length of firstName is > 5.
valid = await firstName.Check().OnMaxLength(5).ValidateAsync();
More information and examples about inline validation can be found here.

Class validation

With class validation you introduce new validators that are tailor-fit to validate a specific class. Property validation rules are registerd with those classes and are enforced when validating an object.
// This example (only)checks that the Name property of a Company object.
ClassValidator<Company> validator = new ClassValidator<Company>();
// Define a rule for the Name property its minimum length
validator.Check(c => c.Name).OnMinLength(5).ReturnError("The minimum length is 5"); 

Company company = new Company { Name = "Xploration" };
// valid becomes true since the length of Name is >= 5.
bool valid = await validator.ValidateAsync(company);
More information and examples about class validation can be found here.