public void Nested_when_inside_otherwise()
        {
            var validator = new InlineValidator <Person>();

            validator.When(x => x.Id == 1, () => {
                validator.RuleFor(x => x.Forename).NotNull();
            }).Otherwise(() => {
                validator.When(x => x.Age > 18, () => {
                    validator.RuleFor(x => x.Email).NotNull();
                });
            });

            var result = validator.Validate(new Person()
            {
                Id = 1
            });

            result.Errors.Count.ShouldEqual(1);
            result.Errors[0].PropertyName.ShouldEqual("Forename");

            result = validator.Validate(new Person()
            {
                Id = 2, Age = 20
            });
            result.Errors.Count.ShouldEqual(1);
            result.Errors[0].PropertyName.ShouldEqual("Email");
        }
Exemple #2
0
        public void Nested_conditions_Rule_For()
        {
            var validator = new InlineValidator <request>();

            validator.When(r => true, () => {
                validator.When(r => r.person?.NickNames?.Any() == true, () => {
                    validator.RuleFor(r => r.person.NickNames)
                    .Must(nn => true)
                    .WithMessage("Failed RuleFor");
                });
            });
            var result = validator.Validate(new request());

            result.IsValid.ShouldBeTrue();
        }
        public void When_condition_executed_for_each_instance_of_RuleForEach_condition_should_not_be_cached()
        {
            var person = new Person {
                Children = new List <Person> {
                    new Person {
                        Id = 1
                    },
                    new Person {
                        Id = 0
                    }
                }
            };

            var childValidator = new InlineValidator <Person>();
            int executions     = 0;

            childValidator.When(a => {
                executions++;
                return(a.Id != 0);
            }, () => {
                childValidator.RuleFor(a => a.Id).Equal(1);
            });
            var personValidator = new InlineValidator <Person>();

            personValidator.RuleForEach(p => p.Children).SetValidator(childValidator);

            var validationResult = personValidator.Validate(person);

            validationResult.IsValid.ShouldBeTrue();
            executions.ShouldEqual(2);
        }
Exemple #4
0
        public void Nested_conditions_Rule_For_Each()
        {
            var validator = new InlineValidator <request>();

            validator.When(x => true, () => {
                validator.When(r => r.person?.NickNames?.Any() == true, () => {
                    validator.RuleForEach(x => x.person.NickNames)
                    .Must(nn => true)
                    .WithMessage("Failed RuleForEach");
                });
            });

            var result = validator.Validate(new request());

            result.Errors.Count.ShouldEqual(0);
        }
		public void Nested_collection_for_null_property_should_not_throw_null_reference() {
			var validator = new InlineValidator<request>();
			validator.When(r => r.person != null, () => {
				validator.RuleForEach(x => x.person.NickNames).NotNull();
			});

			var result = validator.Validate(new request());
			result.Errors.Count.ShouldEqual(0);
		}
Exemple #6
0
        public void Nested_collection_for_null_property_should_not_throw_null_reference()
        {
            var validator = new InlineValidator <request>();

            validator.When(r => r.person != null, () => { validator.RuleForEach(x => x.person.NickNames).NotNull(); });

            var result = validator.Validate(new request());

            result.Errors.Count.ShouldEqual(0);
        }
Exemple #7
0
        public void Shouldnt_break_with_hashcode_collision()
        {
            var v1 = new InlineValidator <Collision1>();
            var v2 = new InlineValidator <Collision2>();


            var v = new InlineValidator <CollisionBase>();

            v.When(x => x is Collision1, () => {
                v.RuleFor(x => ((Collision1)x).Name).NotNull();
            });
            v.When(x => x is Collision2, () => {
                v.RuleFor(x => ((Collision2)x).Name).NotNull();
            });

            // shouldn't throw an InvalidCastException.
            var containerValidator = new InlineValidator <List <CollisionBase> >();

            containerValidator.RuleForEach(x => x).SetValidator(v);
            containerValidator.Validate(new List <CollisionBase> {
                new Collision1(), new Collision2()
            });
        }