1+ using ServiceLayer . Mesh . Configuration ;
12using ServiceLayer . Mesh . FileTypes . NbssAppointmentEvents . Models ;
23
34namespace ServiceLayer . Mesh . FileTypes . NbssAppointmentEvents . Validation ;
45
56public class ValidationRunner (
7+ IValidationRunnerConfiguration configuration ,
68 IEnumerable < IFileValidator > fileValidators ,
79 IEnumerable < IRecordValidator > recordValidators )
810 : IValidationRunner
@@ -11,18 +13,61 @@ public IList<ValidationError> Validate(ParsedFile file)
1113 {
1214 var errors = new List < ValidationError > ( ) ;
1315
16+ RunFileValidators ( file , errors ) ;
17+ if ( errors . Count >= configuration . MaximumValidationErrors )
18+ {
19+ return FinalizeEarly ( errors ) ;
20+ }
21+
22+ RunRecordValidators ( file , errors ) ;
23+ if ( errors . Count >= configuration . MaximumValidationErrors )
24+ {
25+ return FinalizeEarly ( errors ) ;
26+ }
27+
28+ return errors ;
29+ }
30+
31+ private void RunFileValidators ( ParsedFile file , List < ValidationError > errors )
32+ {
1433 foreach ( var validator in fileValidators )
1534 {
16- errors . AddRange ( validator . Validate ( file ) ) ;
35+ var results = validator . Validate ( file ) ;
36+ AddErrorsWithCap ( results , errors ) ;
37+ if ( errors . Count >= configuration . MaximumValidationErrors ) return ;
1738 }
39+ }
1840
19- foreach ( var dataRecord in file . DataRecords )
41+ private void RunRecordValidators ( ParsedFile file , List < ValidationError > errors )
42+ {
43+ foreach ( var record in file . DataRecords )
2044 {
21- foreach ( var recordValidator in recordValidators )
45+ foreach ( var validator in recordValidators )
2246 {
23- errors . AddRange ( recordValidator . Validate ( dataRecord ) ) ;
47+ var results = validator . Validate ( record ) ;
48+ AddErrorsWithCap ( results , errors ) ;
49+ if ( errors . Count >= configuration . MaximumValidationErrors ) return ;
2450 }
2551 }
52+ }
53+
54+ private void AddErrorsWithCap ( IEnumerable < ValidationError > newErrors , List < ValidationError > existingErrors )
55+ {
56+ foreach ( var error in newErrors )
57+ {
58+ if ( existingErrors . Count >= configuration . MaximumValidationErrors ) break ;
59+ existingErrors . Add ( error ) ;
60+ }
61+ }
62+
63+ private List < ValidationError > FinalizeEarly ( List < ValidationError > errors )
64+ {
65+ errors . Add ( new ValidationError
66+ {
67+ Code = ErrorCodes . ValidationAborted ,
68+ Error = $ "Validation aborted after { configuration . MaximumValidationErrors } errors encountered",
69+ Scope = ValidationErrorScope . File
70+ } ) ;
2671
2772 return errors ;
2873 }
0 commit comments