Click or drag to resize

Quickstart inline validation.

This quickstart demonstrates how to make use of the inline validation functionality.

Inline validation is a convenient way to validate small pieces of data, for example the parameters of a method.

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;

The example below validates a string variable on its length and returns true if the variable is valid.

C#
// This example checks that the firstName variable does not contain more then 5 chars.

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();

If we analyze the code we can see that a fluent statement is used to validate the string variable firstName.
To validate the variable several steps were made:

  1. First we need a variable, argument or property to validate. In this case the firstName variable.

  2. Next we tell the framework that we want to Check() that variable.

  3. Next we tell the framework what we want to check, in this case we added a rule for the maximum length of the string variable.

  4. Last we ask the framework to validate the variable. If the variable is valid true will be returned; otherwise false.

See Also