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