Пример #1
0
        public IPropertyRule <TEntity> ExecuteRule <TEntity>(TEntity entity, IEnumerable <IPropertyRule <TEntity> > rules) where TEntity : RuleAwareEntity

        {
            IPropertyRule <TEntity> appliedRule = rules.FirstOrDefault(r => r.Execute(entity));

            return(appliedRule);
        }
        /// <summary>
        /// Determines whether or not a rule should execute.
        /// </summary>
        /// <param name="rule">The rule</param>
        /// <param name="propertyPath">Property path (eg Customer.Address.Line1)</param>
        /// <param name="context">Contextual information</param>
        /// <returns>Whether or not the validator can execute.</returns>
        public bool CanExecute(IPropertyRule rule, string propertyPath, ConditionContext context)
        {
            // By default we ignore any rules part of a RuleSet.
            // TODO
            //if (!string.IsNullOrEmpty(rule.RuleSet)) return false;

            return true;
        }
 public static bool DateNotMinHandler(IPropertyRule <DateTime, object> target, object objectChecked)
 {
     if (target.PropertyValue(objectChecked) == DateTime.MinValue)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
 public static bool NotEqualToHandler(IPropertyRule <IComparable, IComparable> target, object objectChecked)
 {
     if (target.PropertyValue(objectChecked).CompareTo((IComparable)target.AllowedValue) != 0)
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
 public static bool NotNullHandler(IPropertyRule <object, object> target, object objectChecked)
 {
     if (target.PropertyValue(objectChecked) == null)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
 public static bool NonEmptyGuidHandler(IPropertyRule <Guid, Guid> target, object objectChecked)
 {
     if (target.PropertyValue(objectChecked).IsEmpty())
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
        public static bool BetweenValueHandlerAllowMinValue(IPropertyRule <IComparable, ValueRangeValidationHelper> target, object objectChecked)
        {
            if (target.AllowedValue == null)
            {
                return(false);
            }

            ValueRangeValidationHelper valRange = target.AllowedValue;
            IComparable val = target.PropertyValue(objectChecked);

            return(!val.Between(valRange.FromValue, valRange.ToValue));
        }
        public static bool NonBlankStringHandler(IPropertyRule <String, string> target, object objectChecked)
        {
            String propVal = target.PropertyValue(objectChecked);

            if (propVal != null)
            {
                return(propVal.ToString().IsEmpty());
            }
            else
            {
                return(true);
            }
        }
Пример #9
0
        public IPropertyRule <TEntity> ExecuteRule <TEntity>(TEntity entity, IEnumerable <IPropertyRule <TEntity> > ruleList) where TEntity : RuleAwareEntity

        {
            IPropertyRule <TEntity> appliedRule = null;

            foreach (var rule in ruleList)
            {
                if (!rule.Execute(entity))
                {
                    continue;
                }
                appliedRule = rule;
            }
            return(appliedRule);
        }
        public static bool MinStringLengthHandler(IPropertyRule <String, int> target, object objectChecked)
        {
            if (target.PropertyValue(objectChecked) == null)
            {
                return(true);
            }

            if (target.PropertyValue(objectChecked).Trim().Length < target.AllowedValue)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #11
0
 public override bool Equals(object obj)
 {
     if (obj == null)
     {
         return(false);
     }
     if (obj is IPropertyRule <t, u> )
     {
         IPropertyRule <t, u> ValidationToCompare = (IPropertyRule <t, u>)obj;
         if (ValidationToCompare.RuleName.CompareAbsolute(RuleName) && ValidationToCompare.PropertyName.CompareAbsolute(PropertyName))
         {
             return(true);
         }
     }
     return(false);
 }
        public static bool LessThanHandler(IPropertyRule <IComparable, IComparable> target, object objectChecked)
        {
            IComparable val = target.PropertyValue(objectChecked);

            if (val == null)
            {
                return(true);
            }
            if (val.CompareTo(target.AllowedValue) >= 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
 public static bool DateRangeHandler(IPropertyRule <DateTime, IDateRange> target, object objectChecked)
 {
     if (target.AllowedValue != null)
     {
         if (!target.AllowedValue.InRange(target.PropertyValue(objectChecked)))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
Пример #14
0
        /// <summary>
        /// Performs default serialization for subclasses of <see cref="PropertyRule"/>.
        /// </summary>
        /// <param name="rule"></param>
        /// <param name="writer"></param>
        public static void SerializePropertyRule(IPropertyRule rule, JsonWriter writer)
        {
            // Assume the type does not need to be included if the name can be inferred from context
            if (String.Format("{0}.{1}.{2}", ((Rule)rule).RootType.Name, rule.Property, rule.Name) != ((Rule)rule).Name)
            {
                writer.Set("type", rule.Name.Substring(0, 1).ToLower() + rule.Name.Substring(1));
            }

            // Embed the condition type, if present, along with the rule
            if (rule.ConditionType != null && (writer.SerializePropertyConditionType || rule.ConditionType.AlwaysSerialize))
            {
                if (rule.ConditionType.Category != ConditionCategory.Error)
                {
                    writer.Set("category", rule.ConditionType.Category.ToString());
                }

                writer.Set("message", rule.ConditionType.Message);

                if (rule.ConditionType.Sets != null && rule.ConditionType.Sets.Any())
                {
                    writer.Set("sets", rule.ConditionType.Sets.Select(set => set.Name));
                }
            }
        }
Пример #15
0
 public static bool ContainProperty <TEntity>(this IPropertyRule propertyRule, Expression <Func <TEntity, object> > propertyExpression)
 {
     return(propertyRule.PropertyChain.Contains(PropertyUtil.ExtractPropertyName(propertyExpression)));
 }
Пример #16
0
 public static IPropertyRule <T, DateTime> Between <T>(this IPropertyRule <T, DateTime> rule, DateTime value1,
                                                       DateTime value2)
 {
     rule.Specifications.Add(new DateBetween(value1, value2));
     return(rule);
 }
Пример #17
0
 public static IPropertyRule <T, DateTime> IsNotInThePast <T>(this IPropertyRule <T, DateTime> rule)
 {
     rule.Specifications.Add(new DateNotInThePast());
     return(rule);
 }
Пример #18
0
 public static IPropertyRule <T, string> IsNotEmpty <T>(this IPropertyRule <T, string> rule)
 {
     rule.Specifications.Add(new StringNotNullOrWhiteSpace());
     return(rule);
 }
 public static bool IsNumberBroke(IPropertyRule <Object, object> target, object objectChecked)
 {
     return(Extensions.IsNumeric(target.PropertyValue(objectChecked).ToString()).IsFalse());
 }