Validate exposes available validations through a series of extension methods which can be used to add object validations to any object.

Getting started with Validate:
// Validators and Validations are central to Validate
var person = new Person {FirstName = "Billy", LastName = "Moron", Age = 25, Address = new Address { Line1 = .... Zipcode = "AB 123" }};
var validator = person.Validate(); // This creates a new validator for Person.
// We can now add validations to this validator
validator.IsNotNull(p => p.FirstName) // You can let the validation message be auto generated
             .IsNotNull(p => p.LastName, "Last Name is mandatory.") // You can override the auto generated validation message by specifying one
             .RegexMatches(p => p.Address.Zipcode, "SomeRegex");
Assert.IsTrue(validator.IsValid); // This allows us to check if the object is valid or not.

Stopping/Continuing validations on first encountered error:
var validator = person.Validate(new ValidationOptions{StopOnFirstError = false}) // The default value for StopOnFirstError is true, setting it to false allows the validator to continue validating even in case it encounters errors
                      .IsNotNull(p => p.FirstName) // We are chaining validations in this case
                      .RegexMatches(p => p.Address.Zipcode, "SomeRegex");

Throwing exceptions on validation errors:
var validator = person.Validate(new ValidationOptions{ThrowValidationExceptionOnError = true}) // The default value for ThrowValidationExceptionOnError is false, setting it to true throws a validation exception on a validation error
                      .IsNotNull(p => p.FirstName) // If the FirstName was null this would throw a ValidationException
                      .RegexMatches(p => p.Address.Zipcode, "SomeRegex");

Waiting for all validations before throwing a validation exception:
var validator = person.Validate(new ValidationOptions{ThrowValidationExceptionOnError = true, StopOnFirstError = false}) // We will now not throw an exception on the first encountered validation error
                      .IsNotNull(p => p.FirstName) // If the FirstName was null this would not throw an exception now
                      .RegexMatches(p => p.Address.Zipcode, "SomeRegex")
                      .Throw(); // A throw is required at the end in this case. The ValidationException will now contain a list of all the validation errors encountered.

Creating a named validation:
var validationRepository = new ValidationRepositoryFactory().GetValidationRepository();
// A validation is a cached validator which can be reused by name
var validation = new Validation<Person>("My_Person_Validation") // This creates a named validation, we can also pass ValidationOptions as we did while creating a validator above.
                        .Setup(v => v.IsNotNull(p => p.FirstName) // We use the same syntax to create validations, as we did for validators
                                           .IsNotNul(p => p.LastName) ....
var validator = validation.RunAgainst(person); // We can now run this validation against a person object
validationRepository.Save(validation); // We can also save this validation in memory for later use
// Somewhere else in code, we can now use this validation using ValidateUsing("Validation_Name")
var validator = person.ValidateUsing("My_Person_Validation"); // Invoke the saved validation using the saved validation's name

Please refer to the documentation for a list of available validations and examples on usage. You can also look at the unit tests from the source code.

Last edited Jan 5, 2011 at 5:37 PM by ashishsharmaait, version 3


No comments yet.