10using System.Text.RegularExpressions;
11using System.Reflection;
23 public static partial class CommonRules
26 #region StringRequired
40 [System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Design",
"CA1062:ValidateArgumentsOfPublicMethods")]
41 public static bool StringRequired(
object target, RuleArgs e)
43 string value = (string)Utilities.CallByName(
44 target, e.PropertyName,
CallType.Get);
45 if (
string.IsNullOrEmpty(value))
55 #region StringMaxLength
69 [System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Design",
"CA1062:ValidateArgumentsOfPublicMethods")]
70 public static bool StringMaxLength(
71 object target, RuleArgs e)
73 DecoratedRuleArgs args = (DecoratedRuleArgs)e;
74 int max = (int)args[
"MaxLength"];
75 string value = (string)Utilities.CallByName(
76 target, e.PropertyName,
CallType.Get);
77 if (!
String.IsNullOrEmpty(value) && (value.Length > max))
79 string format = (string)args[
"Format"];
81 if (
string.IsNullOrEmpty(format))
82 outValue = max.ToString();
84 outValue = max.ToString(format);
85 e.Description =
String.Format(
87 RuleArgs.GetPropertyName(e), outValue);
104 get {
return (
int)
this[
"MaxLength"]; }
113 string propertyName,
int maxLength)
116 this[
"MaxLength"] = maxLength;
117 this[
"Format"] =
string.Empty;
128 this[
"MaxLength"] = maxLength;
129 this[
"Format"] =
string.Empty;
141 string propertyName,
string friendlyName,
int maxLength)
142 : base(propertyName, friendlyName)
144 this[
"MaxLength"] = maxLength;
145 this[
"Format"] =
string.Empty;
156 string propertyName,
int maxLength,
string format)
159 this[
"MaxLength"] = maxLength;
160 this[
"Format"] = format;
173 this[
"MaxLength"] = maxLength;
174 this[
"Format"] = format;
188 string propertyName,
string friendlyName,
int maxLength,
string format)
189 : base(propertyName, friendlyName)
191 this[
"MaxLength"] = maxLength;
192 this[
"Format"] = format;
198 #region StringMinLength
212 [System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Design",
"CA1062:ValidateArgumentsOfPublicMethods")]
213 public static bool StringMinLength(
217 int min = (int)args[
"MinLength"];
218 string value = (string)Utilities.CallByName(
220 if (String.IsNullOrEmpty(value) || (value.Length < min))
222 string format = (string)args[
"Format"];
224 if (
string.IsNullOrEmpty(format))
227 outValue = min.ToString(format);
247 get {
return (
int)
this[
"MinLength"]; }
256 string propertyName,
int minLength)
259 this[
"MinLength"] = minLength;
260 this[
"Format"] =
string.Empty;
269 Core.IPropertyInfo propertyInfo,
int minLength)
272 this[
"MinLength"] = minLength;
273 this[
"Format"] =
string.Empty;
285 string propertyName,
string friendlyName,
int minLength)
286 : base(propertyName, friendlyName)
288 this[
"MinLength"] = minLength;
289 this[
"Format"] =
string.Empty;
300 string propertyName,
int minLength,
string format)
303 this[
"MinLength"] = minLength;
304 this[
"Format"] = format;
315 Core.IPropertyInfo propertyInfo,
int minLength,
string format)
318 this[
"MinLength"] = minLength;
319 this[
"Format"] = format;
333 string propertyName,
string friendlyName,
int minLength,
string format)
334 : base(propertyName, friendlyName)
336 this[
"MinLength"] = minLength;
337 this[
"Format"] = format;
343 #region IntegerMaxValue
353 public static bool IntegerMaxValue(
object target,
RuleArgs e)
356 int max = (int)args[
"MaxValue"];
360 string format = (string)args[
"Format"];
362 if (
string.IsNullOrEmpty(format))
365 outValue = max.ToString(format);
384 get {
return (
int)
this[
"MaxValue"]; }
395 this[
"MaxValue"] = maxValue;
396 this[
"Format"] =
string.Empty;
407 this[
"MaxValue"] = maxValue;
408 this[
"Format"] =
string.Empty;
420 string propertyName,
string friendlyName,
int maxValue)
421 : base(propertyName, friendlyName)
423 this[
"MaxValue"] = maxValue;
424 this[
"Format"] =
string.Empty;
437 this[
"MaxValue"] = maxValue;
438 this[
"Format"] = format;
451 this[
"MaxValue"] = maxValue;
452 this[
"Format"] = format;
466 string propertyName,
string friendlyName,
int maxValue,
string format)
467 : base(propertyName, friendlyName)
469 this[
"MaxValue"] = maxValue;
470 this[
"Format"] = format;
476 #region IntegerMinValue
486 public static bool IntegerMinValue(
object target,
RuleArgs e)
489 int min = (int)args[
"MinValue"];
493 string format = (string)args[
"Format"];
495 if (
string.IsNullOrEmpty(format))
498 outValue = min.ToString(format);
517 get {
return (
int)
this[
"MinValue"]; }
528 this[
"MinValue"] = minValue;
529 this[
"Format"] =
string.Empty;
540 this[
"MinValue"] = minValue;
541 this[
"Format"] =
string.Empty;
553 string propertyName,
string friendlyName,
int minValue)
554 : base(propertyName, friendlyName)
556 this[
"MinValue"] = minValue;
557 this[
"Format"] =
string.Empty;
570 this[
"MinValue"] = minValue;
571 this[
"Format"] = format;
584 this[
"MinValue"] = minValue;
585 this[
"Format"] = format;
599 string propertyName,
string friendlyName,
int minValue,
string format)
600 : base(propertyName, friendlyName)
602 this[
"MinValue"] = minValue;
603 this[
"Format"] = format;
620 public static bool MaxValue<T>(
object target,
RuleArgs e) where T : IComparable
624 T value = (T)pi.GetValue(target,
null);
625 T max = (T)args[
"MaxValue"];
627 int result = value.CompareTo(max);
630 string format = (string)args[
"Format"];
632 if (
string.IsNullOrEmpty(format))
633 outValue = max.ToString();
635 outValue =
string.Format(
string.Format(
"{{0:{0}}}", format), max);
656 get {
return (T)
this[
"MaxValue"]; }
667 this[
"MaxValue"] = maxValue;
668 this[
"Format"] =
string.Empty;
669 this[
"ValueType"] = typeof(T).FullName;
680 this[
"MaxValue"] = maxValue;
681 this[
"Format"] =
string.Empty;
682 this[
"ValueType"] = typeof(T).FullName;
694 string propertyName,
string friendlyName, T maxValue)
695 : base(propertyName, friendlyName)
697 this[
"MaxValue"] = maxValue;
698 this[
"Format"] =
string.Empty;
699 this[
"ValueType"] = typeof(T).FullName;
712 this[
"MaxValue"] = maxValue;
713 this[
"Format"] = format;
714 this[
"ValueType"] = typeof(T).FullName;
727 this[
"MaxValue"] = maxValue;
728 this[
"Format"] = format;
729 this[
"ValueType"] = typeof(T).FullName;
743 string propertyName,
string friendlyName, T maxValue,
string format)
744 : base(propertyName, friendlyName)
746 this[
"MaxValue"] = maxValue;
747 this[
"Format"] = format;
748 this[
"ValueType"] = typeof(T).FullName;
765 public static bool MinValue<T>(
object target,
RuleArgs e) where T : IComparable
769 T value = (T)pi.GetValue(target,
null);
770 T min = (T)args[
"MinValue"];
772 int result = value.CompareTo(min);
775 string format = (string)args[
"Format"];
777 if (
string.IsNullOrEmpty(format))
778 outValue = min.ToString();
780 outValue =
string.Format(
string.Format(
"{{0:{0}}}", format), min);
801 get {
return (T)
this[
"MinValue"]; }
812 this[
"MinValue"] = minValue;
813 this[
"Format"] =
string.Empty;
814 this[
"ValueType"] = typeof(T).FullName;
825 this[
"MinValue"] = minValue;
826 this[
"Format"] =
string.Empty;
827 this[
"ValueType"] = typeof(T).FullName;
839 string propertyName,
string friendlyName, T minValue)
840 : base(propertyName, friendlyName)
842 this[
"MinValue"] = minValue;
843 this[
"Format"] =
string.Empty;
844 this[
"ValueType"] = typeof(T).FullName;
857 this[
"MinValue"] = minValue;
858 this[
"Format"] = format;
859 this[
"ValueType"] = typeof(T).FullName;
872 this[
"MinValue"] = minValue;
873 this[
"Format"] = format;
874 this[
"ValueType"] = typeof(T).FullName;
888 string propertyName,
string friendlyName, T minValue,
string format)
889 : base(propertyName, friendlyName)
891 this[
"MinValue"] = minValue;
892 this[
"Format"] = format;
893 this[
"ValueType"] = typeof(T).FullName;
912 public static bool RegExMatch(
object target,
RuleArgs e)
914 bool ruleSatisfied =
false;
916 RegExRuleArgs.NullResultOptions nullOption =
917 (RegExRuleArgs.NullResultOptions)args[
"NullOption"];
918 Regex expression = (Regex)args[
"RegEx"];
921 if (value ==
null && nullOption == RegExRuleArgs.NullResultOptions.ConvertToEmptyString)
922 value =
string.Empty;
928 ruleSatisfied = (nullOption == RegExRuleArgs.NullResultOptions.ReturnTrue);
934 ruleSatisfied = expression.IsMatch(value.ToString());
949 public enum RegExPatterns
967 #region NullResultOptions
1005 get {
return (Regex)
this[
"RegEx"]; }
1026 : base(propertyName)
1028 this[
"RegEx"] =
new Regex(
GetPattern(pattern));
1038 : base(propertyInfo)
1040 this[
"RegEx"] =
new Regex(
GetPattern(pattern));
1053 string propertyName,
string friendlyName, RegExPatterns pattern)
1054 : base(propertyName, friendlyName)
1056 this[
"RegEx"] =
new Regex(
GetPattern(pattern));
1066 : base(propertyName)
1068 this[
"RegEx"] =
new Regex(pattern);
1078 : base(propertyInfo)
1080 this[
"RegEx"] =
new Regex(pattern);
1093 string propertyName,
string friendlyName,
string pattern)
1094 : base(propertyName, friendlyName)
1096 this[
"RegEx"] =
new Regex(pattern);
1105 public RegExRuleArgs(
string propertyName, System.Text.RegularExpressions.Regex regEx)
1106 : base(propertyName)
1108 this[
"RegEx"] = regEx;
1117 public RegExRuleArgs(Core.IPropertyInfo propertyInfo, System.Text.RegularExpressions.Regex regEx)
1118 : base(propertyInfo)
1120 this[
"RegEx"] = regEx;
1133 string propertyName,
string friendlyName, System.Text.RegularExpressions.Regex regEx)
1134 : base(propertyName, friendlyName)
1136 this[
"RegEx"] = regEx;
1150 : base(propertyName)
1152 this[
"RegEx"] =
new Regex(
GetPattern(pattern));
1153 this[
"NullOption"] = nullResult;
1166 : base(propertyInfo)
1168 this[
"RegEx"] =
new Regex(
GetPattern(pattern));
1169 this[
"NullOption"] = nullResult;
1185 string propertyName,
string friendlyName, RegExPatterns pattern,
NullResultOptions nullResult)
1186 : base(propertyName, friendlyName)
1188 this[
"RegEx"] =
new Regex(
GetPattern(pattern));
1189 this[
"NullOption"] = nullResult;
1202 : base(propertyName)
1204 this[
"RegEx"] =
new Regex(pattern);
1205 this[
"NullOption"] = nullResult;
1218 : base(propertyInfo)
1220 this[
"RegEx"] =
new Regex(pattern);
1221 this[
"NullOption"] = nullResult;
1237 string propertyName,
string friendlyName,
string pattern,
NullResultOptions nullResult)
1238 : base(propertyName, friendlyName)
1240 this[
"RegEx"] =
new Regex(pattern);
1241 this[
"NullOption"] = nullResult;
1254 : base(propertyName)
1256 this[
"RegEx"] = regEx;
1257 this[
"NullOption"] = nullResult;
1270 : base(propertyInfo)
1272 this[
"RegEx"] = regEx;
1273 this[
"NullOption"] = nullResult;
1289 string propertyName,
string friendlyName, System.Text.RegularExpressions.Regex regEx,
NullResultOptions nullResult)
1290 : base(propertyName, friendlyName)
1292 this[
"RegEx"] = regEx;
1293 this[
"NullOption"] = nullResult;
1304 case RegExPatterns.SSN:
1305 return @"^\d{3}-\d{2}-\d{4}$";
1306 case RegExPatterns.Email:
1307 return @"^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$";
1309 return string.Empty;
1329 public static bool CanRead(
object target,
RuleArgs e)
1331 bool isAuthorized =
true;
1334 if (business !=
null && !
string.IsNullOrEmpty(e.
PropertyName))
1343 return isAuthorized;
1361 public static bool CanWrite(
object target, RuleArgs e)
1363 bool isAuthorized =
true;
1366 if (business !=
null && !
string.IsNullOrEmpty(e.PropertyName))
1372 e.Description =
string.Format(
"You are not authorized to write to this field {0}", RuleArgs.GetPropertyName(e));
1375 return isAuthorized;
1380 #region DataAnnotations
1407 public System.ComponentModel.DataAnnotations.ValidationAttribute
Attribute {
get;
set; }
1414 return string.Format(
"{0}?Attribute={1}", base.ToString(),
Attribute.GetType().FullName);
1430 public static bool DataAnnotation(
object target,
RuleArgs e)
1432 var args = (DataAnnotationRuleArgs)e;
1435 var ctx =
new System.ComponentModel.DataAnnotations.ValidationContext(target,
null,
null)
1440 var result = args.Attribute.GetValidationResult(pValue, ctx);
This is the non-generic base class from which most business objects will be derived.
virtual bool CanWriteProperty(Csla.Core.IPropertyInfo property)
Returns true if the user is allowed to write the specified property.
virtual bool CanReadProperty(Csla.Core.IPropertyInfo property)
Returns true if the user is allowed to read the calling property.
A strongly-typed resource class, for looking up localized strings, etc.
static string MaxValueRule
Looks up a localized string similar to {0} can not exceed {1}.
static string StringMaxLengthRule
Looks up a localized string similar to {0} can not exceed {1} characters.
static string StringRequiredRule
Looks up a localized string similar to {0} required.
static string MinValueRule
Looks up a localized string similar to {0} can not be less than {1}.
static string RegExMatchRule
Looks up a localized string similar to {0} does not match regular expression.
static string StringMinLengthRule
Looks up a localized string similar to {0} must be at least {1} characters.
Maintains metadata about a property.
Intermediate base class for BusinessBase
Arguments provided to the DataAnnotation rule method
System.ComponentModel.DataAnnotations.ValidationAttribute Attribute
The attribute containing the rule implementation.
override string ToString()
Gets a string representation of the object.
DataAnnotationRuleArgs(string name, System.ComponentModel.DataAnnotations.ValidationAttribute attribute)
Creates an instance of the object.
Custom RuleArgs object required by the IntegerMaxValue rule method.
IntegerMaxValueRuleArgs(string propertyName, int maxValue, string format)
Create a new object.
IntegerMaxValueRuleArgs(string propertyName, string friendlyName, int maxValue)
Create a new object.
IntegerMaxValueRuleArgs(Core.IPropertyInfo propertyInfo, int maxValue, string format)
Create a new object.
int MaxValue
Get the max value for the property.
IntegerMaxValueRuleArgs(string propertyName, string friendlyName, int maxValue, string format)
Create a new object.
IntegerMaxValueRuleArgs(string propertyName, int maxValue)
Create a new object.
IntegerMaxValueRuleArgs(Core.IPropertyInfo propertyInfo, int maxValue)
Create a new object.
Custom RuleArgs object required by the IntegerMinValue rule method.
IntegerMinValueRuleArgs(string propertyName, string friendlyName, int minValue, string format)
Create a new object.
IntegerMinValueRuleArgs(string propertyName, int minValue, string format)
Create a new object.
IntegerMinValueRuleArgs(Core.IPropertyInfo propertyInfo, int minValue)
Create a new object.
IntegerMinValueRuleArgs(string propertyName, string friendlyName, int minValue)
Create a new object.
IntegerMinValueRuleArgs(Core.IPropertyInfo propertyInfo, int minValue, string format)
Create a new object.
IntegerMinValueRuleArgs(string propertyName, int minValue)
Create a new object.
int MinValue
Get the min value for the property.
Custom RuleArgs object required by the StringMaxLength rule method.
MaxLengthRuleArgs(string propertyName, string friendlyName, int maxLength)
Create a new object.
MaxLengthRuleArgs(Core.IPropertyInfo propertyInfo, int maxLength, string format)
Create a new object.
int MaxLength
Get the max length for the string.
MaxLengthRuleArgs(string propertyName, string friendlyName, int maxLength, string format)
Create a new object.
MaxLengthRuleArgs(string propertyName, int maxLength, string format)
Create a new object.
MaxLengthRuleArgs(Core.IPropertyInfo propertyInfo, int maxLength)
Create a new object.
MaxLengthRuleArgs(string propertyName, int maxLength)
Create a new object.
Custom RuleArgs object required by the MaxValue rule method.
MaxValueRuleArgs(string propertyName, string friendlyName, T maxValue, string format)
Create a new object.
MaxValueRuleArgs(string propertyName, T maxValue, string format)
Create a new object.
MaxValueRuleArgs(string propertyName, T maxValue)
Create a new object.
MaxValueRuleArgs(Core.IPropertyInfo propertyInfo, T maxValue)
Create a new object.
MaxValueRuleArgs(Core.IPropertyInfo propertyInfo, T maxValue, string format)
Create a new object.
T MaxValue
Get the max value for the property.
MaxValueRuleArgs(string propertyName, string friendlyName, T maxValue)
Create a new object.
Custom RuleArgs object required by the StringMinLength rule method.
MinLengthRuleArgs(Core.IPropertyInfo propertyInfo, int minLength)
Create a new object.
MinLengthRuleArgs(string propertyName, string friendlyName, int minLength)
Create a new object.
MinLengthRuleArgs(string propertyName, int minLength)
Create a new object.
int MinLength
Get the min length for the string.
MinLengthRuleArgs(Core.IPropertyInfo propertyInfo, int minLength, string format)
Create a new object.
MinLengthRuleArgs(string propertyName, int minLength, string format)
Create a new object.
MinLengthRuleArgs(string propertyName, string friendlyName, int minLength, string format)
Create a new object.
Custom RuleArgs object required by the MinValue rule method.
MinValueRuleArgs(Core.IPropertyInfo propertyInfo, T minValue, string format)
Create a new object.
T MinValue
Get the min value for the property.
MinValueRuleArgs(Core.IPropertyInfo propertyInfo, T minValue)
Create a new object.
MinValueRuleArgs(string propertyName, string friendlyName, T minValue)
Create a new object.
MinValueRuleArgs(string propertyName, T minValue, string format)
Create a new object.
MinValueRuleArgs(string propertyName, string friendlyName, T minValue, string format)
Create a new object.
MinValueRuleArgs(string propertyName, T minValue)
Create a new object.
Custom RuleArgs object required by the RegExMatch rule method.
RegExRuleArgs(string propertyName, string pattern)
Creates a new object.
Regex RegEx
The RegEx object used to validate the property.
RegExRuleArgs(string propertyName, string friendlyName, System.Text.RegularExpressions.Regex regEx, NullResultOptions nullResult)
Creates a new object.
RegExRuleArgs(string propertyName, string friendlyName, RegExPatterns pattern)
Creates a new object.
RegExRuleArgs(string propertyName, string pattern, NullResultOptions nullResult)
Creates a new object.
NullResultOptions
List of options for the NullResult property.
RegExRuleArgs(Core.IPropertyInfo propertyInfo, string pattern)
Creates a new object.
RegExRuleArgs(string propertyName, string friendlyName, string pattern, NullResultOptions nullResult)
Creates a new object.
RegExRuleArgs(string propertyName, string friendlyName, System.Text.RegularExpressions.Regex regEx)
Creates a new object.
RegExRuleArgs(string propertyName, System.Text.RegularExpressions.Regex regEx)
Creates a new object.
NullResultOptions NullResult
Gets a value indicating whether a null value means the rule will return true or false.
RegExRuleArgs(string propertyName, string friendlyName, string pattern)
Creates a new object.
RegExRuleArgs(string propertyName, RegExPatterns pattern)
Creates a new object.
RegExRuleArgs(Core.IPropertyInfo propertyInfo, System.Text.RegularExpressions.Regex regEx)
Creates a new object.
RegExRuleArgs(string propertyName, System.Text.RegularExpressions.Regex regEx, NullResultOptions nullResult)
Creates a new object.
static string GetPattern(RegExPatterns pattern)
Returns the specified built-in regex pattern.
RegExRuleArgs(Core.IPropertyInfo propertyInfo, RegExPatterns pattern, NullResultOptions nullResult)
Creates a new object.
RegExRuleArgs(string propertyName, RegExPatterns pattern, NullResultOptions nullResult)
Creates a new object.
RegExRuleArgs(Core.IPropertyInfo propertyInfo, string pattern, NullResultOptions nullResult)
Creates a new object.
RegExRuleArgs(string propertyName, string friendlyName, RegExPatterns pattern, NullResultOptions nullResult)
Creates a new object.
RegExRuleArgs(Core.IPropertyInfo propertyInfo, RegExPatterns pattern)
Creates a new object.
RegExRuleArgs(Core.IPropertyInfo propertyInfo, System.Text.RegularExpressions.Regex regEx, NullResultOptions nullResult)
Creates a new object.
Object providing extra information to methods that implement business rules.
override string ToString()
Return a string representation of the object using the rule:// URI format.
Object providing extra information to methods that implement business rules.
string Description
Set by the rule handler method to describe the broken rule.
RuleSeverity Severity
Gets or sets the severity of the broken rule.
string PropertyName
The name of the property to be validated.
static string GetPropertyName(RuleArgs e)
Gets the property name from the RuleArgs object, using the friendly name if one is defined.
RuleSeverity
Values for validation rule severities.
CallType
Valid options for calling a property or method via the Csla.Utilities.CallByName method.