public void Should_format_validation_with_property_values() {
			const string expected = "Property Name: Surname Custom: Foo";
			validator.RuleFor(x => x.Surname).NotNull().WithMessage("Property Name: {PropertyName} Custom: {0}", x => x.Forename);
			var person = new Person { Forename = "Foo" };

			string error = validator.Validate(person).Errors.Single().ErrorMessage;
			error.ShouldEqual(expected);
		}
		public MemberAccessorTests() {
			person = new Person();
			person.NameField = "John Smith";
			person.Forename = "John";
			person.Address = new Address {Country = new Country {Name = "United States"}};

			nameFieldAccessor = MemberAccessor<Person>.From(x => x.NameField);
			forenameAccessor = MemberAccessor<Person>.From(x => x.Forename);
			countryNameAccessor = MemberAccessor<Person>.From(x => x.Address.Country.Name);
		}
        public void Calling_ValidateAsync_should_delegate_to_underlying_sync_validator()
        {
            var person = new Person { Surname = "Foo" };
            var validator = new Mock<IPropertyValidator>();
            builder.SetValidator(validator.Object);

            builder.Rule.ValidateAsync(new ValidationContext<Person>(person, new PropertyChain(), new DefaultValidatorSelector())).Result.ToList();

            validator.Verify(x => x.Validate(It.Is<PropertyValidatorContext>(c => (string)c.PropertyValue == "Foo")));
        }
        public void Calling_ValidateAsync_should_delegate_to_underlying_async_validator()
        {
            var person = new Person { Surname = "Foo" };
            var validator = new Mock<AsyncValidatorBase>(MockBehavior.Loose, Messages.predicate_error) {CallBase = true};
            validator.Setup(v => v.ValidateAsync(It.IsAny<PropertyValidatorContext>())).Returns(TaskHelpers.FromResult(Enumerable.Empty<ValidationFailure>()));
            builder.SetValidator(validator.Object);

            builder.Rule.ValidateAsync(new ValidationContext<Person>(person, new PropertyChain(), new DefaultValidatorSelector())).Result.ToList();

            validator.Verify(x => x.ValidateAsync(It.Is<PropertyValidatorContext>(c => (string)c.PropertyValue == "Foo")));
        }
		public void Passes_object_being_validated_to_action() {
			var person = new Person();
			Person validatedPerson = null;

			validator.RuleFor(x => x.Surname).NotNull().OnAnyFailure(x => {
				validatedPerson = x;
			});

			validator.Validate(person);

			person.ShouldBeTheSameAs(validatedPerson);
		}
		public void Executes_rule_for_each_item_in_collection() {
			var validator = new TestValidator {
				v => v.RuleForEach(x => x.NickNames).NotNull()
			};

			var person = new Person {
				NickNames =  new[] { null, "foo", null }
			};

			var result = validator.Validate(person);
			result.Errors.Count.ShouldEqual(2);
		}
Пример #7
0
		public void Correctly_provides_object_being_validated() {
			Person resultPerson = null;

			validator.RuleFor(x => x.Surname).NotNull().WithState(x => {
				resultPerson = x;
				return new object();
			});

			var person = new Person();
			validator.Validate(person);

			resultPerson.ShouldBeTheSameAs(person);
		}
		public void Correctly_gets_collection_indicies() {
			var validator = new TestValidator {
				v => v.RuleForEach(x => x.NickNames).NotNull()
			};

			var person = new Person {
				NickNames = new[] { null, "foo", null }
			};

			var result = validator.Validate(person);
			result.Errors[0].PropertyName.ShouldEqual("NickNames[0]");
			result.Errors[1].PropertyName.ShouldEqual("NickNames[2]");
		}
 public void Should_compile_expression()
 {
     var person = new Person {Surname = "Foo"};
     builder.Rule.PropertyFunc(person).ShouldEqual("Foo");
 }
        public void Does_not_throw_when_valid_and_a_ruleset_async()
        {
            var validator = new TestValidator {
                v => v.RuleFor(x => x.Surname).NotNull()
            };

            const string ruleSetName = "blah";
            validator.RuleSet(ruleSetName, () => {
                validator.RuleFor(x => x.Forename).NotNull();
            });

            var person = new Person
            {
                Forename = "foo",
                Surname = "foo"
            };
            validator.ValidateAndThrowAsync(person, ruleSetName).Wait();
        }
Пример #11
0
		public void Calling_ValidateAsync_should_delegate_to_underlying_sync_validator() {
#if CoreCLR
			Assert.True(false, "No mocking on coreclr");
#else
			var person = new Person { Surname = "Foo" };
			var validator = new Mock<IPropertyValidator>();
			builder.SetValidator(validator.Object);

			builder.Rule.ValidateAsync(new ValidationContext<Person>(person, new PropertyChain(), new DefaultValidatorSelector()), new CancellationToken()).Result.ToList();

			validator.Verify(x => x.Validate(It.Is<PropertyValidatorContext>(c => (string)c.PropertyValue == "Foo")));

#endif
		}
Пример #12
0
		public void Should_compile_expression() {
			var person = new Person {Surname = "Foo"};
			builder.Rule.PropertyFunc(person).ShouldEqual("Foo");
		}