@@ -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