ValidationAspects – where next?


This is a post for .net developers using VA, developers considering VA, and also developers not considering VA as the validation framework of their choice.

VA (ValidationAspects) is an validation framework used to validate Objects, Properties and Method Parameters against coding contracts and business rules. Validation can be declared via attributes and/or augmented/replaced programmatically at runtime … more.

ValidationAspects is two and a half months old now, not nearly old enough to have had serious use, but the core, for me, is feature complete. I want to broaden the feature set and the appeal of VA for the next release. I want to know what I can do to make it the right validation framework for your project.

I’ve identified the following three areas where I may be able to achieve this:

  1. Support other aspect frameworks such as Unity Interception and Castle.DynamicProxy2.
  2. Build for different platforms such as Silverlight and the Compact Framework.
  3. Add to the current set of built-in validators.

But I’m guessing – I’d love some feedback – what else could be added or enhanced. Please let me know what’s important to you.

  • If you are using VA, what are the features you would like to see implemented? Are there any general case validators that you would find useful – validators beyond the typical set? How about “FileExists” or “DateBefore”?
  • If VA doesn’t offer what you need, what are the features that you require? What don’t you like about it?
  • In regard to what VA currently offers, what would you like improved? Is the exception formatting approach good for you? Are the default exception messges good to go? What features could do just a little bit more for you? Is there anything that is confusing about the api or the wiki documentation?

Please reply on this post, reply on this discussion thread, raise a new issue, or vote on an issue. I would very much appreciate any feedback you may have.

Many thanks


11 Responses to “ValidationAspects – where next?”

  1. Hi, I love the VA library, but I was thinking that maybe it would be useful to have one big document describing everything you could do with a library – something you could package together with the assemblies. What do you think?

  2. 2 Mike

    Hi Dmitri, thanks for your comment – it’s a good idea. I’ll put it in the next release. I also plan to provide an end-to-end example application. What do you think of the current documentation on the wiki? It’s hard for me to detach myself from VA to see where the holes are for users looking at it for the first time.

  3. Well, two things come to mind: first, there is no Hello World or anything like that. Second, the Wiki is really fragmented – some parts point to blog posts, others are described on a page. I just thought it would be nice to have one single document that has everything in it.

  4. 4 dotnet4fun

    VA rocks! I am using it at my current project and I am very pleased with it.
    1. A new aspect which would validate the precision of a decimal number, smth which is mapped into a database as decimal(x,y). I think it would be useful and so would be any other new aspect which reflects a database column definition.
    2. Some XML comments would be nice to have, they look good in Intellisense 😉
    3. Stop using PostSharp.Laos and implement own weaver. (I know how hard it is, I thought I’d mention it though…)

  5. 5 Mike

    @ dotnet4fun
    Just curious why the suggestion of a custom weaver?

  6. Hi Mike,
    I’ve found that I cannot get validation on parameters for constructors, or static methods (which includes implitic cast operators). I know this gives problems with the content of the Validation Exceptions (no instance to report against), but would be useful to have.


    class Program
    static void Main(string[] args)
    // Does not throw error,
    // even though parameter validation added.
    Poco p = new Poco(70);

    // Does not throw error,
    // even though parameter validation added.
    int z = Poco.StaticIntMethod(55);

    // Correctly throws exception.
    p.IntValue = 6;

    // Correctly throws exception
    int answer = p.IntMethod(125);

    Console.WriteLine(“Press key to end …”);


    internal class Poco
    // Want to limit constructor argument to between 0 and 10
    // — this does not appear to work
    public Poco([Minimum(0), Maximum(10)] int initialValue)
    IntValue = initialValue;

    // Can only set property between 0 and 10
    // — this works.
    public int IntValue { get; set; }

    // Want to limit method parameter to between 0 and 10
    // — this works.
    public int IntMethod([Minimum(0), Maximum(10)] int i)
    return i * 2;

    // Want to limit static method parameters to between 0 and 10
    // — this does not appear to work.
    public static int StaticIntMethod([Minimum(0), Maximum(10)]int i)
    return i * 2;


  7. 7 Mike

    @ Dr Herbie
    Thanks for your comment. VA should intercept static methods and validate the parameters. I’ll raise an issue on codeplex, add some unit tests and fix.
    For constructors I’m not sure PostSharp permits constructor interception – something I read but not tried myself. VA doesn’t currently support validation on constructors – discovery/registration/interception. If PostSharp can do the interception I’ll add it as a feature for next release.

  8. 8 Adam P.

    I found ValidationAspects on when looking for an attribute-based validation framework to use with a middle-tier application (no UI). I was really interested in what I saw with this framework, but there are a few things that would make this more useful for what I’m doing.

    1. Some more startup examples would be nice. Maybe I just haven’t looked around enough to find the documentation, but I’m interested in integrating this with Unity to make use of the interception but didn’t see any documentation on how to accomplish that.

    2. One thing I’m not thrilled about is how exceptions are thrown in order to convey that a validation error occurred. Most of the time when a validation error occurs, especially if the validation is working closely with the UI, you probably don’t care much about performance, but in some cases I could see this being a concern.
    I can see how throwing exceptions probably works well with interception validation, as it would interrupt the operation being intercepted, but I’d really prefer to see the actions returned from the factory objects outputting/returning some kind of “ValidationError” object and then having something else within the interception code decide that an exception needs to be thrown based on those “ValidationError” objects. As it is, the state validation logic has to pay the potential performance penalty of throwing multiple exceptions just to construct a ValidationResult object containing all of the errors.

    3. I like the ability to call one method to validate an object and cause that to validate any child objects as well. I see that there is a built-in ValidateObject attribute that makes this very easy to accomplish, but when calling Validate on the parent object the ValidationResult just contains a flat list of messages. It would be nice if the ValidationResult object contained a more heirarchial data structure that resembled the object being validated, so it would be easier to tell what validation errors go with what object.
    For instance, I might have some kind of Transaction class that contains two Person properties, PersonA and PersonB. I can put a ValidateObject attribute on the PersonA and PersonB properties so that when I validate the Transaction object it will also validate both of the Person objects, but if either of the Person objects generates a validation error, there isn’t really a way for me to tell whether the validation error came from PersonA or PersonB.

    4. In some cases, I’m not just interested in detecting a validation error and reporting it, I’m interested in simply fixing the data. I like the fact that it seems very easy to implement my own attribute and corresponding factory where I override the validate ValidateAction method to return an action that corrects the value on my property instead of throwing an exception. However, in that case I might like to have my action return some kind of informational object (ValidationInfo?) that informs the consumer that something did happen, though it wasn’t an error. This goes back to point #2 where I’d prefer to receive a ValidationResult object that is constructed based on something other than exceptions.

  9. 9 Mike

    1) Documentation isn’t great I admit. There are Unit Tests that will hopefully provide you with enough info to integrate with Unity, but if I remember correctly I couldn’t get object.Validate() to work correctly. The issue with Unity (when I looked at it) was that given a proxy you cannot get to the underlying target object Type. Castle DP2 provides a IProxyTarget (I think) interface on the proxy to get to the target.
    2) VA uses exceptions to pass validation errors. That’s just the way it works and has pros and cons.
    3) The ValidationResult contains a ValidationException which provides the complete validation failure hierachy you require.
    4) Fixing up data on the fly goes beyond Validation, but if you get something working please send me a patch.

  10. 10 Adam P.

    Oh, thanks, I see now that the ValidationException class holds a reference to the corresponding IValidationContext object; that’s just what I was looking for. I like the fact that it gives me access to the actual System.Reflection objects, so if I’d like to do some additional interrogation of the code where the validation error occurred then I can do that.
    I’ll have to take a closer look at that and see if there is a way to get the individual ValidationExceptions or IValidationContexts per validation error. Just glancing at it, I only see one IValidationContext corresponding to the top level validation of my object even though I have messages coming from validations on several different properties and from within other referenced objects.

    To elaborate on #4, below is a Truncate factory that I quickly threw together using this framework. I liked how easy the framework makes it to put this together, but there isn’t really a mechanism in place for me to report a message saying “Hey, I truncated this value”. I wouldn’t want to throw an exception here to report that message because in this case I actually don’t want to interrupt the call in case interception validation mode is being used. As you mentioned, this falls outside of the realm of pure validation, but I just thought I’d mention it if you were interested in a possible expansion of this framework:

    public class Truncate : MaximumLength
    protected override Action ValidateAction
    return (v, c) =>
    if (v != null && v.Length > Length)
    if (c.Parameter != null)
    // truncate the value of the argument
    c.Arguments[c.Parameter.Position] = v.Substring(0, this.Length);
    if (c.Property != null)
    // truncate the value of the property
    c.Property.SetValue(c.Instance, v.Substring(0, this.Length), null);

  11. Great post.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: