Handle null/empty property name in ValidatingModelBase.ValidatePropertyAsync

In this case, we call ValidateAllProperties instead
This commit is contained in:
Antony Male 2015-01-15 10:12:37 +00:00
parent 07160aa4fe
commit 3418fda5a2
2 changed files with 23 additions and 5 deletions

View File

@ -182,16 +182,16 @@ namespace Stylet
/// <summary>
/// Validate a single property asynchronously, by name.
/// </summary>
/// <param name="propertyName">Property to validate</param>
/// <param name="propertyName">Property to validate. Validates all properties if null or String.Empty</param>
/// <returns>True if the property validated successfully</returns>
/// <remarks>If you override this, you MUST fire ErrorsChange and call OnValidationStateChanged() if appropriate</remarks>
/// <remarks>If you override this, you MUST fire ErrorsChanged and call OnValidationStateChanged() if appropriate</remarks>
protected virtual async Task<bool> ValidatePropertyAsync([CallerMemberName] string propertyName = null)
{
if (this.Validator == null)
throw new InvalidOperationException("Can't run validation if a validator hasn't been set");
if (propertyName == null)
propertyName = String.Empty;
if (propertyName == null || propertyName == String.Empty)
return await this.ValidateAsync().ConfigureAwait(false);
// To allow synchronous calling of this method, we need to resume on the ThreadPool.
// Therefore, we might resume on any thread, hence the need for a lock

View File

@ -138,7 +138,25 @@ namespace StyletUnitTests
}
[Test]
public void ValidatePropertyByExpressoinCallsAdapterValidate()
public void ValidatePropertyAsyncWithNullCallsAdapterValidate()
{
this.validator.Setup(x => x.ValidateAllPropertiesAsync()).Returns(Task.Delay(1).ContinueWith(t => new Dictionary<string, IEnumerable<string>>())).Verifiable();
this.model.ValidatePropertyAsync(null).Wait();
this.validator.Verify();
}
[Test]
public void ValidatePropertyAsyncWithEmptyStringCallsAdapterValidate()
{
this.validator.Setup(x => x.ValidateAllPropertiesAsync()).Returns(Task.Delay(1).ContinueWith(t => new Dictionary<string, IEnumerable<string>>())).Verifiable();
this.model.ValidatePropertyAsync(String.Empty).Wait();
this.validator.Verify();
}
[Test]
public void ValidatePropertyByExpressionCallsAdapterValidate()
{
this.validator.Setup(x => x.ValidatePropertyAsync("IntProperty")).Returns(Task.Delay(1).ContinueWith(t => Enumerable.Empty<string>())).Verifiable();
this.model.ValidateProperty(() => this.model.IntProperty);