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;
        }
Ejemplo n.º 2
0
 public void Define(Engine rules)
 {
     rules.For<Card>()
         .Setup(p => p.CardNumber)
             .MustNotBeNullOrEmpty().WithMessage("Card number should be not empty")
         .Setup(c => c.Person).MustNotBeNull().CallValidate();
 }
Ejemplo n.º 3
0
 public void Define(Engine rules)
 {
     rules.For<Person>()
         .Setup(p=>p.Name)
             .MustNotBeNullOrEmpty().WithMessage(emptyName)
         .Setup(p=>p.Number)
             .MustNotBeNullOrEmpty().WithMessage(emptyNumber)
             .MustMatchRegex(@"^[0-9]*$").WithMessage(numberOnlyDigits);
 }
        public static void RunObjectGraphValidation(Club club)
        {
            Engine engine = new Engine();

            engine.For<Member>()
                .Setup(member => member.Name)
                    .MustNotBeNullOrEmpty()
                .Setup(member => member.Email)
                    .MustNotBeNullOrEmpty();

            engine.For<Club>()
                .Setup(c => club.President)
                    .MustNotBeNull()
                    .CallValidate() //Calls engine.Validate for the President.
                .Setup(c => club.Members)
                    .MustNotBeNull()
                    .CallValidateForEachElement(); //Calls engine.Validate() on each element

            club.IsValid = engine.Validate(club).ToString();
        }
        public static void RunCustomRules(Member member)
        {
            Engine engine = new Engine();

            engine.For<Member>()
               .Setup(m => member.Name)
                   .MustNotBeNullOrEmpty()
               .Setup(m => member.Email)
                   .MustNotBeNullOrEmpty()
                   .MustPassRule(new EmailRule());

            member.IsValid = engine.Validate(member).ToString();
        }
        public static void RunCrossField(NewUserRegistration user)
        {
            Engine engine = new Engine();
            engine.For<NewUserRegistration>()
                .Setup(u => user.UserName)
                    .MustNotBeNullOrEmpty()
                .Setup(u => user.Password)
                    .MustNotBeNullOrEmpty()
                .Setup(u => user.ConfirmPassword)
                    .MustEqual(u => user.Password);

            user.IsValid = engine.Validate(user).ToString();
        }
Ejemplo n.º 7
0
        private void TestRuleEngine()
        {
            RulesEngine.Engine engine = new RulesEngine.Engine();
            engine.For<Person>()                    
                    .Setup(p => p.Name)
                        .MustNotBeNull()
                        .MustMatchRegex("^[a-zA-z]+$")
                    .Setup(p => p.Phone)
                        .MustNotBeNull()
                        .MustMatchRegex("^[0-9]+$");

            engine.For<MatchDTO>()
                .Setup(m => m.HomeTeamName)
                    .MustEqual("Man U");

            MatchDTO match = new MatchDTO();
            match.HomeTeamName = "Man U";
            match.AwayTeamName = "Chelsea";
            match.AwayScore = "2";
            match.HomeScore = "1";

            bool isV = engine.Validate(match);
            iBet.Utilities.WriteLog.Write(isV.ToString());

            Person person = new Person();
            person.Name = "Bill";
            person.Phone = "1234214";
            //person.DateOfBirth = new DateTime(1999, 10, 2);

            bool isValid = engine.Validate(person);
            iBet.Utilities.WriteLog.Write(isValid.ToString());
        }
        public static void RunSimple(Person person)
        {
            Engine engine = new Engine();
            engine.For<Person>()
                    .Setup(p => p.DateOfBirth)
                        .MustBeLessThan(DateTime.Now)
                    .Setup(p => p.Name)
                        .MustNotBeNull()
                        .MustMatchRegex("^[a-zA-z]+$")
                    .Setup(p => p.Phone)
                        .MustNotBeNull()
                        .MustMatchRegex("^[0-9]+$");

            person.IsValid = engine.Validate(person).ToString();
        }