Click or drag to resize

Quickstart class validation.

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

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.

using Xploration.Validation;

Throughout this quickstart a Person class is used as the data object.

public class Person
  public string Name { get; set; }
  public string EMail { get; set; }
  public int Age { get; set; }
  public DateTime LastModified { get; set; }

Altough we could use inline validation to validate an object, the recommended way is to create a class validator.
Using a class validator gives you the opportunity to encapsulate and reuse your validation rules.
This quickstart will show you how to create a class validator for the Person class.

// Derive from ClassValidator with the generic parameter set to Person
public class PersonValidator : ClassValidator<Person>
  // You can add validation rules in the constructor. 
  public PersonValidator()
    // Check that the Name property is not null or whitespace. Return a custom string error message.
    Check((person) => person.Name).OnNotNullOrWhiteSpace().ReturnError("A name is required");

    // Conditionally(if) check the EMail property with a regular expression.
    Check((person) => person.EMail).OnMatches(@"^\S+@\S+$").If((c) => !string.IsNullOrEmpty(c.NewValue.EMail));

    // Check that the Age property is in the range of 0..110.
    Check((person) => person.Age).OnMinValue(0).And().OnMaxValue(110);

    // Check that the LastModified property is not in the future. Return a custom integer error code.
    Check((person) => person.LastModified).On((lm) => lm <= DateTime.Now).ReturnError(99);

In the above code snippet we created a class validator for the Person class. To validate Person objects we need to declare rules to validate the properties.
The best place to declare these rules is in the constructor. In this example we created four rules the properties of a Person object must meet:

  1. The Name property can't be null or whitespace. If the rule fails an error message is returned.
  2. The EMail property must match the given regular expression but only if the EMail property is not null or empty.
  3. The Age property must be at least 0 and at most 110.
  4. The LastModified property can't be in the future. Since this is not a constant an expression is usedd to validate this.

After declaring these rules, using the Fluent API, we are ready to validate a Person object.

// Create a PersonValidator instance.
PersonValidator validator = new PersonValidator();
// Create a Person instance.
Person person = new Person();

// Validate the peron.
bool valid = await validator.ValidateAsync(person);
// According to the rules only Name should fail. valid will become false.

// Fix the Name property, no failures should occur now. valid will become true.
person.Name = "Danny";
valid = await validator.ValidateAsync(person);

// Set the LastModified property into the future. valid will become false.
person.LastModified = DateTime.Now.AddMinutes(1);
valid = await validator.ValidateAsync(person);
Note Note

Validation of the Person it's properties by the declared rules are done in parallel.

See Also