SPRNET-1122
-change behavior of RegularExpressionValidator.Validate(...) method to depend almost entirely on the regex match in re: its boolean return
This commit is contained in:
@@ -30,33 +30,33 @@ using Spring.Util;
|
|||||||
|
|
||||||
namespace Spring.Validation
|
namespace Spring.Validation
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validates that object matches specified regular expression.
|
/// Validates that object matches specified regular expression.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// <p>
|
/// <p>
|
||||||
/// The test expression must evaluate to a <see cref="System.String"/>;
|
/// The test expression must evaluate to a <see cref="System.String"/>;
|
||||||
/// otherwise, an exception is thrown.
|
/// otherwise, an exception is thrown.
|
||||||
/// </p>
|
/// </p>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <author>Aleksandar Seovic</author>
|
/// <author>Aleksandar Seovic</author>
|
||||||
public class RegularExpressionValidator : BaseSimpleValidator
|
public class RegularExpressionValidator : BaseSimpleValidator
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
|
|
||||||
private string expression = string.Empty;
|
private string expression = string.Empty;
|
||||||
private RegexOptions options;
|
private RegexOptions options;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the <see cref="RegularExpressionValidator"/> class.
|
/// Creates a new instance of the <see cref="RegularExpressionValidator"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public RegularExpressionValidator()
|
public RegularExpressionValidator()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the <see cref="RegularExpressionValidator"/> class.
|
/// Creates a new instance of the <see cref="RegularExpressionValidator"/> class.
|
||||||
@@ -64,76 +64,86 @@ namespace Spring.Validation
|
|||||||
/// <param name="test">The expression to validate.</param>
|
/// <param name="test">The expression to validate.</param>
|
||||||
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
|
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
|
||||||
/// <param name="expression">The regular expression to match against.</param>
|
/// <param name="expression">The regular expression to match against.</param>
|
||||||
public RegularExpressionValidator(string test, string when, string expression) : base(test, when)
|
public RegularExpressionValidator(string test, string when, string expression)
|
||||||
{
|
: base(test, when)
|
||||||
|
{
|
||||||
AssertUtils.ArgumentHasText(test, "test");
|
AssertUtils.ArgumentHasText(test, "test");
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the <see cref="RegularExpressionValidator"/> class.
|
/// Creates a new instance of the <see cref="RegularExpressionValidator"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="test">The expression to validate.</param>
|
/// <param name="test">The expression to validate.</param>
|
||||||
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
|
/// <param name="when">The expression that determines if this validator should be evaluated.</param>
|
||||||
/// <param name="expression">The regular expression to match against.</param>
|
/// <param name="expression">The regular expression to match against.</param>
|
||||||
public RegularExpressionValidator(IExpression test, IExpression when, string expression) : base(test, when)
|
public RegularExpressionValidator(IExpression test, IExpression when, string expression)
|
||||||
{
|
: base(test, when)
|
||||||
|
{
|
||||||
AssertUtils.ArgumentNotNull(test, "test");
|
AssertUtils.ArgumentNotNull(test, "test");
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The regular expression <b>text</b> to match against.
|
/// The regular expression <b>text</b> to match against.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The regular expression <b>text</b>.</value>
|
/// <value>The regular expression <b>text</b>.</value>
|
||||||
public string Expression
|
public string Expression
|
||||||
{
|
{
|
||||||
get { return expression; }
|
get { return expression; }
|
||||||
set { expression = value; }
|
set { expression = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The <see cref="RegexOptions"/> for the regular expression evaluation.
|
/// The <see cref="RegexOptions"/> for the regular expression evaluation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The regular expression evaluation options.</value>
|
/// <value>The regular expression evaluation options.</value>
|
||||||
/// <seealso cref="RegexOptions"/>
|
/// <seealso cref="RegexOptions"/>
|
||||||
public RegexOptions Options
|
public RegexOptions Options
|
||||||
{
|
{
|
||||||
get { return options; }
|
get { return options; }
|
||||||
set { options = value; }
|
set { options = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validates an object.
|
/// Validates an object.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="objectToValidate">Object to validate.</param>
|
/// <param name="objectToValidate">Object to validate.</param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <see lang="true"/> if the supplied <paramref name="objectToValidate"/>
|
/// <see lang="true"/> if the supplied <paramref name="objectToValidate"/>
|
||||||
/// object is valid.
|
/// object is valid.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <exception cref="System.ArgumentException">
|
/// <exception cref="System.ArgumentException">
|
||||||
/// If the supplied <paramref name="objectToValidate"/> is not a
|
/// If the supplied <paramref name="objectToValidate"/> is not a
|
||||||
/// <see cref="System.String"/>
|
/// <see cref="System.String"/>
|
||||||
/// </exception>
|
/// </exception>
|
||||||
protected override bool Validate(object objectToValidate)
|
protected override bool Validate(object objectToValidate)
|
||||||
{
|
{
|
||||||
string text = objectToValidate as string;
|
string text = objectToValidate as string;
|
||||||
if (text == null)
|
|
||||||
{
|
if (text == null)
|
||||||
throw new ArgumentException("Test for RegularExpressionValidator must evaluate to a string.");
|
{
|
||||||
}
|
throw new ArgumentException("Test for RegularExpressionValidator must evaluate to a string.");
|
||||||
if (!StringUtils.HasLength(text))
|
}
|
||||||
{
|
|
||||||
return true;
|
if (!StringUtils.HasLength(text))
|
||||||
}
|
{
|
||||||
Match match = Regex.Match(text, this.Expression, this.Options);
|
return true;
|
||||||
return match.Success && match.Index == 0 && match.Length == text.Length;
|
}
|
||||||
}
|
|
||||||
}
|
if (!StringUtils.HasLength(text.Trim()) && !StringUtils.HasLength(expression))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Match match = Regex.Match(text, this.Expression, this.Options);
|
||||||
|
return match.Success;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -79,7 +79,7 @@ namespace Spring.Validation.Validators
|
|||||||
[Test]
|
[Test]
|
||||||
public void CaseSensitiveStringMatching()
|
public void CaseSensitiveStringMatching()
|
||||||
{
|
{
|
||||||
RegularExpressionValidator validator = new RegularExpressionValidator("ToString()", "true", @"[A-Z][a-z]*");
|
RegularExpressionValidator validator = new RegularExpressionValidator("ToString()", "true", @"^[A-Z]([a-z]{1})");
|
||||||
Assert.IsTrue(validator.Validate("Aleksandar", new ValidationErrors()));
|
Assert.IsTrue(validator.Validate("Aleksandar", new ValidationErrors()));
|
||||||
Assert.IsFalse(validator.Validate("ALEKSANDAR", new ValidationErrors()));
|
Assert.IsFalse(validator.Validate("ALEKSANDAR", new ValidationErrors()));
|
||||||
Assert.IsFalse(validator.Validate("aleksandar", new ValidationErrors()));
|
Assert.IsFalse(validator.Validate("aleksandar", new ValidationErrors()));
|
||||||
@@ -118,5 +118,14 @@ namespace Spring.Validation.Validators
|
|||||||
Assert.IsTrue(valid, "Validation should succeed when regex validator is not evaluated.");
|
Assert.IsTrue(valid, "Validation should succeed when regex validator is not evaluated.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Test()
|
||||||
|
{
|
||||||
|
RegularExpressionValidator validator = new RegularExpressionValidator();
|
||||||
|
validator.Expression = "^[A-Za-z]";
|
||||||
|
Assert.True(validator.Validate("hello", new ValidationErrors()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user