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


C# Validator.Validate方法代码示例

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


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

示例1: ShouldClearErrorMessageWhenPropertyValidationSucceed

        public void ShouldClearErrorMessageWhenPropertyValidationSucceed()
        {
            var validator = new Validator<DummyValidator>();

            var dummy = new DummyValidator();

            validator.Validate(dummy, "Name");

            dummy.Name = "Jose";

            validator.Validate(dummy, "Name");

            Assert.IsNullOrEmpty((string)validator.ErrorMessage);
        }
开发者ID:felipecvo,项目名称:boycott,代码行数:14,代码来源:ValidatorTest.cs

示例2: TestValidator

    public void TestValidator()
    {
      var validator = new Validator();

      Assert.AreEqual("", validator.Validate());
      Assert.AreEqual("", validator.Error);

      validator.AddValidationRule(() => TestInt).Condition(() => TestInt <= 0).Message("Test int validation message");
      validator.Validate();
      Assert.IsFalse(string.IsNullOrWhiteSpace(validator.Validate()));
      Assert.AreEqual("Test int validation message", validator.Error);

      validator.AddValidationRule(() => TestObject).Condition(() => TestObject == null).Message("Test object validation message");
      validator.Validate();
      Assert.IsTrue(validator.Error.Contains("Test int validation message"));
      Assert.IsTrue(validator.Error.Contains("Test object validation message"));

      TestInt = 100;
      validator.Validate();
      Assert.AreEqual("Test object validation message", validator.Error);
      Assert.AreEqual("Test object validation message", validator["TestObject"]);
      Assert.IsTrue(string.IsNullOrWhiteSpace(validator["TestInt"]));

      TestObject = new object();
      Assert.AreEqual("", validator.Validate());
      Assert.AreEqual("", validator.Error);

      TestObject = null;
      validator.Validate();
      Assert.AreEqual("Test object validation message", validator.Error);
      validator.RemoveValidationRule(() => TestObject);
      validator.Validate();
      Assert.IsTrue(string.IsNullOrWhiteSpace(validator.Error));
    }
开发者ID:dingjimmy,项目名称:Caliburn.Micro.FluentValidation,代码行数:34,代码来源:ValidatorTest.cs

示例3: ShouldFailValidationWithAllErrorsAndPass

        public void ShouldFailValidationWithAllErrorsAndPass()
        {
            var validator = new Validator<DummyValidator>();

            var dummy = new DummyValidator();

            validator.Validate(dummy);

            dummy.Name = "Me";
            dummy.Email = "[email protected]";

            Assert.IsTrue(validator.Validate(dummy));
            Assert.IsFalse(validator.HasErrors);
            Assert.IsNullOrEmpty((string)validator.ErrorMessage);
        }
开发者ID:felipecvo,项目名称:boycott,代码行数:15,代码来源:ValidatorTest.cs

示例4: Validate_Should_Return_Error_When_Referenced_Collection_Of_Objects_Have_Errors

        public void Validate_Should_Return_Error_When_Referenced_Collection_Of_Objects_Have_Errors()
        {
            // Arrange
            var testClass =
                new TestClassWithCollectionOfReferencesToAnotherClass
                {
                    TestProperty =
                        new[]
                        {
                            new TestClass { TestProperty1 = "ABC", TestProperty2 = null },
                            new TestClass { TestProperty1 = "ABC", TestProperty2 = "ABC"},
                            new TestClass { TestProperty1 = null, TestProperty2 = "ABC"}
                        }
                };

            var validator = new Validator();

            // Act
            var errors = validator.Validate(testClass);

            // Assert
            Assert.IsNotNull(errors);
            Assert.AreEqual(testClass, errors.Object);
            Assert.AreEqual(2, errors.Errors.Length);

            Assert.AreEqual("TestProperty2", errors.Errors[0].Key);
            Assert.AreEqual("Error2", errors.Errors[0].Message);
            Assert.AreEqual("TestProperty1", errors.Errors[1].Key);
            Assert.AreEqual("Error1", errors.Errors[1].Message);
        }
开发者ID:dilterporto,项目名称:Validator,代码行数:30,代码来源:ValidatorTests.cs

示例5: Validate_Should_Return_Errors

        public void Validate_Should_Return_Errors()
        {
            // Arrange
            var validator = new Validator();
            var testClass = new TestClassWithAttrs();

            // Act
            var errors = validator.Validate(testClass);

            // Assert
            Assert.AreSame(testClass, errors.Object);
            Assert.AreEqual(10, errors.Errors.Length);

            CollectionAssert.AreEquivalent(
                new[]
                    {
                        new ValidationError("staticproperty", "Static_Property"),
                        new ValidationError("publicproperty", "Public_Property"),
                        new ValidationError("internalproperty", "Internal_Property"),
                        new ValidationError("protectedproperty", "Protected_Property"),
                        new ValidationError("privateproperty", "Private_Property"),

                        new ValidationError("staticmethod", "Static_Method"),
                        new ValidationError("publicmethod", "Public_Method"),
                        new ValidationError("internalmethod", "Internal_Method"),
                        new ValidationError("protectedmethod", "Protected_Method"),
                        new ValidationError("privatemethod", "Private_Method")
                    },
                errors.Errors);
        }
开发者ID:dilterporto,项目名称:Validator,代码行数:30,代码来源:ValidatorTests.cs

示例6: BindModel

		public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
		{
			var form = controllerContext.HttpContext.Request.Form;

			var order = new Order
			{
				//OrderStatusId = OrderStatus.CreatedId,
				CreatedDate = DateTime.Now,
				DispatchedDate = DateTime.Now
			};

			try
			{
				var validator = new Validator
				{
					() => UpdateOrder(order, form, bindingContext.ModelState),
					() => UpdateCardContact(order, form, bindingContext.ModelState),
					() => UpdateDeliveryContact(order, form, bindingContext.ModelState),
					() => UpdateCard(order, form, bindingContext.ModelState)
				};

				validator.Validate();
			}
			catch (ValidationException)
			{
				//Ignore validation exceptions - they will be stored in ModelState.
			}

		
			EnsureBasketCountryId(order);

			return order;
		}
开发者ID:bertusmagnus,项目名称:Sutekishop,代码行数:33,代码来源:OrderBinder.cs

示例7: ShouldCopyToModelState

		public void ShouldCopyToModelState()
		{
			var modelstate = new ModelStateDictionary();

			const string property = "";

			var validator = new Validator
            {
            	() => property.Label("Property 1").IsRequired(),
            	() => property.Label("Property 2").IsRequired(),
            	() => property.Label("Property 3").IsRequired()
            };

			try
			{
				validator.Validate();
			}
			catch (ValidationException ex)
			{
				ex.CopyToModelState(modelstate, "foo");
			}

			modelstate.Count.ShouldEqual(3);
			modelstate["foo.Property 1"].ShouldNotBeNull();
			modelstate["foo.Property 2"].ShouldNotBeNull();
			modelstate["foo.Property 3"].ShouldNotBeNull();

		}
开发者ID:bertusmagnus,项目名称:Sutekishop,代码行数:28,代码来源:ValidationExtensionsTests.cs

示例8: Validate_1CharacterPassword_FailValidation

    public void Validate_1CharacterPassword_FailValidation()
    {
      var settings = new PasswordValidationSettings {MinimumPasswordLength = 8};
      var validator = new Validator(settings);
      var actualResult = validator.Validate("1");

      Assert.IsFalse(actualResult);
    }
开发者ID:Nuschler,项目名称:Xander.PasswordValidator,代码行数:8,代码来源:ValidatorTests.cs

示例9: Validate_8CharacterPassword_PassValidaton

    public void Validate_8CharacterPassword_PassValidaton()
    {
      var settings = new PasswordValidationSettings { MinimumPasswordLength = 8 };
      var validator = new Validator(settings);
      var actualResult = validator.Validate("12345678");

      Assert.IsTrue(actualResult);
    }
开发者ID:Nuschler,项目名称:Xander.PasswordValidator,代码行数:8,代码来源:ValidatorTests.cs

示例10: Validate

        public void Validate()
        {
            Validator validator = new Validator
            {
                () => Email.Label("Email").IsRequired(),
                () => Password.Label("Password").IsRequired(),
            };

            validator.Validate();
        }
开发者ID:bertusmagnus,项目名称:Sutekishop,代码行数:10,代码来源:User.cs

示例11: ShouldFailValidationWithAllErrors

        public void ShouldFailValidationWithAllErrors()
        {
            var validator = new Validator<DummyValidator>();

            var dummy = new DummyValidator();

            Assert.IsFalse(validator.Validate(dummy));
            Assert.IsTrue(validator.HasErrors);
            Assert.IsTrue(validator.ErrorMessage.ToString().Contains("Name can't be blank"));
            Assert.IsTrue(validator.ErrorMessage.ToString().Contains("Email can't be blank"));
        }
开发者ID:felipecvo,项目名称:boycott,代码行数:11,代码来源:ValidatorTest.cs

示例12: ShouldFailValidationWithOneError

        public void ShouldFailValidationWithOneError()
        {
            var validator = new Validator<DummyValidator>();

            var dummy = new DummyValidator();

            dummy.Name = "Josh";

            Assert.IsFalse(validator.Validate(dummy));
            Assert.IsTrue(validator.HasErrors);
            Assert.AreEqual("Email can't be blank", (string)validator.ErrorMessage);
        }
开发者ID:felipecvo,项目名称:boycott,代码行数:12,代码来源:ValidatorTest.cs

示例13: Test_Validate_Should_Regex_Error

        public void Test_Validate_Should_Regex_Error()
        {
            var myClass = new MyClass()
            {
                Email = "notEmail"
            };

            var validator = new Validator();
            var validationErrors = validator.Validate(myClass);

            Assert.IsNotNull(validationErrors.Errors.FirstOrDefault(d => d.Key == "Email"));
        }
开发者ID:dilterporto,项目名称:Validator,代码行数:12,代码来源:QuickStartTests.cs

示例14: CollectingExceptionsSpike

		public void CollectingExceptionsSpike()
		{
			const string property = "";

			var validator = new Validator
			                {
			                	() => property.Label("Property 1").IsRequired(),
			                	() => property.Label("Property 2").IsRequired(),
			                	() => property.Label("Property 3").IsRequired()
			                };

			validator.Validate();
		}
开发者ID:bertusmagnus,项目名称:Sutekishop,代码行数:13,代码来源:ValidationExtensionsTests.cs

示例15: CollectingExceptionsSpike

		public void CollectingExceptionsSpike()
		{
			var validator = new Validator
			                {
			                	() => { throw new ValidationException("the first one"); },
			                	() => { throw new ValidationException("the second one"); },
			                	() => { throw new ValidationException("the third one"); },
			                };

		    var modelState = new ModelStateDictionary();
			validator.Validate(modelState);

            modelState["validation_error_0"].Errors[0].ErrorMessage.ShouldEqual("the first one");
            modelState["validation_error_1"].Errors[0].ErrorMessage.ShouldEqual("the second one");
            modelState["validation_error_2"].Errors[0].ErrorMessage.ShouldEqual("the third one");
		}
开发者ID:sthapa123,项目名称:sutekishop,代码行数:16,代码来源:ValidationExtensionsTests.cs


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