Exemple #1
0
        public void TestMessagesCount()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <MyMessageTestClass>()
            .WithMessage("Object is not valid")
            .Setup(m => m.A)
            .WithMessage("Property A is not valid.")
            .MustBeBetween(1, 10)
            .Setup(m => m.B)
            .WithMessage("Property B is not valid.")
            .MustBeBetween(1, 10);

            var engine = builder.Build();
            var report = new ValidationReport(engine);
            var obj    = new MyMessageTestClass(0, 0);

            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            Assert.AreEqual(2, report.GetErrorMessages(obj).Length);

            obj = new MyMessageTestClass(0, 0);
            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            Assert.AreEqual(2, report.GetErrorMessages(obj).Length);

            //There should be a total of 4 error messages...
            Assert.AreEqual(4, report.GetErrorMessages().Length);
        }
        public static void RunErrorMessages(Employee employee)
        {
            var engine = new Engine();
            engine.For<Person>()
                    .Setup(m => m.Name)
                        .MustNotBeNullOrEmpty()
                        .WithMessage("Name must not be empty") //Only when name is null or empty
                        .MustMatchRegex(@"^[a-z]+$")
                        .WithMessage("Name contains invalid characters") //Only when name contains invalid characters

                    .Setup(m => m.Email)
                        .WithMessage("Invalid email address") //For any rule breakers on the Email property.
                        .MustNotBeNullOrEmpty()
                        .MustMatchRegex(@"^[a-z]+@[a-z]+(\.[a-z]+)*$");

            engine.For<Employee>() //Member will inherit rules set for a Person.
                    .Setup(m => m.EmployeeNumber)
                        .WithMessage("Employee number is not valid")
                        .MustBeGreaterThan(0);

            var report = new ValidationReport(engine);
            var result = report.Validate(employee).ToString();
            var errors = report.GetErrorMessages(employee);
            employee.IsValid = result;
            string flatErrors = string.Empty;
            foreach (string s in errors)
                flatErrors += s;
            employee.Errors = flatErrors;
        }
Exemple #3
0
        public Result Validate(object obj)
        {
            var error = "";
            var valid = report.Validate(obj);

            if (!valid)
            {
                var errors = report.GetErrorMessages();
                if (errors.Any())
                {
                    error = report.GetErrorMessages().Aggregate((i, j) => i + ", " + j);
                }
            }

            return(new Result
            {
                Success = rulesEngine.Validate(obj),
                Error = error
            });
        }
Exemple #4
0
        public ActionResult Demo()
        {
            Engine   engine   = new Engine();
            Employee employee = new Employee()
            {
                //FirstName = "Alan",
                LastName = "Wei",
                Age      = 10
                           //, Email = "abc"
            };

            engine.For <Employee>().Setup(e => e.FirstName).MustNotBeNullOrEmpty().WithMessage("{0}不能为空")
            .Setup(e => e.Email).WithMessage("邮箱格式不正确").MustMatchRegex(@"^\w+@\w+\.\w+$").MustNotBeNullOrEmpty();
            var report = new ValidationReport(engine);

            report.Validate(employee);
            var errors = report.GetErrorMessages(employee);

            return(Json(new { errors }, JsonRequestBehavior.AllowGet));
        }
        public void TestMessagesCount()
        {
            var builder = new Fluent.FluentBuilder();
            builder.For<MyMessageTestClass>()
                .WithMessage("Object is not valid")
                .Setup(m => m.A)
                    .WithMessage("Property A is not valid.")
                    .MustBeBetween(1, 10)
                .Setup(m => m.B)
                    .WithMessage("Property B is not valid.")
                    .MustBeBetween(1, 10);

            var engine = builder.Build();
            var report = new ValidationReport(engine);
            var obj = new MyMessageTestClass(0, 0);
            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            Assert.AreEqual(2, report.GetErrorMessages(obj).Length);

            obj = new MyMessageTestClass(0, 0);
            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            Assert.AreEqual(2, report.GetErrorMessages(obj).Length);

            //There should be a total of 4 error messages...
            Assert.AreEqual(4, report.GetErrorMessages().Length);
        }
Exemple #6
0
 public string[] GetErrorMessages(object value)
 {
     return(_innerReport.GetErrorMessages(value));
 }