StringLengthValidator NotNullValidator and ValidatorComposition not owrking

Topics: Validation Application Block
Mar 19, 2014 at 2:15 AM
Is it possible? something very very essensial, don't works well, i'm surprised then Enterprise Library to have this kind of issues, i'm very stuck of this library:
    [ValidatorComposition(CompositionType.Or, Ruleset = "MyRules", MessageTemplate = "length is 3")]
    [StringLengthValidator(3, 3, Ruleset = "MyRules")]
    [NotNullValidator(Negated = true, Ruleset = "MyRules")]
Can someone help please?,
thanks.
Mar 19, 2014 at 7:02 AM
What behavior are you looking to see and what are you seeing?

It looks like you want to validate a string length but only if it's not null. The posted rule is: null is allowed, if not null then the string must be 3 characters long.

Here' the full code I'm using:
    public class MyObject
    {
        [ValidatorComposition(CompositionType.Or, Ruleset = "MyRules", MessageTemplate = "Length must be 3 or null")]
        [StringLengthValidator(3, 3, Ruleset = "MyRules")]
        [NotNullValidator(Negated = true, Ruleset = "MyRules")]
        public string Name { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var myobj1 = new MyObject() { Name = "ABCD" };

            Console.WriteLine("Obj1");
            var vrs = Validation.Validate(myobj1, "MyRules");

            foreach(var vr in vrs)
            {
                Console.WriteLine(vr.Message);
            }

            var myobj2 = new MyObject() { Name = "AB" };

            Console.WriteLine("Obj2");
            vrs = Validation.Validate(myobj2, "MyRules");

            foreach (var vr in vrs)
            {
                Console.WriteLine(vr.Message);
            }

            var myobj3 = new MyObject() { Name = null };

            Console.WriteLine("Obj3");
            vrs = Validation.Validate(myobj3, "MyRules");

            foreach (var vr in vrs)
            {
                Console.WriteLine(vr.Message);
            }

            var myobj4 = new MyObject() { Name = "ABC" };

            vrs = Validation.Validate(myobj4, "MyRules");

            foreach (var vr in vrs)
            {
                Console.WriteLine(vr.Message);
            }

        }
    }
Which gives the output:
Obj1
Length must be 3 or null
Obj2
Length must be 3 or null
Obj3
Obj4
~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to