当前位置: 首页>>代码示例>>C#>>正文


C# ValidatorEngine.Validate方法代码示例

本文整理汇总了C#中ValidatorEngine.Validate方法的典型用法代码示例。如果您正苦于以下问题:C# ValidatorEngine.Validate方法的具体用法?C# ValidatorEngine.Validate怎么用?C# ValidatorEngine.Validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ValidatorEngine的用法示例。


在下文中一共展示了ValidatorEngine.Validate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ValidateWithMultipleConditions

        public void ValidateWithMultipleConditions()
        {
            var configure = new FluentConfiguration();

            var validationDef = new ValidationDef<EntityWithString>();
            validationDef.Define(e => e.Name)
                .Satisfy(name => name != null && name.StartsWith("ab")).WithMessage("Name should start with 'ab'")
                .And
                .Satisfy(name => name != null && name.EndsWith("zz")).WithMessage("Name should end with 'zz'");

            configure.Register(validationDef).SetDefaultValidatorMode(ValidatorMode.UseExternal);
            var ve = new ValidatorEngine();

            ve.Configure(configure);

            Assert.That(ve.IsValid(new EntityWithString { Name = "abczz" }));
            Assert.That(!ve.IsValid(new EntityWithString { Name = "bc" }));
            var iv = ve.Validate(new EntityWithString {Name = "abc"});
            Assert.That(iv.Length, Is.EqualTo(1));
            Assert.That(iv.Select(i => i.Message).First(), Is.EqualTo("Name should end with 'zz'"));

            iv = ve.Validate(new EntityWithString { Name = "zz" });
            Assert.That(iv.Length, Is.EqualTo(1));
            Assert.That(iv.Select(i => i.Message).First(), Is.EqualTo("Name should start with 'ab'"));

            iv = ve.Validate(new EntityWithString { Name = "bc" });
            Assert.That(iv.Length, Is.EqualTo(2));
            var messages = iv.Select(i => i.Message);
            Assert.That(messages, Has.Member("Name should start with 'ab'") & Has.Member("Name should end with 'zz'"));
        }
开发者ID:spib,项目名称:nhcontrib,代码行数:30,代码来源:StringConstraintsTests.cs

示例2: NoEndlessLoop

        public void NoEndlessLoop()
        {
            var john = new User("John", null);
            john.Knows(john);

            var validator = new ValidatorEngine();

            InvalidValue[] constraintViolations = validator.Validate(john);
            Assert.AreEqual(constraintViolations.Length, 1, "Wrong number of constraints");
            Assert.AreEqual("LastName", constraintViolations.ElementAt(0).PropertyName);

            var jane = new User("Jane", "Doe");
            jane.Knows(john);
            john.Knows(jane);

            constraintViolations = validator.Validate(john);
            Assert.AreEqual(constraintViolations.Length, 1, "Wrong number of constraints");
            Assert.AreEqual("LastName", constraintViolations.ElementAt(0).PropertyName);

            constraintViolations = validator.Validate(jane);
            Assert.AreEqual(1, constraintViolations.Length, "Wrong number of constraints");
            Assert.AreEqual(constraintViolations.ElementAt(0).PropertyPath, "knowsUser[0].LastName");

            john.LastName = "Doe";
            constraintViolations = validator.Validate(john);
            Assert.AreEqual(0, constraintViolations.Length, "Wrong number of constraints");
        }
开发者ID:hazzik,项目名称:nh-contrib-everything,代码行数:27,代码来源:Fixture.cs

示例3: can_validate_legs

        public void can_validate_legs()
        {
            var validationDef = new ValidationDef<Cat>();
            validationDef.Define(c => c.Legs).GreaterThanOrEqualTo(2);

            var vc = new FluentConfiguration();
            vc.SetDefaultValidatorMode(ValidatorMode.UseExternal);
            vc.Register(validationDef);

            var ve = new ValidatorEngine();
            ve.Configure(vc);

            ve.Validate(new Cat {Legs = 0}).Should().Have.Count.EqualTo(1);
            ve.Validate(new Cat {Legs = 3}).Should().Be.Empty();
        }
开发者ID:hazzik,项目名称:nh-contrib-everything,代码行数:15,代码来源:Fixture.cs

示例4: InterpolatingMemberAndSubMembers

        public void InterpolatingMemberAndSubMembers()
        {
            var c = new Contractor
            {
                SubcontractorHourEntries = new List<SubcontractorHourEntry>
                                                       	{
                                                       		new SubcontractorHourEntry
                                                       			{
                                                       				Contrator = new SubContractor(1),
                                                       				Hour = 9
                                                       			},
                                                       		new SubcontractorHourEntry
                                                       			{
                                                       				Contrator = new SubContractor(2),
                                                       				Hour = 10
                                                       			}
                                                       	}
            };

            var vtor = new ValidatorEngine();
            Assert.IsFalse(vtor.IsValid(c));
            var values = vtor.Validate(c);
            Assert.AreEqual(1, values.Length);
            Assert.AreEqual("The min value in the SubContractor Id: 1 is invalid. Instead was found: 9", values[0].Message);
        }
开发者ID:spib,项目名称:nhcontrib,代码行数:25,代码来源:InterpolatingValuesFixture.cs

示例5: when_validate_customer_with_invalid_name_and_email_should_return_two_invalid_values

        public void when_validate_customer_with_invalid_name_and_email_should_return_two_invalid_values()
        {
            var validatorEngine = new ValidatorEngine();
            var notValidCustomer = GetNotValidCustomer();
            validatorEngine.Configure();

            validatorEngine.Validate(notValidCustomer).Should().Have.Count.EqualTo(2);
        }
开发者ID:spib,项目名称:nhcontrib,代码行数:8,代码来源:ValidComponentTester.cs

示例6: InterpolatingMemberAndSubMembers

        public void InterpolatingMemberAndSubMembers()
        {
            var animal = new Animal {Legs = 1, Name = "Horse"};

            var vtor = new ValidatorEngine();
            var values = vtor.Validate(animal);
            Assert.AreEqual(1, values.Length);
            //The legs validator add a error message for the property Legs.
            Assert.AreEqual("Legs", values[0].PropertyName);
        }
开发者ID:hazzik,项目名称:nh-contrib-everything,代码行数:10,代码来源:Fixture.cs

示例7: DelegatedValidate_WithoutMessageHasInvalidValue

        public void DelegatedValidate_WithoutMessageHasInvalidValue()
        {
            var configure = new FluentConfiguration();
            configure.Register(new[] { typeof(RangeDefWithoutCustomMessage) })
                .SetDefaultValidatorMode(ValidatorMode.UseExternal);
            var ve = new ValidatorEngine();

            ve.Configure(configure);
            var iv = ve.Validate(new Range {Start = 5, End = 4});
            iv.Should().Not.Be.Empty();
        }
开发者ID:spib,项目名称:nhcontrib,代码行数:11,代码来源:EngineIntegrationTest.cs

示例8: ShouldAddAnothersMessages

        public void ShouldAddAnothersMessages()
        {
            var vtor = new ValidatorEngine();
            var mi = new MembershipInfo
                     	{
                     		Username = null,
                     		Password = "123X"
                     	};

            InvalidValue[] invalidValues = vtor.Validate(mi);
            Assert.AreEqual(3, invalidValues.Length);
        }
开发者ID:hazzik,项目名称:nh-contrib-everything,代码行数:12,代码来源:ConstraintContextIntegrationFixture.cs

示例9: Validate

        public void Validate()
        {
            ve = new ValidatorEngine();

            Run(delegate
            {
                for (int i = 0; i < ITERATIONS; i++)
                {
                    Thread t = new Thread(delegate() { ve.Validate(new Foo()); });

                    t.Start();
                }
            });
        }
开发者ID:spib,项目名称:nhcontrib,代码行数:14,代码来源:ThreadSafeFixture.cs

示例10: CopositionUsingEngine

        public void CopositionUsingEngine()
        {
            DerivatedClass dFullBroken = new DerivatedClass();
            dFullBroken.A = "1234";
            dFullBroken.B = "1234";

            DerivatedClass dPartialBroken = new DerivatedClass();
            dPartialBroken.A = "1234";

            BaseClass bOk = new BaseClass();
            bOk.A = "123";

            BaseClass bBroken = new BaseClass();
            bBroken.A = "1234";

            ValidatorEngine ve = new ValidatorEngine();
            InvalidValue[] ivalues;
            Composition c = new Composition();

            c.Any = bBroken;
            ivalues = ve.Validate(c);
            Assert.AreEqual(1, ivalues.Length);

            c.Any = dFullBroken;
            ivalues = ve.Validate(c);
            Assert.AreEqual(2, ivalues.Length);

            c.Any = bOk;
            ivalues = ve.Validate(c);
            Assert.AreEqual(0, ivalues.Length);

            c.Any = dPartialBroken;
            ivalues = ve.Validate(c);
            Assert.AreEqual(1, ivalues.Length);
            Assert.AreEqual("A", ivalues[0].PropertyName);
        }
开发者ID:mpielikis,项目名称:nhibernate-contrib,代码行数:36,代码来源:PolimorphismFixture.cs

示例11: ShouldDisableTheDefaultMessageAndAddAnothers

        public void ShouldDisableTheDefaultMessageAndAddAnothers()
        {
            var vtor = new ValidatorEngine();
            var mi = new MembershipInfo
                     	{
                     		Username = "MyName",
                            Password = "123X"
                     	};

            InvalidValue[] invalidValues = vtor.Validate(mi);

            Assert.AreEqual(2, invalidValues.Length);
            Assert.AreEqual(Messages.PasswordLength, invalidValues.ElementAt(0).Message);
            Assert.AreEqual(Messages.PasswordContent, invalidValues.ElementAt(1).Message);
        }
开发者ID:hazzik,项目名称:nh-contrib-everything,代码行数:15,代码来源:ConstraintContextIntegrationFixture.cs

示例12: ShouldAddAnothersMessagesUsingEntityValidation

        public void ShouldAddAnothersMessagesUsingEntityValidation()
        {
            var vtor = new ValidatorEngine();
            var mi = new MembershipInfo2
                     	{
                     		Username = null,
                     		Password = "123X"
                     	};

            InvalidValue[] invalidValues = vtor.Validate(mi);
            Assert.AreEqual(3, invalidValues.Length);

            Assert.AreEqual(Messages.PasswordLength, invalidValues.ElementAt(1).Message);
            Assert.AreEqual(Messages.PasswordContent, invalidValues.ElementAt(2).Message);
        }
开发者ID:hazzik,项目名称:nh-contrib-everything,代码行数:15,代码来源:ConstraintContextIntegrationFixture.cs

示例13: GraphNavigationDeterminism

        public void GraphNavigationDeterminism()
        {
            // build the test object graph
            var user = new User("John", "Doe");

            var address1 = new Address(null, "11122", "Stockholm");
            address1.SetInhabitant(user);

            var address2 = new Address("Kungsgatan 5", "11122", "Stockholm");
            address2.SetInhabitant(user);

            user.AddAddress(address1);
            user.AddAddress(address2);

            var order = new Order(1);
            order.ShippingAddress = address1;
            order.BillingAddress = address2;
            order.Customer = user;

            var line1 = new OrderLine(order, 42);
            var line2 = new OrderLine(order, 101);
            order.AddOrderLine(line1);
            order.AddOrderLine(line2);

            var vtor = new ValidatorEngine();

            InvalidValue[] constraintViolations = vtor.Validate(order);
            Assert.AreEqual(3, constraintViolations.Length, "Wrong number of constraints");

            var expectedErrorMessages = new List<string>();
            expectedErrorMessages.Add("shippingAddress.addressline1");
            expectedErrorMessages.Add("customer.addresses[0].addressline1");
            expectedErrorMessages.Add("billingAddress.inhabitant.addresses[0].addressline1");

            foreach (InvalidValue violation in constraintViolations)
            {
                if (expectedErrorMessages.Contains(violation.PropertyPath))
                {
                    expectedErrorMessages.Remove(violation.PropertyPath);
                }
            }

            Assert.IsTrue(expectedErrorMessages.Count == 0, "All error messages should have occured once");
        }
开发者ID:hazzik,项目名称:nh-contrib-everything,代码行数:44,代码来源:Fixture.cs

示例14: Main

        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);
        }
开发者ID:spib,项目名称:nhcontrib,代码行数:39,代码来源:Program.cs

示例15: Run_Example_With_Invalid_Entity

        private static void Run_Example_With_Invalid_Entity(ValidatorEngine vtor)
        {
            Console.WriteLine("==Entity with Invalid values==");
            var m = new Meeting
                        {
                            Name = "NHibernate Validator virtual meeting",
                            Description = "How to configure NHibernate Validator in different ways.",
                            Start = DateTime.Now.AddHours(2), //Invalid: the Start date is minor than End.
                            End = DateTime.Now
                        };

            var invalidValues = vtor.Validate(m);
            if (invalidValues.Length == 0)
                throw new Exception("Something was wrong in the NHV configuration, the entity should be invalid.");
            else
            {
                Console.WriteLine("The entity is invalid.");
                foreach (var value in invalidValues)
                {
                    Console.WriteLine(" - " + value.Message);
                }
            }
        }
开发者ID:hazzik,项目名称:nh-contrib-everything,代码行数:23,代码来源:Program.cs


注:本文中的ValidatorEngine.Validate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。