/// <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."); } }
/// <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."); } }
/// <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())); } }
/// <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())); } }
/// <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); }
/// <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)); }
/// <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)); }
/// <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)); }