Click or drag to resize

Included validators.

This section provides an overview of the predefined validators that comes with the Xporation Validation library.

You can use the provided validators to validate your data or use as a base to build your own custom validators.
Many of the listed validators are also used by the Fluent API, which translates the "On..." methods to an appropriate validator.
If you have a suggestion to include a validator in the library and/or Fluent API then please let us know!

The examples require you to install the Xploration.Validation nuget package into your project or reference the Xploration.Validation.dll.
After the package is installed or the dll is referenced add a using directive to your source file.

C#
using Xploration.Validation;
RequiredValidator

The RequiredValidator requires an entity to be other than its default type value or null.

C#
// Create an instance of the RequiredValidator.
RequiredValidator validator = new RequiredValidator();

int age = default(int);
// valid becomes false since age contains the default value of an int.
bool valid = await validator.ValidateAsync(age);

age = 25;
// valid becomes true since age does not contain the default value of an int.
valid = await validator.ValidateAsync(age);

object anObject = null;
// valid becomes false since anObject is a null reference.
valid = await validator.ValidateAsync(anObject);

anObject = this;
// valid becomes true since anObject points to this.
valid = await validator.ValidateAsync(anObject);

You can include this validator in a Fluent API statement by using the OnNotDefault()method.

ReadOnlyValidator

The ReadOnlyValidator doesn't allow changes of an entity. For this it checks the transition between the old and new state of the entity.

C#
// Create an instance of the ReadOnlyValidator.
ReadOnlyValidator validator = new ReadOnlyValidator();

// Define the old and new state of a name.
string oldValue = "Rik";
string newValue = "Luc";

// valid becomes false since "Rik" changed into "Luc"
bool valid = await validator.ValidateAsync(oldValue, newValue);

newValue = oldValue;
// valid becomes true since no change is detected.
valid = await validator.ValidateAsync(oldValue, newValue);

You can include this validator in a Fluent API statement by using the OnUnChanged()method.

NotNullOrEmptyValidator

The NotNullOrEmptyValidator requires a string value not to be null or empty.

C#
// Create an instance of the NotNullOrEmptyValidator.
NotNullOrEmptyValidator validator = new NotNullOrEmptyValidator();

// Define a name.
string name = "";

// valid becomes false since name is an empty string
bool valid = await validator.ValidateAsync(name);

name = "Shirley";
// valid becomes true since since name is not null or an empty string
valid = await validator.ValidateAsync(name);

You can include this validator in a Fluent API statement by using the OnNotNullOrEmpty()method.

NotNullOrWhiteSpaceValidator

The NotNullOrWhiteSpaceValidator requires a string not to be null, empty, or consists only of white-space.

C#
// Create an instance of the NotNullOrWhiteSpaceValidator.
NotNullOrWhiteSpaceValidator validator = new NotNullOrWhiteSpaceValidator();

// Define a name.
string name = "   ";

// valid becomes false since name consists only of white-space.
bool valid = await validator.ValidateAsync(name);

name = "Danny";
// valid becomes true since since name is not null, empty or consists only of white-space.
valid = await validator.ValidateAsync(name);

You can include this validator in a Fluent API statement by using the OnNotNullOrWhiteSpace()method.

MinLengthValidator

The MinLengthValidator requires a string to have a minimum length.

C#
// Create an instance of the MinLengthValidator that requires a minimum string length of 4.
MinLengthValidator validator = new MinLengthValidator(4);

// Define a name.
string name = "Rik";

// valid becomes false since the name length is 3.
bool valid = await validator.ValidateAsync(name);

name = "Nico";
// valid becomes true since since name has a length of 4.
valid = await validator.ValidateAsync(name);

You can include this validator in a Fluent API statement by using the OnMinLength()method.

MaxLengthValidator

The MaxLengthValidator requires a string not to exceed a certain length.

C#
// Create an instance of the MaxLengthValidator that requires a string not to exceed a length of 4.</para>
MaxLengthValidator validator = new MaxLengthValidator(3);

// Define a name.
string name = "Nico";

// valid becomes false since the name exceeds a length of 3.
bool valid = await validator.ValidateAsync(name);

name = "Luc";
// valid becomes true since since the name does not exceeds a length of 3.
valid = await validator.ValidateAsync(name);

You can include this validator in a Fluent API statement by using the OnMaxLength()method.

RegexValidator

The RegexValidator requires a string to match a regular expression.

C#
// Create an instance of the RegexValidator that requires a string to match a regular expression.
// In this case a string must start with character a or A.
RegexValidator validator = new RegexValidator("^[aA]");

// Define a name.
string name = "Marcel";

// valid becomes false since the name does not start with an a or A.
bool valid = await validator.ValidateAsync(name);

name = "Angela";
// valid becomes true since since the name starts with an A.
valid = await validator.ValidateAsync(name);

You can include this validator in a Fluent API statement by using the OnMatches()method.

MinValueValidator

The MinValueValidator requires an entity to be equal or greater then a minimum value.

C#
// Define a minimum DateTime.
DateTime now = DateTime.Now;
// Create an instance of the MinValueValidator that requires a DateTime to have a minimum value of 'now'.
MinValueValidator<DateTime> validator = new MinValueValidator<DateTime>(now);

// valid becomes false since the given DateTime is before 'now'.
bool valid = await validator.ValidateAsync(now.AddSeconds(-1));

// valid becomes true since the given DateTime is after 'now'.
valid = await validator.ValidateAsync(now.AddSeconds(1));

You can include this validator in a Fluent API statement by using the OnMinValue()method.

MaxValueValidator

The MaxValueValidator requires an entity to be equal or less then a maximum value.

C#
// Create an instance of the MaxValueValidator that requires a float to be less or equal then 4.5.
MaxValueValidator<float> validator = new MaxValueValidator<float>(4.5f);

// valid becomes false since the given float is larger the 4.5.
bool valid = await validator.ValidateAsync(4.6f);

// valid becomes true since the given float is less then 4.5.
valid = await validator.ValidateAsync(4.4f);

You can include this validator in a Fluent API statement by using the OnMaxValue()method.

PermittedValuesValidator

The PermittedValuesValidator requires an entity to be a member of a predefined set of entities.

C#
// Create an instance of the PermittedValuesValidator that requires an int to be an element of the int array.
PermittedValuesValidator<int> validator = new PermittedValuesValidator<int>(new int[] { 1, 2, 3 });

// valid becomes false since 4 is not an element of the int array.
bool valid = await validator.ValidateAsync(4);

// valid becomes true since 2 is an element of the int array.
valid = await validator.ValidateAsync(2);

You can include this validator in a Fluent API statement by using the OnElementOf()method.

ExpressionValidator

The ExpressionValidator validates an entity with an expression. The expression must return true if the entity is valid; otherwise false.

C#
// Create an instance of the ExpressionValidator that requires a DateTime to be in the past.
ExpressionValidator<DateTime> validator = new ExpressionValidator<DateTime>((dt) => dt < DateTime.Now);

// valid becomes false since the given DateTime is in the future.
bool valid = await validator.ValidateAsync(DateTime.Now.AddHours(1));

// valid becomes true since the given DateTime is in the past.
valid = await validator.ValidateAsync(DateTime.Now.AddSeconds(-1));

You can include this validator in a Fluent API statement by using the On()method.

MinElementsValidator

The MinElementsValidator requires a minimum amount of elements in a collection.

C#
// Create an instance of the MinElementsValidator that requires a collection to contain at least 3 elements.
MinElementsValidator validator = new MinElementsValidator(3);

// valid becomes false since the given collection contains less the 3 elements.
bool valid = await validator.ValidateAsync(new List<string> { "1", "2" });

// valid becomes true since the given collection contains more then 2 elements.
valid = await validator.ValidateAsync(new int[] { 1, 2, 3 });

You can include this validator in a Fluent API statement by using the OnMinElements()method.

MaxElementsValidator

The MaxElementsValidator allows up to a certain amount of elements in a collection.

C#
// Create an instance of the MaxElementsValidator that allows up to 2 elements in a collection.
MaxElementsValidator validator = new MaxElementsValidator(2);

// valid becomes false since the given collection contains more then 2 elements.
bool valid = await validator.ValidateAsync(new List<string> { "1", "2", "3" });

// valid becomes true since the given collection contains not more then 2 elements.
valid = await validator.ValidateAsync(new int[] { 1, 2 });

You can include this validator in a Fluent API statement by using the OnMaxElements()method.

DisallowedElementValidator

The DisallowedElementValidator disallows a certain entity to be present in a collection.

C#
// Create an instance of the DisallowedElementValidator that does not allow the int 13 to be present in a collection.
DisallowedElementValidator<int> validator = new DisallowedElementValidator<int>(13);

// valid becomes false since the given collection contains the int 13.
bool valid = await validator.ValidateAsync(new int[] { 11, 12, 13, 14 });

// valid becomes true since the given collection does not contain the int 13.
valid = await validator.ValidateAsync(new int[] { 11, 12, 14 });

You can include this validator in a Fluent API statement by using the OnDisallowedElement()method.

RequiredElementValidator

The RequiredElementValidator requires a collection to contain a certain element.

C#
// Create an instance of the RequiredElementValidator that requires the int 25 to be present in a collection.
RequiredElementValidator<int> validator = new RequiredElementValidator<int>(25);

// valid becomes false since the given collection does not contain the int 25.
bool valid = await validator.ValidateAsync(new int[] { 11, 12, 13, 14 });

// valid becomes true since the given collection does not contain the int 13.
valid = await validator.ValidateAsync(new int[] { 23, 24, 25 });

You can include this validator in a Fluent API statement by using the OnRequiredElement()method.

DelegatedElementValidator

The DelegatedElementValidator validates elements of a collection by delegating element validation to an other validator.

C#
// Create an instance of the DelegatedElementValidator that uses a MaxValueValidator to validate the elements in a collection.
DelegatedElementValidator<int> validator = new DelegatedElementValidator<int>(new MaxValueValidator<int>(10));

// valid becomes false since the given collection contains elements greater as 10
bool valid = await validator.ValidateAsync(new List<int>() { 8, 9, 10, 11, 12 });

// valid becomes true since the given collection does not contain elements greater as 10
valid = await validator.ValidateAsync(new int[] { 7, 8, 9 });

You can include this validator in a Fluent API statement by using the CheckElements()method.

ClassValidator

The ClassValidator is a specialized validator for validating (data)objects. More information about this validator can be found at Class validation.