Property Validation

Because you can define a property as being optional, you may want to validate the property in your template to determine whether or not the user has entered a value. For example, you might want to allow generating a class either with a namespace declaration or without, at the user's option. To do this, you would first define an appropriate optional property:

<%@ Property Name="ClassNamespace" Type="System.String" Optional="True" Category="Context" Description="The namespace that the generated class will be a member of." %>

In your template, you can check to see whether there's a value in this property at runtime. If so, you want to output the appropriate namespace declaration. If you're using C# as your template language, you'd do that like this:

<% if (ClassNamespace != null && ClassNamespace.Length > 0)
{ %>namespace <%= ClassNamespace %>{<% }
%>

If your template is using VB, the equivalent code is:

<% If Not ClassNamespace Is Nothing AndAlso ClassNamespace.Length > 0 Then %>
Namespace <%= ClassNamespace %>
<% End If %>

Custom Validation Errors

You can also specify custom validation logic to validate your template properties. By specifying custom validation logic, you can prevent your template from generating instead of using conditionals to control what is generated (as shown in the example above). You can specify your own custom validation errors by implementing the GetCustomValidationErrors method.

In the example below we will create a property called Age and validate that it's within our age limit.

<%@ Property Name="Age" Default="130" Type="System.Byte" %>
You may notice that the default age is 130 years old. We are doing this on purpose to show that this age is invalid with a ValidationError.

Next, we will import the System.Collections.Generic namespace so we can use the IEnumerable<T> and List<T> types.

<%@ Import Namespace="System.Collections.Generic" %>

We then create a new script block and implement the GetCustomValidationErrors method. This method must return an IEnumerable<ValidationError> that contains your validation errors. In the code below you can see we are checking to see if the Age property has a value greater than 125. If the value is greater than 125, we add a new ValidationError to the errors collection with our own message stating why this property is invalid. When we run the template, Age will always be greater than 130 because we set the default value of Age to 130.

<script runat="template">
/// <summary>
/// Allows the developer to add custom ValidationErrors to the validation collection.
/// </summary>
public override IEnumerable<ValidationError> GetCustomValidationErrors() {
    var errors = new List<ValidationError>();

    if(Age > 125)
        errors.Add(new ValidationError("Invalid Age, the oldest living person in history lived to 122 years old.", "Age"));

    return errors;
}
</script>

Now, when you try to generate this template, you will get a validation error in the Error Window with your custom message.

You can download this sample template here.