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.
The example below validates a string variable on its length and returns true if the variable is valid.
// 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:
First we need a variable, argument or property to validate. In this case the firstName variable.
Next we tell the framework that we want to Check() that variable.
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.
Last we ask the framework to validate the variable. If the variable is valid true will be returned; otherwise false.