Exemplo n.º 1
0
 public void Invoke(object value, IValidationReport report, ValidationReportDepth depth)
 {
     if (_condition.Invoke((T)value))
     {
         _innerTrue.Validate(value, report, depth);
     }
     else
     {
         _innerFalse.Validate(value, report, depth);
     }
 }
        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();
        }
Exemplo n.º 4
0
        public void Invoke(object value, IValidationReport report, ValidationReportDepth depth)
        {
            if (depth == ValidationReportDepth.FieldShortCircuit && report.HasError(value, _expression))
            {
                return;
            }

            R objToValidate = _compiledExpression.Invoke((T)value);

            if (objToValidate != null)
            {
                _rulesEngine.Validate(objToValidate, report, depth);
            }
        }
Exemplo n.º 5
0
        public void Invoke(object value, IValidationReport report, ValidationReportDepth depth)
        {
            if (depth == ValidationReportDepth.FieldShortCircuit && report.HasError(value, _enumerableCompositionExpression))
            {
                return;
            }

            IEnumerable enumerableToValidate = _compiledExpression.Invoke((T)value);

            if (enumerableToValidate != null)
            {
                foreach (object objToValidate in enumerableToValidate)
                {
                    _rulesEngine.Validate(objToValidate, report, depth);
                    if (report.HasErrors && (depth == ValidationReportDepth.ShortCircuit))
                    {
                        return;
                    }
                }
            }
        }
Exemplo n.º 6
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();
        }
        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();
        }