WAQS: Non metadata validation rules

7 reasons to use WAQS

WAQS documentation

 

Metadata allows to define some basic validation rules. But sometimes, validation is more complex. For example imagine that we want to be sure that order’s OrderDate is always less than ShippedDate with an error if OrderDate is greater and a warning if dates are equal.

With WAQS, you can do it like this:

public static Error ValidateShipDateNotLessThanOrderDate(this Order o)
{
    if (o.ShippedDate.HasValue && o.ShippedDate.Value < o.OrderDate)
        return new Error { Criticity = Criticity.Error, Message = "Shipped date can't be less than order date" };
    return null;
}

public static Error ValidateShipDateGreaterThanOrderDate(this Order o)
{
    if (o.ShippedDate.HasValue && o.ShippedDate.Value == o.OrderDate)
        return new Error { Criticity = Criticity.Warning, Message = "Shipped date should be greater than order date" };
    return null;
}


// Of course, it’s probably better to use a resource file (I will show you how in a future post) but for this post I prefer having a very basic sample.



Like with calculated properties, validation methods use convention: extension method on entity and starts with “Validate”.



After transforming T4 templates, this validation will be effective on the server and the client if one of these two properties (ShippedDate and OrderDate) is modified.



In the client, we can use the same way than with metadata. So in a WPF app, we can use the following XAML:



<StackPanel>
    <DatePicker SelectedDate="{Binding Order.OrderDate}"
               controls:ErrorsBehaviors.Errors="{Binding Order.OrderDateErrors}" />
    <DatePicker SelectedDate="{Binding Order.ShippedDate}"
               controls:ErrorsBehaviors.Errors="{Binding Order.ShippedDateErrors}" />
</StackPanel>

This entry was posted in 16868, 8708. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>