public void AssertValid()
 {
     ValidatorEngine ve = new ValidatorEngine();
     ve.AssertValid(new AnyClass()); // not cause exception
     try
     {
         Boo b = new Boo();
         ve.AssertValid(b);
         Assert.Fail("An invalid entity not throw exception.");
     }
     catch (InvalidStateException)
     {
         // OK
     }
 }
        public void IntegrationWithValidation()
        {
            ValidatorEngine ve = new ValidatorEngine();
            ve.AssertValid(new Foo(1));

            Assert.IsFalse(ve.IsValid(new Foo(3)));
        }
        public void AssertValid()
        {
            ValidatorEngine ve = new ValidatorEngine();

            ve.AssertValid(new AnyClass());             // not cause exception
            try
            {
                Boo b = new Boo();
                ve.AssertValid(b);
                Assert.Fail("An invalid entity not throw exception.");
            }
            catch (InvalidStateException)
            {
                // OK
            }
        }
        public void NullIsAllwaysValid()
        {
            ValidatorEngine ve = new ValidatorEngine();

            Assert.IsTrue(ve.IsValid(null));
            Assert.AreEqual(0, ve.Validate(null).Length);
            ve.AssertValid(null);             // not cause exception
        }
Exemple #5
0
        public void IntegrationWithValidation()
        {
            var ve = new ValidatorEngine();

            ve.AssertValid(new Foo(1));

            Assert.IsFalse(ve.IsValid(new Foo(3)));
        }
Exemple #6
0
        public void Should_Be_Able_To_Validate_A_Valid_Entity_With_Custom_Validator()
        {
            ValidatorEngine vtor = GetValidatorEngine();

            vtor.AssertValid(new Foo
            {
                Name = "Not null name"
            });
        }
        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 ValidateInitializedProxyAtDeepLevel()
        {
            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);
                NHibernateUtil.Initialize(proxy);
                Assert.That(engine.IsValid(new SimpleWithRelation {
                    Name = "OK", Relation = proxy
                }));
                Assert.DoesNotThrow(() => engine.AssertValid(new SimpleWithRelation {
                    Name = "OK", Relation = proxy
                }));
            }

            CleanDb();
        }
Exemple #9
0
        private static void Main(string[] args)
        {
            ///Configuration at Application Start-up.
            var container = new WindsorContainer();

            IoC.Container = container;
            //registering NHV out-of-the-box constraint validators
            WindsorConstraintValidatorFactory.RegisteringValidators(container);

            //registering your own constraint validators
            container.AddComponent(typeof(PersonNameValidator).Name.ToLower(), typeof(PersonNameValidator));
            ///End of IoC Configuration.

            var vtor = new ValidatorEngine();

            vtor.Configure();

            //Validating a valid entity 'Contact'
            var validCustomer = new Contact
            {
                FirstName   = "Dario",
                LastName    = "Quintana",
                Address     = "2nd Street at 34",
                Description = "Some description"
            };

            vtor.AssertValid(validCustomer);


            //Validating an invalid entity 'Contact'
            var invalidCustomer = new Contact
            {
                FirstName   = "dario" /*INVALID*/,
                LastName    = "Quintana",
                Address     = "2nd Street at 34",
                Description = "Some description"
            };

            Debug.Assert(vtor.Validate(invalidCustomer).Length == 1);
            Console.WriteLine("Done.");
            Console.ReadKey(true);
        }
Exemple #10
0
        private static void Main(string[] args)
        {
            ///Configuration at Application Start-up.
            var container = new WindsorContainer();
            IoC.Container = container;
            //registering NHV out-of-the-box constraint validators
            WindsorConstraintValidatorFactory.RegisteringValidators(container);

            //registering your own constraint validators
            container.AddComponent(typeof (PersonNameValidator).Name.ToLower(), typeof (PersonNameValidator));
            ///End of IoC Configuration.

            var vtor = new ValidatorEngine();
            vtor.Configure();

            //Validating a valid entity 'Contact'
            var validCustomer = new Contact
                                    {
                                        FirstName = "Dario",
                                        LastName = "Quintana",
                                        Address = "2nd Street at 34",
                                        Description = "Some description"
                                    };

            vtor.AssertValid(validCustomer);

            //Validating an invalid entity 'Contact'
            var invalidCustomer = new Contact
                                  	{
                                  		FirstName = "dario" /*INVALID*/,
                                  		LastName = "Quintana",
                                  		Address = "2nd Street at 34",
                                  		Description = "Some description"
                                  	};

            Debug.Assert(vtor.Validate(invalidCustomer).Length == 1);
            Console.WriteLine("Done.");
            Console.ReadKey(true);
        }
        public void ValidateNotInitializeProxyAtFirstLevel()
        {
            var validatorConf = new FluentConfiguration();

            validatorConf.SetDefaultValidatorMode(ValidatorMode.UseExternal);

            var vDefSimple = new ValidationDef <SimpleWithRelation>();

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

            var engine = new ValidatorEngine();

            engine.Configure(validatorConf);

            object savedId;

            // fill DB
            using (ISession s = OpenSession())
                using (ITransaction tx = s.BeginTransaction())
                {
                    savedId = s.Save(new SimpleWithRelation {
                        Name = "OK"
                    });
                    tx.Commit();
                }

            using (ISession s = OpenSession())
            {
                var proxy = s.Load <SimpleWithRelation>(savedId);
                Assert.That(engine.IsValid(proxy));
                Assert.DoesNotThrow(() => engine.AssertValid(proxy));
                Assert.That(!NHibernateHelper.IsInitialized(proxy), "should not initialize the proxy");
            }

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

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

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

            object savedId;
            // fill DB
            using (ISession s = OpenSession())
            using (ITransaction tx = s.BeginTransaction())
            {
                savedId = s.Save(new SimpleWithRelation { Name = "OK" });
                tx.Commit();
            }

            using (ISession s = OpenSession())
            {
                var proxy = s.Load<SimpleWithRelation>(savedId);
                Assert.That(engine.IsValid(proxy));
                Assert.DoesNotThrow(() => engine.AssertValid(proxy));
                Assert.That(!NHibernateUtil.IsInitialized(proxy), "should not initialize the proxy");
            }

            CleanDb();
        }
        public void ValidateInitializedProxyAtDeepLevel()
        {
            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);
                NHibernateUtil.Initialize(proxy);
                Assert.That(engine.IsValid(new SimpleWithRelation { Name = "OK", Relation = proxy }));
                Assert.DoesNotThrow(() => engine.AssertValid(new SimpleWithRelation { Name = "OK", Relation = proxy }));
            }

            CleanDb();
        }
        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 NullIsAllwaysValid()
 {
     ValidatorEngine ve = new ValidatorEngine();
     Assert.IsTrue(ve.IsValid(null));
     Assert.AreEqual(0, ve.Validate(null).Length);
     ve.AssertValid(null); // not cause exception
 }