Skip to content

Commit 3ced165

Browse files
committed
Updated FluentValidatorCollection to support interface to allow for complex collections
1 parent 8504af2 commit 3ced165

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/MADE.Data.Validation.FluentValidation/FluentValidatorCollection{T}.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ namespace MADE.Data.Validation
1111
/// Defines a list of <see cref="FluentValidation.IValidator"/> objects that can be accessed by index.
1212
/// </summary>
1313
/// <typeparam name="T">The type of item being validated.</typeparam>
14-
public class FluentValidatorCollection<T> : List<AbstractValidator<T>>, IValidatorCollection
15-
where T : class
14+
public class FluentValidatorCollection<T> : List<IValidator<T>>, IValidatorCollection
1615
{
1716
private readonly List<string> feedbackMessages = new();
1817

@@ -71,7 +70,7 @@ public void Validate(object value)
7170

7271
this.ForEach(validator =>
7372
{
74-
var result = validator.Validate(value as T);
73+
var result = validator.Validate((T)value);
7574
if (!result.IsValid)
7675
{
7776
IsInvalid = true;

tests/MADE.Data.Validation.FluentValidation.Tests/Tests/FluentValidatorCollectionTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ namespace MADE.Data.Validation.FluentValidation.Tests.Tests
44
using System.Collections.Generic;
55
using System.Diagnostics.CodeAnalysis;
66
using System.Linq;
7+
using System.Threading;
8+
using System.Threading.Tasks;
79
using global::FluentValidation;
10+
using global::FluentValidation.Results;
811
using MADE.Testing;
912
using NUnit.Framework;
1013
using Shouldly;
@@ -147,6 +150,32 @@ public void ShouldHaveFeedbackMessagesIfInvalidValue()
147150
collection.FeedbackMessages.Count().ShouldBe(1);
148151
collection.FeedbackMessages.FirstOrDefault().ShouldBe(PersonValidator.DateOfBirthValidationMessage);
149152
}
153+
154+
[Test]
155+
public void ShouldValidateComplexObjectWithMultipleValidators()
156+
{
157+
// Arrange
158+
var value = new Staff
159+
{
160+
Name = "Joe Bloggs",
161+
JobTitle = null, // Invalid job title
162+
Department = "Build",
163+
DateOfBirth = DateTime.UtcNow.AddDays(1) // Invalid birth date
164+
};
165+
166+
var collection = new FluentValidatorCollection<Person> { new PersonValidator(), new StaffValidator() };
167+
168+
// Act
169+
collection.Validate(value);
170+
171+
// Assert
172+
collection.FeedbackMessages.ShouldNotBeEmpty();
173+
collection.FeedbackMessages.Count().ShouldBe(2);
174+
collection.FeedbackMessages.FirstOrDefault(x => x.Equals(PersonValidator.DateOfBirthValidationMessage,
175+
StringComparison.InvariantCultureIgnoreCase)).ShouldNotBeNull();
176+
collection.FeedbackMessages.FirstOrDefault(x => x.Equals(StaffValidator.JobTitleValidationMessage,
177+
StringComparison.InvariantCultureIgnoreCase)).ShouldNotBeNull();
178+
}
150179
}
151180
}
152181

@@ -157,6 +186,13 @@ public class Person
157186
public DateTime? DateOfBirth { get; set; }
158187
}
159188

189+
public class Staff : Person
190+
{
191+
public string JobTitle { get; set; }
192+
193+
public string Department { get; set; }
194+
}
195+
160196
public class PersonValidator : AbstractValidator<Person>
161197
{
162198
public const string DateOfBirthValidationMessage = "Please specify a valid date of birth";
@@ -170,4 +206,25 @@ public PersonValidator()
170206
.WithMessage(DateOfBirthValidationMessage);
171207
}
172208
}
209+
210+
public class StaffValidator : AbstractValidator<Staff>, IValidator<Person>
211+
{
212+
public const string JobTitleValidationMessage = "Please specify a job title";
213+
214+
public StaffValidator()
215+
{
216+
this.RuleFor(x => x.JobTitle).NotEmpty().WithMessage(JobTitleValidationMessage);
217+
this.RuleFor(x => x.Department).NotEmpty();
218+
}
219+
220+
public ValidationResult Validate(Person instance)
221+
{
222+
return base.Validate(instance as Staff);
223+
}
224+
225+
public Task<ValidationResult> ValidateAsync(Person instance, CancellationToken cancellation = new())
226+
{
227+
return base.ValidateAsync(instance as Staff, cancellation);
228+
}
229+
}
173230
}

0 commit comments

Comments
 (0)