📄 commonrules.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Collections;
namespace Common.Utility.Validation
{
/// <summary>
/// Static class that contains common validation rules. Each rule conforms to the <see cref="ValidationRuleArgs"/> delegate.
/// </summary>
public static class CommonRules
{
#region NotNull
/// <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()));
}
}
#endregion
#region StringRequired
/// <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()));
}
}
#endregion
#region StringMaxLength
/// <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>
/// Class used with the <see cref="StringMaxLength"/>.
/// </summary>
public class MaxLengthRuleArgs : ValidationRuleArgs
{
private int _maxLength;
/// <summary>
/// Maximum length of the string property.
/// </summary>
public int MaxLength
{
get { return _maxLength; }
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="propertyName">Property to validate</param>
/// <param name="friendlyName">Friendly name to use in the validation error text.</param>
/// <param name="maxLength">Max length of the property</param>
public MaxLengthRuleArgs(string propertyName, string friendlyName, int maxLength)
: base(propertyName, friendlyName)
{
_maxLength = maxLength;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="propertyName">Property to validate</param>
/// <param name="maxLength">Max length of the property</param>
public MaxLengthRuleArgs(string propertyName, int maxLength)
: base(propertyName)
{
_maxLength = maxLength;
}
/// <summary>
/// Return a string representation of the object.
/// </summary>
public override string ToString()
{
return base.ToString() + "!" + _maxLength.ToString();
}
}
#endregion
#region MaxWords
/// <summary>
/// Summary description for MaxWordsRuleArgs.
/// </summary>
public class MaxWordsRuleArgs : ValidationRuleArgs
{
/// <summary>
/// Creates a new instance of the MaxWordsRuleArgs class.
/// </summary>
/// <param name="propertyName">The name of the property to be validated.</param>
/// <param name="maxLength">Maximum number of words allowed.</param>
public MaxWordsRuleArgs(string propertyName, int maxLength)
: base(propertyName)
{
this._maxLength = maxLength;
}
/// <summary>
/// Creates a new instance of the MaxWordsRuleArgs class.
/// </summary>
/// <param name="propertyName">The name of the property to be validated.</param>
/// <param name="friendlyName">Friendly name to use in the validation error text.</param>
/// <param name="maxLength">Maximum number of words allowed.</param>
public MaxWordsRuleArgs(string propertyName, string friendlyName, int maxLength)
: base(propertyName, friendlyName)
{
this._maxLength = maxLength;
}
/// <summary>
/// Return a string representation of the object.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return (base.ToString() + "!" + this._maxLength.ToString());
}
/// <summary>
/// Gets the value of the MaxLength property.
/// </summary>
public int MaxLength
{
get
{
return this._maxLength;
}
}
// Fields
private int _maxLength;
}
/// <summary>
/// Summary description for MaxWords.
/// </summary>
/// <param name="target"></param>
/// <param name="e"></param>
/// <returns></returns>
public static bool MaxWords(object target, ValidationRuleArgs e)
{
CommonRules.MaxWordsRuleArgs args1 = e as CommonRules.MaxWordsRuleArgs;
if (args1 == null)
{
throw new ArgumentException("Invalid ValidationRuleArgs. e must be of type MaxWordsRuleArgs.");
}
string text1 = @"\b\w+\b";
PropertyInfo info1 = target.GetType().GetProperty(e.PropertyName);
if (info1 == null)
{
throw new ArgumentException(string.Format("Property \"{0}\" not found on object \"{1}\"", e.PropertyName, target.GetType().ToString()));
}
if (info1.PropertyType != typeof(string))
{
throw new ArgumentException(string.Format("Property \"{0}\" is not of type String.", e.PropertyName));
}
string text2 = (string)info1.GetValue(target, null);
if (Regex.Matches(text2, text1).Count > args1.MaxLength)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -