private void Example5_Fluent()
        {
            // 5. EXAMPLE : Fluent validation.
            var val  = new ValidatorFluent(typeof(User));
            var user = new User();

            val.Check(() => user.UserName).IsNotNull().IsBetween(1, 50)
            .Check(() => user.CreateDate).IsAfterToday()
            .Check(() => user.Email).IsValidEmail()
            .Check(() => user.MobilePhone).IsValidPhoneUS();

            PrintErrors(val.Errors);
        }
Exemple #2
0
        public void CanValidateFluently()
        {
            var product   = new Product("MagicPencil", "draw like an artist", true, 10, 2, DateTime.Today.AddDays(5));
            var validator = new ValidatorFluent(product.GetType());

            validator.Check(product.Title).IsNotNull().IsBetween(1, 15)
            .Check(product.Description).IsNot(string.Empty).IsBetween(1, 25)
            .Check(product.Category).Min(1)
            .Check(product.Cost).Min(1).Max(500)
            .Check(product.IsInStock).IsTrue()
            .Check(product.AvailableDate).IsAfterToday().End();

            Assert.IsFalse(validator.HasErrors);
        }
            protected override IValidator GetValidator()
            {
                var val = new Validator((valEvent) =>
                {
                    var errors    = valEvent.Results;
                    var validator = new ValidatorFluent(this.GetType(), true, errors);
                    validator.Check(() => Name).IsNotNull().IsBetween(1, 50)
                    .Check(() => Age).Min(18).Max(65)
                    .Check(() => About).If(!string.IsNullOrEmpty(About)).IsBetween(1, 20)
                    .Check(() => IsLegalAge).IsTrue()
                    .Check(() => RegisterDate).IsAfterToday()
                    .Check(() => Email).IsValidEmail()
                    .Check(() => Phone).IsValidPhoneUS()
                    .Check(() => Url).IsValidUrl();

                    return(validator.HasErrors);
                });

                val.Validate();
                return(null);
            }