Example #1
0
 public IList <IInvalidValueInfo> Validate <T, TP>(T entityInstance, Expression <Func <T, TP> > property) where T : class
 {
     return
         (validatorEngine.ValidatePropertyValue(entityInstance, property)
          .Select(iv => new InvalidValueInfo(iv))
          .Cast <IInvalidValueInfo>().ToList());
 }
        public void ValidateAnyClass()
        {
            ValidatorEngine ve = new ValidatorEngine();

            Assert.IsTrue(ve.IsValid(new AnyClass()));
            Assert.IsNotNull(ve.GetValidator <AnyClass>());
            ve.AssertValid(new AnyClass());             // not cause exception
            Assert.AreEqual(0, ve.Validate(new AnyClass()).Length);
            Assert.AreEqual(0, ve.ValidatePropertyValue <AnyClass>("aprop", new AnyClass()).Length);
            Assert.AreEqual(0, ve.ValidatePropertyValue(new AnyClass(), "aprop").Length);

            Assert.AreEqual(0, ve.Validate("always valid").Length);
            Assert.AreEqual(0, ve.ValidatePropertyValue("always valid", "Length").Length);
        }
        public void ValidateChangedPropertyOfProxy()
        {
            var validatorConf = new FluentConfiguration();

            validatorConf.SetDefaultValidatorMode(ValidatorMode.UseExternal);

            var vDefSimple = new ValidationDef <SimpleWithRelation>();

            vDefSimple.Define(s => s.Name).MatchWith("OK");
            vDefSimple.Define(s => s.Relation).IsValid();
            validatorConf.Register(vDefSimple);

            var vDefRelation = new ValidationDef <Relation>();

            vDefRelation.Define(s => s.Description).MatchWith("OK");
            validatorConf.Register(vDefRelation);

            var engine = new ValidatorEngine();

            engine.Configure(validatorConf);

            object savedIdRelation;

            // fill DB
            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    var relation = new Relation {
                        Description = "OK"
                    };
                    savedIdRelation = s.Save(relation);
                    tx.Commit();
                }

            using (ISession s = OpenSession())
            {
                var proxy = s.Load <Relation>(savedIdRelation);
                proxy.Description = "no";

                Assert.AreEqual(1, engine.ValidatePropertyValue(proxy, p => p.Description).Length);
                Assert.DoesNotThrow(() => engine.ValidatePropertyValue(proxy, p => p.Description));

                Assert.AreEqual(1, engine.ValidatePropertyValue(proxy, "Description").Length);
                Assert.DoesNotThrow(() => engine.ValidatePropertyValue(proxy, "Description"));
            }

            CleanDb();
        }
        public void ValidateChangedPropertyOfProxy()
        {
            var validatorConf = new FluentConfiguration();
            validatorConf.SetDefaultValidatorMode(ValidatorMode.UseExternal);

            var vDefSimple = new ValidationDef<SimpleWithRelation>();
            vDefSimple.Define(s => s.Name).MatchWith("OK");
            vDefSimple.Define(s => s.Relation).IsValid();
            validatorConf.Register(vDefSimple);

            var vDefRelation = new ValidationDef<Relation>();
            vDefRelation.Define(s => s.Description).MatchWith("OK");
            validatorConf.Register(vDefRelation);

            var engine = new ValidatorEngine();
            engine.Configure(validatorConf);

            object savedIdRelation;
            // fill DB
            using (ISession s = OpenSession())
            using (ITransaction tx = s.BeginTransaction())
            {
                var relation = new Relation { Description = "OK" };
                savedIdRelation = s.Save(relation);
                tx.Commit();
            }

            using (ISession s = OpenSession())
            {
                var proxy = s.Load<Relation>(savedIdRelation);
                proxy.Description = "no";

                Assert.AreEqual(1, engine.ValidatePropertyValue(proxy, p => p.Description).Length);
                Assert.DoesNotThrow(() => engine.ValidatePropertyValue(proxy, p => p.Description));

                Assert.AreEqual(1, engine.ValidatePropertyValue(proxy, "Description").Length);
                Assert.DoesNotThrow(() => engine.ValidatePropertyValue(proxy, "Description"));
            }

            CleanDb();
        }
Example #5
0
        public void GetInvalidValues_EntityWithInvalidProperty_ReturnsTheSameErrorsForEntityAsForProperty()
        {
            var group = new Group();
            var user  = new User
            {
                Name  = "qwerty",
                Group = group
            };


            InvalidValue[] userErrors = validatorEngine.Validate(user);

            userErrors.Count().Should().Be.EqualTo(1);

            InvalidValue[] userGroupErrors = validatorEngine.ValidatePropertyValue(user, "Group");

            userGroupErrors.Count().Should().Be.EqualTo(1);

            InvalidValue[] userNameErrors = validatorEngine.ValidatePropertyValue(user, "Name");

            userNameErrors.Count().Should().Be.EqualTo(0);
        }
        public void ShouldAddAnothersMessagesUsingValidationProperties()
        {
            var vtor = new ValidatorEngine();
            var mi = new MembershipInfo
            {
                Username = null,
                Password = "******"
            };

            InvalidValue[] invalidValues = vtor.ValidatePropertyValue(mi, x => x.Password);
            Assert.AreEqual(2, invalidValues.Length);
            Assert.AreEqual(Messages.PasswordLength, invalidValues.ElementAt(0).Message);
            Assert.AreEqual(Messages.PasswordContent, invalidValues.ElementAt(1).Message);
        }
Example #7
0
        public void ShouldAddAnothersMessagesUsingValidationProperties()
        {
            var vtor = new ValidatorEngine();
            var mi   = new MembershipInfo
            {
                Username = null,
                Password = "******"
            };

            InvalidValue[] invalidValues = vtor.ValidatePropertyValue(mi, x => x.Password);
            Assert.AreEqual(2, invalidValues.Length);
            Assert.AreEqual(Messages.PasswordLength, invalidValues.ElementAt(0).Message);
            Assert.AreEqual(Messages.PasswordContent, invalidValues.ElementAt(1).Message);
        }
        public void ValidatingHandler(object sender, CancelEventArgs e)
        {
            System.Type entityType = vvtor.GetEntityType((Control)sender);

            IControlValuable controlValuable = vvtor.Resolver.GetControlValuable(sender);

            InvalidValue[] errors =
                validatorEngine.ValidatePropertyValue(entityType, GetPropertyName((Control)sender), controlValuable.GetValue((Control)sender));

            if (errors.Length > 0)
            {
                errorProvider.SetError((TextBox)sender, errors[0].Message);
            }
            else
            {
                errorProvider.SetError((TextBox)sender, string.Empty);
            }
        }
        public void ValidatePropertyValueOfInstance()
        {
            var b = new BaseClass();
            var d = new DerivatedClass();

            var ve = new ValidatorEngine();

            Assert.AreEqual(1, ve.ValidatePropertyValue(b, "A").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue(d, "A").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue(b, e => e.A).Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue(d, e => e.A).Length);


            b.A = "1234";
            Assert.AreEqual(1, ve.ValidatePropertyValue(b, "A").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue(b, e => e.A).Length);
            d.A = "1234";
            Assert.AreEqual(1, ve.ValidatePropertyValue(d, "A").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue(d, e => e.A).Length);
            d.B = "123456";
            Assert.AreEqual(1, ve.ValidatePropertyValue(d, "B").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue(d, e => e.B).Length);
            d.B = null;
            Assert.AreEqual(0, ve.ValidatePropertyValue(d, "B").Length);
            Assert.AreEqual(0, ve.ValidatePropertyValue(d, e => e.B).Length);

            try
            {
                ve.ValidatePropertyValue(d, "WrongName");
                Assert.Fail("Intent to validate a wrong property don't throw any exception.");
            }
            catch (TargetException)
            {
                //ok
            }

            // same behavior GetInvalidValues(object)
            Assert.AreEqual(0, ve.ValidatePropertyValue(null, "A").Length);

            try
            {
                ve.ValidatePropertyValue(d, "");
                Assert.Fail("Intent to validate a empty property name don't throw any exception.");
            }
            catch (ArgumentNullException)
            {
                //ok
            }
        }
        public void ValidatePropertyValue()
        {
            var ve = new ValidatorEngine();

            Assert.AreEqual(1, ve.ValidatePropertyValue <BaseClass>("A", null).Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue <DerivatedClass>("A", null).Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue <BaseClass>("A", "1234").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue <DerivatedClass>("A", "1234").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue <DerivatedClass>("B", "123456").Length);
            Assert.AreEqual(0, ve.ValidatePropertyValue <DerivatedClass>("B", null).Length);

            Assert.AreEqual(1, ve.ValidatePropertyValue <BaseClass, string>(x => x.A, null).Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue <DerivatedClass, string>(x => x.A, null).Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue <BaseClass, string>(x => x.A, "1234").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue <DerivatedClass, string>(x => x.A, "1234").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue <DerivatedClass, string>(x => x.B, "123456").Length);
            Assert.AreEqual(0, ve.ValidatePropertyValue <DerivatedClass, string>(x => x.B, null).Length);

            try
            {
                ve.ValidatePropertyValue <DerivatedClass>("WrongName", null);
                Assert.Fail("Intent to validate a wrong property don't throw any exception.");
            }
            catch (TargetException)
            {
                //ok
            }
        }
        public void ValidatePropertyValueOfInstance()
        {
            var b = new BaseClass();
            var d = new DerivatedClass();

            var ve = new ValidatorEngine();
            Assert.AreEqual(1, ve.ValidatePropertyValue(b, "A").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue(d, "A").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue(b, e => e.A).Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue(d, e => e.A).Length);

            b.A = "1234";
            Assert.AreEqual(1, ve.ValidatePropertyValue(b, "A").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue(b, e => e.A).Length);
            d.A = "1234";
            Assert.AreEqual(1, ve.ValidatePropertyValue(d, "A").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue(d, e => e.A).Length);
            d.B = "123456";
            Assert.AreEqual(1, ve.ValidatePropertyValue(d, "B").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue(d, e => e.B).Length);
            d.B = null;
            Assert.AreEqual(0, ve.ValidatePropertyValue(d, "B").Length);
            Assert.AreEqual(0, ve.ValidatePropertyValue(d, e => e.B).Length);

            try
            {
                ve.ValidatePropertyValue(d, "WrongName");
                Assert.Fail("Intent to validate a wrong property don't throw any exception.");
            }
            catch (TargetException)
            {
                //ok
            }

            // same behavior GetInvalidValues(object)
            Assert.AreEqual(0, ve.ValidatePropertyValue(null, "A").Length);

            try
            {
                ve.ValidatePropertyValue(d, "");
                Assert.Fail("Intent to validate a empty property name don't throw any exception.");
            }
            catch (ArgumentNullException)
            {
                //ok
            }
        }
        public void ValidatePropertyValue()
        {
            var ve = new ValidatorEngine();
            Assert.AreEqual(1, ve.ValidatePropertyValue<BaseClass>("A", null).Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue<DerivatedClass>("A", null).Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue<BaseClass>("A", "1234").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue<DerivatedClass>("A", "1234").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue<DerivatedClass>("B", "123456").Length);
            Assert.AreEqual(0, ve.ValidatePropertyValue<DerivatedClass>("B", null).Length);

            Assert.AreEqual(1, ve.ValidatePropertyValue<BaseClass, string>(x => x.A, null).Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue<DerivatedClass, string>(x => x.A, null).Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue<BaseClass, string>(x => x.A, "1234").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue<DerivatedClass, string>(x => x.A, "1234").Length);
            Assert.AreEqual(1, ve.ValidatePropertyValue<DerivatedClass, string>(x => x.B, "123456").Length);
            Assert.AreEqual(0, ve.ValidatePropertyValue<DerivatedClass, string>(x => x.B, null).Length);

            try
            {
                ve.ValidatePropertyValue<DerivatedClass>("WrongName", null);
                Assert.Fail("Intent to validate a wrong property don't throw any exception.");
            }
            catch (TargetException)
            {
                //ok
            }
        }
        public void ValidateAnyClass()
        {
            ValidatorEngine ve = new ValidatorEngine();
            Assert.IsTrue(ve.IsValid(new AnyClass()));
            Assert.IsNotNull(ve.GetValidator<AnyClass>());
            ve.AssertValid(new AnyClass()); // not cause exception
            Assert.AreEqual(0, ve.Validate(new AnyClass()).Length);
            Assert.AreEqual(0, ve.ValidatePropertyValue<AnyClass>("aprop", new AnyClass()).Length);
            Assert.AreEqual(0, ve.ValidatePropertyValue(new AnyClass(), "aprop").Length);

            Assert.AreEqual(0, ve.Validate("always valid").Length);
            Assert.AreEqual(0, ve.ValidatePropertyValue("always valid", "Length").Length);
        }