Exemple #1
0
 public ValidatorTesterTester()
 {
     validator = new TestValidator();
     validator.RuleFor(x => x.CreditCard).Must(creditCard => !string.IsNullOrEmpty(creditCard)).WhenAsync((x, cancel) => Task.Run(() => { return(x.Age >= 18); }));
     validator.RuleFor(x => x.Forename).NotNull();
     validator.RuleForEach(person => person.NickNames).MinimumLength(5);
 }
 public ValidatorTesterTester()
 {
     validator = new TestValidator();
     validator.RuleFor(x => x.Forename).NotNull();
     validator.RuleForEach(person => person.NickNames).MinimumLength(5);
     CultureScope.SetDefaultCulture();
 }
 public ValidatorTesterTester()
 {
     validator = new TestValidator();
     validator.RuleFor(x => x).Must(NotHaveSameForenameAndSurname).WithMessage(sutSameForenameAndSurnameMessage);
     validator.RuleFor(x => x.CreditCard).Must(creditCard => !string.IsNullOrEmpty(creditCard)).WhenAsync((x, cancel) => Task.Run(() => { return(x.Age >= 18); }));
     validator.RuleFor(x => x.Forename).NotNull();
     validator.RuleForEach(person => person.NickNames).MinimumLength(5);
     CultureScope.SetDefaultCulture();
 }
        public void Async_condition_throws_when_executed_synchronosuly_with_synchronous_collection_role()
        {
            var validator = new TestValidator();

            validator.RuleForEach(x => x.NickNames).NotNull()
            .WhenAsync((x, token) => Task.FromResult(false));
            Assert.Throws <AsyncValidatorInvokedSynchronouslyException>(() =>
                                                                        validator.Validate(new Person {
                NickNames = new string[0]
            }));
        }
Exemple #5
0
        public void Can_Provide_severity_for_item_in_collection()
        {
            validator.RuleForEach(x => x.Children).NotNull().WithSeverity((person, child) => Severity.Warning);
            var result = validator.Validate(new Person {
                Children = new List <Person> {
                    null
                }
            });

            result.Errors[0].Severity.ShouldEqual(Severity.Warning);
        }
        public void Can_Provide_state_for_item_in_collection()
        {
            validator.RuleForEach(x => x.Children).NotNull().WithState((person, child) => "test");
            var result = validator.Validate(new Person {
                Children = new List <Person> {
                    null
                }
            });

            result.Errors[0].CustomState.ToString().ShouldEqual("test");
        }
        public void RuleForeach_with_null_instances()
        {
            var model = new Person {
                NickNames = new string[] { null }
            };

            validator.RuleForEach(x => x.NickNames).NotNull();
            var result = validator.Validate(model);

            Console.WriteLine(result.Errors[0].ErrorMessage);
            result.IsValid.ShouldBeFalse();
        }
Exemple #8
0
        public void Async_condition_executed_synchronosuly_with_synchronous_collection_role()
        {
            var validator = new TestValidator();

            validator.RuleForEach(x => x.NickNames).NotNull()
            .WhenAsync((x, token) => Task.FromResult(false));
            var result = validator.Validate(new Person {
                NickNames = new string[0]
            });

            result.IsValid.ShouldBeTrue();
        }
        public void Can_access_property_value_in_custom_condition_foreach()
        {
            var validator = new TestValidator();

            validator.RuleForEach(x => x.Orders).Must(v => false).Configure(cfg => {
                cfg.ApplyCondition(context => cfg.GetPropertyValue(context.InstanceToValidate) != null);
            });

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

            result.IsValid.ShouldBeTrue();

            result = validator.Validate(new Person {
                Orders = new List <Order> {
                    new Order()
                }
            });
            result.IsValid.ShouldBeFalse();
        }
        public void Ruleset_cascades_to_child_collection_validator()
        {
            var orderValidator = new InlineValidator <Order>();

            orderValidator.RuleSet("Test", () => {
                orderValidator.RuleFor(x => x.ProductName).NotNull();
            });

            var validator = new TestValidator();

            validator.RuleSet("Test", () => {
                validator.RuleForEach(x => x.Orders).SetValidator(orderValidator);
            });

            var person = new Person {
                Orders = { new Order(), new Order() }
            };

            var result = validator.Validate(new ValidationContext <Person>(person, new PropertyChain(), new RulesetValidatorSelector("Test")));


            result.Errors.Count.ShouldEqual(2);             //one for each order
            AssertExecuted(result, "Test");
        }