Skip to content

Commit 820e078

Browse files
committed
Added generic ObservableValidator.GetErrors return
1 parent b4f66bd commit 820e078

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

Microsoft.Toolkit.Mvvm/ComponentModel/ObservableValidator.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,22 @@ protected bool TrySetProperty<TModel, T>(T oldValue, T newValue, IEqualityCompar
326326
SetProperty(oldValue, newValue, comparer, model, callback, propertyName);
327327
}
328328

329-
/// <inheritdoc/>
329+
/// <inheritdoc cref="INotifyDataErrorInfo.GetErrors(string)"/>
330330
[Pure]
331-
public IEnumerable GetErrors(string? propertyName)
331+
public IEnumerable<ValidationResult> GetErrors(string? propertyName)
332332
{
333333
// Entity-level errors when the target property is null or empty
334334
if (string.IsNullOrEmpty(propertyName))
335335
{
336-
return this.GetAllErrors();
336+
// Local function to gather all the entity-level errors
337+
[Pure]
338+
[MethodImpl(MethodImplOptions.NoInlining)]
339+
IEnumerable<ValidationResult> GetAllErrors()
340+
{
341+
return this.errors.Values.SelectMany(errors => errors);
342+
}
343+
344+
return GetAllErrors();
337345
}
338346

339347
// Property-level errors, if any
@@ -350,16 +358,9 @@ public IEnumerable GetErrors(string? propertyName)
350358
return Array.Empty<ValidationResult>();
351359
}
352360

353-
/// <summary>
354-
/// Implements the logic for entity-level errors gathering for <see cref="GetErrors"/>.
355-
/// </summary>
356-
/// <returns>An <see cref="IEnumerable"/> instance with all the errors in <see cref="errors"/>.</returns>
361+
/// <inheritdoc/>
357362
[Pure]
358-
[MethodImpl(MethodImplOptions.NoInlining)]
359-
private IEnumerable GetAllErrors()
360-
{
361-
return this.errors.Values.SelectMany(errors => errors);
362-
}
363+
IEnumerable INotifyDataErrorInfo.GetErrors(string? propertyName) => GetErrors(propertyName);
363364

364365
/// <summary>
365366
/// Validates a property with a specified name and a given input value.

UnitTests/UnitTests.Shared/Mvvm/Test_ObservableValidator.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,41 +88,41 @@ public void Test_ObservableValidator_GetErrors()
8888
{
8989
var model = new Person();
9090

91-
Assert.AreEqual(model.GetErrors(null).Cast<object>().Count(), 0);
92-
Assert.AreEqual(model.GetErrors(string.Empty).Cast<object>().Count(), 0);
93-
Assert.AreEqual(model.GetErrors("ThereIsntAPropertyWithThisName").Cast<object>().Count(), 0);
94-
Assert.AreEqual(model.GetErrors(nameof(Person.Name)).Cast<object>().Count(), 0);
91+
Assert.AreEqual(model.GetErrors(null).Count(), 0);
92+
Assert.AreEqual(model.GetErrors(string.Empty).Count(), 0);
93+
Assert.AreEqual(model.GetErrors("ThereIsntAPropertyWithThisName").Count(), 0);
94+
Assert.AreEqual(model.GetErrors(nameof(Person.Name)).Count(), 0);
9595

9696
model.Name = "Foo";
9797

98-
var errors = model.GetErrors(nameof(Person.Name)).Cast<ValidationResult>().ToArray();
98+
var errors = model.GetErrors(nameof(Person.Name)).ToArray();
9999

100100
Assert.AreEqual(errors.Length, 1);
101101
Assert.AreEqual(errors[0].MemberNames.First(), nameof(Person.Name));
102102

103-
Assert.AreEqual(model.GetErrors("ThereIsntAPropertyWithThisName").Cast<object>().Count(), 0);
103+
Assert.AreEqual(model.GetErrors("ThereIsntAPropertyWithThisName").Count(), 0);
104104

105-
errors = model.GetErrors(null).Cast<ValidationResult>().ToArray();
105+
errors = model.GetErrors(null).ToArray();
106106

107107
Assert.AreEqual(errors.Length, 1);
108108
Assert.AreEqual(errors[0].MemberNames.First(), nameof(Person.Name));
109109

110-
errors = model.GetErrors(string.Empty).Cast<ValidationResult>().ToArray();
110+
errors = model.GetErrors(string.Empty).ToArray();
111111

112112
Assert.AreEqual(errors.Length, 1);
113113
Assert.AreEqual(errors[0].MemberNames.First(), nameof(Person.Name));
114114

115115
model.Age = -1;
116116

117-
errors = model.GetErrors(null).Cast<ValidationResult>().ToArray();
117+
errors = model.GetErrors(null).ToArray();
118118

119119
Assert.AreEqual(errors.Length, 2);
120120
Assert.IsTrue(errors.Any(e => e.MemberNames.First().Equals(nameof(Person.Name))));
121121
Assert.IsTrue(errors.Any(e => e.MemberNames.First().Equals(nameof(Person.Age))));
122122

123123
model.Age = 26;
124124

125-
errors = model.GetErrors(null).Cast<ValidationResult>().ToArray();
125+
errors = model.GetErrors(null).ToArray();
126126

127127
Assert.AreEqual(errors.Length, 1);
128128
Assert.IsTrue(errors.Any(e => e.MemberNames.First().Equals(nameof(Person.Name))));
@@ -145,11 +145,11 @@ public void Test_ObservableValidator_ValidateReturn(string value, bool isValid)
145145

146146
if (isValid)
147147
{
148-
Assert.IsTrue(!model.GetErrors(nameof(Person.Name)).Cast<object>().Any());
148+
Assert.IsTrue(!model.GetErrors(nameof(Person.Name)).Any());
149149
}
150150
else
151151
{
152-
Assert.IsTrue(model.GetErrors(nameof(Person.Name)).Cast<object>().Any());
152+
Assert.IsTrue(model.GetErrors(nameof(Person.Name)).Any());
153153
}
154154
}
155155

@@ -196,7 +196,7 @@ public void Test_ObservableValidator_TrySetProperty()
196196
// Errors should now be present
197197
Assert.IsTrue(model.HasErrors);
198198
Assert.IsTrue(events.Count == 1);
199-
Assert.IsTrue(model.GetErrors(nameof(Person.Name)).Cast<ValidationResult>().Any());
199+
Assert.IsTrue(model.GetErrors(nameof(Person.Name)).Any());
200200
Assert.IsTrue(model.HasErrors);
201201

202202
// Trying to set a correct property should clear the errors

0 commit comments

Comments
 (0)