Example #1
0
 /// <summary>
 /// Creates and initializes the rule.
 /// </summary>
 /// <param name="target">Object reference containing the data to validate.</param>
 /// <param name="handler">The address of the method implementing <see cref="ValidationRuleHandler"/>.</param>
 /// <param name="args">A <see cref="ValidationRuleArgs"/> object.</param>
 public ValidationRuleInfo(object target, ValidationRuleHandler handler, ValidationRuleArgs args)
 {
     _target = target;
      _handler = handler;
      _args = args;
      _ruleName = _handler.Method.Name + "!" + _args.ToString();
 }
Example #2
0
        /// <summary>
        /// Rule ensuring a String value is matching
        /// a specified regular expression.
        /// </summary>
        /// <param name="target">Object containing the data to validate.</param>
        /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated, must be of type RegexRuleArgs</param>
        /// <returns>False if the rule is broken; true otherwise.</returns>
        /// <remarks>
        /// This implementation uses late binding, and will only work
        /// against String property values.
        /// </remarks>
        public static bool RegexIsMatch(object target, ValidationRuleArgs e)
        {
            RegexRuleArgs args = e as RegexRuleArgs;

            if (args != null)
            {
                string expression = args.Expression;

                PropertyInfo p = target.GetType().GetProperty(e.PropertyName);

                if (p != null)
                {
                    if (p.PropertyType == typeof(string))
                    {
                        string value = (string)p.GetValue(target, null);

                        if (value == null || !Regex.IsMatch(value, expression))
                        {
                            if (string.IsNullOrEmpty(e.Description))
                            {
                                e.Description = String.Format("{0} is not valid.", e.FriendlyName);
                            }
                            return(false);
                        }
                        return(true);
                    }
                    else
                    {
                        throw new ArgumentException(string.Format("Property \"{0}\" is not of type String.", e.PropertyName));
                    }
                }
                else
                {
                    throw new ArgumentException(string.Format("Property \"{0}\" not found on object \"{1}\"", e.PropertyName, target.GetType().ToString()));
                }
            }
            else
            {
                throw new ArgumentException("Invalid ValidationRuleArgs. e must be of type RegexRuleArgs.");
            }
        }
Example #3
0
        /// <summary>
        /// Rule ensuring a String value doesn't exceed
        /// a specified length.
        /// </summary>
        /// <param name="target">Object containing the data to validate.</param>
        /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
        /// <returns>False if the rule is broken; true otherwise.</returns>
        /// <remarks>
        /// This implementation uses late binding, and will only work
        /// against String property values.
        /// </remarks>
        public static bool StringMaxLength(object target, ValidationRuleArgs e)
        {
            MaxLengthRuleArgs args = e as MaxLengthRuleArgs;

            if (args != null)
            {
                int max = args.MaxLength;

                PropertyInfo p = target.GetType().GetProperty(e.PropertyName);

                if (p != null)
                {
                    if (p.PropertyType == typeof(string))
                    {
                        string value = (string)p.GetValue(target, null);

                        if (!String.IsNullOrEmpty(value) && (value.Length > max))
                        {
                            if (string.IsNullOrEmpty(e.Description))
                            {
                                e.Description = String.Format("{0} can not exceed {1} characters", e.FriendlyName, max.ToString());
                            }
                            return(false);
                        }
                        return(true);
                    }
                    else
                    {
                        throw new ArgumentException(string.Format("Property \"{0}\" is not of type String.", e.PropertyName));
                    }
                }
                else
                {
                    throw new ArgumentException(string.Format("Property \"{0}\" not found on object \"{1}\"", e.PropertyName, target.GetType().ToString()));
                }
            }
            else
            {
                throw new ArgumentException("Invalid ValidationRuleArgs. e must be of type MaxLengthRuleArgs.");
            }
        }
Example #4
0
        /// <summary>
        /// Rule ensuring a String value contains one or more
        /// characters.
        /// </summary>
        /// <param name="target">Object containing the data to validate.</param>
        /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
        /// <returns>False if the rule is broken; true otherwise.</returns>
        /// <remarks>
        /// This implementation uses late binding, and will only work
        /// against String property values.
        /// </remarks>
        public static bool StringRequired(object target, ValidationRuleArgs e)
        {
            PropertyInfo p = target.GetType().GetProperty(e.PropertyName);

            if (p != null)
            {
                string value = (string)p.GetValue(target, null);
                if (string.IsNullOrEmpty(value))
                {
                    if (string.IsNullOrEmpty(e.Description))
                    {
                        e.Description = e.FriendlyName + " is required.";
                    }
                    return(false);
                }
                return(true);
            }
            else
            {
                throw new ArgumentException(string.Format("Property \"{0}\" not found on object \"{1}\"", e.PropertyName, target.GetType().ToString()));
            }
        }
Example #5
0
        /// <summary>
        /// Rule that does not allow a property value to be null
        /// </summary>
        /// <param name="target">Object containing the data to validate.</param>
        /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
        /// <returns>False if the rule is broken; true otherwise.</returns>
        /// <returns>Returns true if the property value is not null; false otherwise.</returns>
        public static bool NotNull(object target, ValidationRuleArgs e)
        {
            PropertyInfo p = target.GetType().GetProperty(e.PropertyName);

            if (p != null)
            {
                object value = p.GetValue(target, null);

                if (value == null)
                {
                    if (string.IsNullOrEmpty(e.Description))
                    {
                        e.Description = string.Format("{0} cannot be null.", e.FriendlyName);
                    }
                    return(false);
                }

                return(true);
            }
            else
            {
                throw new ArgumentException(string.Format("Property \"{0}\" not found on object \"{1}\"", e.PropertyName, target.GetType().ToString()));
            }
        }
Example #6
0
        /// <summary>
        /// Adds a rule to the list of validated rules.
        /// </summary>
        /// <remarks>
        /// <para>
        /// A rule is implemented by a method which conforms to the 
        /// method signature defined by the <see cref="ValidationRuleHandler" /> delegate.
        /// </para>
        /// </remarks>
        /// <param name="handler">The method that implements the rule.</param>
        /// <param name="args">
        /// A <see cref="ValidationRuleArgs"/> object specifying the property name and other arguments
        /// passed to the rule method
        /// </param>
        public void AddRule(ValidationRuleHandler handler, ValidationRuleArgs args)
        {
            // get the list of rules for the property
             List<ValidationRuleInfo> list = GetPropertyRules(args.PropertyName);

             // we have the list, add our new rule
             list.Add(new ValidationRuleInfo(_target, handler, args));
        }
Example #7
0
 /// <summary>
 /// Generic rule that determines if an object's property is greater than or equal to a particular value.
 /// </summary>
 /// <typeparam name="T">Datatype of the property to validate</typeparam>
 /// <param name="target">Object containing the data to validate.</param>
 /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
 /// <returns>False if the rule is broken; true otherwise.</returns>
 public static bool GreaterThanOrEqualToValue <T>(object target, ValidationRuleArgs e)
 {
     return(CompareValues <T>(target, e as CompareValueRuleArgs <T>, CompareType.GreaterThanOrEqualTo));
 }
Example #8
0
 /// <summary>
 /// Generic rule that determines if an object's property is equal to a particular value.
 /// </summary>
 /// <typeparam name="T">Datatype of the property to validate</typeparam>
 /// <param name="target">Object containing the data to validate.</param>
 /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
 /// <returns>False if the rule is broken; true otherwise.</returns>
 public static bool EqualsValue <T>(object target, ValidationRuleArgs e)
 {
     return(CompareValues <T>(target, e as CompareValueRuleArgs <T>, CompareType.EqualTo));
 }
Example #9
0
 /// <summary>
 /// Generic rule that determines if an object's property is less than a particular value.
 /// </summary>
 /// <typeparam name="T">Datatype of the property to validate</typeparam>
 /// <param name="target">Object containing the data to validate.</param>
 /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
 /// <returns>False if the rule is broken; true otherwise.</returns>
 public static bool LessThanValue <T>(object target, ValidationRuleArgs e)
 {
     return(CompareValues <T>(target, e as CompareValueRuleArgs <T>, CompareType.LessThan));
 }
Example #10
0
 /// <summary>
 /// Adds a rule to the list of validated rules.
 /// </summary>
 /// <remarks>
 /// </remarks>
 /// <param name="handler">The method that implements the rule.</param>
 /// <param name="args">
 /// A <see cref="Validation.ValidationRuleArgs"/> object specifying the property name and other arguments
 /// passed to the rule method
 /// </param>
 public void AddValidationRuleHandler(Validation.ValidationRuleHandler handler, Validation.ValidationRuleArgs args)
 {
     ValidationRules.AddRule(handler, args);
 }