Validation with LightSpeed

Validation is an important part of most applications, it helps ensure data integrity and, if implemented well, guide the user on how to use applications correctly in a friendly manner.

A common query about how to achieve such validation is where the validation should occur and how it should be applied. Many developers elect to place such validation code close to the view, for example in ASP.NET you may use the Validator controls and have any custom validation in the code behind of the page. Modern software development is moving to place field level validation in the domain model to provide a single instance of these rules to avoid duplication. LightSpeed provides a rich framework to help create these validations.

LightSpeed ships with several common validators (found under the Mindscape.LightSpeed.Validation namespace).

  • Presence validator: ensure a field has a value
  • Unique validator: ensure a field is unique in the database
  • Uri validator: ensure a field is a valid URI
  • Format validator: ensure a field conforms to a provided regular expression
  • Email validator: ensure a field is a valid email address
  • Comparison validator: compares a value to another value

More than one validator can be applied to a single field which enables richer validation if required.

Applying a validation

Validators in LightSpeed are applied as attributes to the fields in the domain classes. For example, to apply the presence validation to a firstname field you would write the following:

private string _firstname;

To apply multiple validators:

private string _emailAddress;

Validating objects

Once validation attributes have been applied you will no longer be able to persist objects that fail validation. The exception thrown will provide details of what validation failed. That works well for ensuring data consistency however this does not help create an enjoyable experience for end users. Thankfully, you can validate domain objects by calling the .Validate() method on the object.

Once validation has occurred, if any have failed, the Errors collection on the object will be populated with friendly error messages about why the object is invalid. This provides a elegant approach to validating forms such as a sign-up web page:

  1. Create the sign-up form
  2. On post back assign each field to the properties of the user object
  3. Call Validate() on the object
  4. If validation fails bind the Errors collection to a repeater or similar on the web page
  5. If validation passes, persist the object

Further to this, every LightSpeed object implements the IDataErrorInfo interface which most .NET controls can use to show in real time if an object that is bound to it is valid. For example, if binding LightSpeed objects to a Grid control the red error icon will display in the field if the object is in an invalid state and provide a tool-tip describing the fault.

Create your own validator

LightSpeed makes the ValidateAttribute class available for developers to create their own validators (by creating new ValidationRule objects). This can be useful if a custom validator is needed in several model classes, allowing reuse of your custom validation code.

Alternatively, if custom validation is required for a given object, LightSpeed allows developers to override the OnValidate() method of the class:

protected override void OnValidate()
  if ((Contributor == null) && (ApprovedBy == null))
    Errors.AddError("Must have one or the other");

For more information regarding Validation in LightSpeed please read the Validation section of the user guide.

I hope this quick post about domain model validation has been useful in helping you write more robust solutions using LightSpeed. If you feel anything has been left out or have any questions please leave a comment.

John-Daniel Trask

kick it on

Tagged as LightSpeed

2 Responses to “Validation with LightSpeed”

  • Hi,

    Can this be used in .net 1.1 projects.


  • Sorry LightSpeed is 2.0 and up.



  • Leave a Reply


Join our mailer

You should join our newsletter! Sent monthly:

Back to Top