|
| 1 | +using System.Text.RegularExpressions; |
| 2 | +using ServiceLayer.Mesh.FileTypes.NbssAppointmentEvents.Models; |
| 3 | + |
| 4 | +namespace ServiceLayer.Mesh.FileTypes.NbssAppointmentEvents.Validation; |
| 5 | + |
| 6 | +public partial class FileValidator : IFileValidator |
| 7 | +{ |
| 8 | + private readonly HeaderFieldRegexValidator _headerExtractIdValidator = new( |
| 9 | + x => x.ExtractId, "Extract ID", ExtractIdRegex(), |
| 10 | + ErrorCodes.MissingExtractId, ErrorCodes.InvalidExtractId); |
| 11 | + |
| 12 | + private readonly HeaderFieldRegexValidator _headerIdRecordCountValidator = new( |
| 13 | + x => x.RecordCount, "Record count", RecordCountRegex(), |
| 14 | + ErrorCodes.MissingRecordCount, ErrorCodes.InvalidRecordCount); |
| 15 | + |
| 16 | + public IEnumerable<ValidationError> Validate(ParsedFile file) |
| 17 | + { |
| 18 | + return ValidateHeaderPresence(file) |
| 19 | + .Concat(ValidateTrailerPresence(file)) |
| 20 | + .Concat(ValidateExtractId(file)) |
| 21 | + .Concat(ValidateRecordCount(file)); |
| 22 | + } |
| 23 | + |
| 24 | + private static IEnumerable<ValidationError> ValidateHeaderPresence(ParsedFile file) |
| 25 | + { |
| 26 | + if (file.FileHeader == null) |
| 27 | + { |
| 28 | + yield return new ValidationError |
| 29 | + { |
| 30 | + Code = ErrorCodes.MissingHeader, |
| 31 | + Error = "Header is missing", |
| 32 | + Scope = ValidationErrorScope.File |
| 33 | + }; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private static IEnumerable<ValidationError> ValidateTrailerPresence(ParsedFile file) |
| 38 | + { |
| 39 | + if (file.FileTrailer == null) |
| 40 | + { |
| 41 | + yield return new ValidationError |
| 42 | + { |
| 43 | + Code = ErrorCodes.MissingTrailer, |
| 44 | + Error = "Trailer is missing", |
| 45 | + Scope = ValidationErrorScope.File |
| 46 | + }; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private IEnumerable<ValidationError> ValidateExtractId(ParsedFile file) |
| 51 | + { |
| 52 | + if (file.FileHeader == null) yield break; |
| 53 | + |
| 54 | + foreach (var error in _headerExtractIdValidator.Validate(file)) |
| 55 | + { |
| 56 | + yield return error; |
| 57 | + } |
| 58 | + |
| 59 | + if (file.FileTrailer != null && file.FileHeader.ExtractId != file.FileTrailer.ExtractId) |
| 60 | + { |
| 61 | + yield return new ValidationError |
| 62 | + { |
| 63 | + Field = "Extract ID", |
| 64 | + Code = ErrorCodes.InconsistentExtractId, |
| 65 | + Error = "Extract ID does not match value in header", |
| 66 | + Scope = ValidationErrorScope.Trailer |
| 67 | + }; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private IEnumerable<ValidationError> ValidateRecordCount(ParsedFile file) |
| 72 | + { |
| 73 | + if (file.FileHeader == null) yield break; |
| 74 | + |
| 75 | + var headerRecordCountErrors = _headerIdRecordCountValidator.Validate(file).ToList(); |
| 76 | + |
| 77 | + foreach (var error in headerRecordCountErrors) |
| 78 | + { |
| 79 | + yield return error; |
| 80 | + } |
| 81 | + |
| 82 | + if (file.FileTrailer != null && file.FileHeader.RecordCount != file.FileTrailer.RecordCount) |
| 83 | + { |
| 84 | + yield return new ValidationError |
| 85 | + { |
| 86 | + Field = "Record count", |
| 87 | + Code = ErrorCodes.InconsistentRecordCount, |
| 88 | + Error = "Record count does not match value in header", |
| 89 | + Scope = ValidationErrorScope.Trailer |
| 90 | + }; |
| 91 | + } |
| 92 | + else if (headerRecordCountErrors.Count == 0 && |
| 93 | + file.DataRecords.Count != int.Parse(file.FileHeader.RecordCount!)) |
| 94 | + { |
| 95 | + yield return new ValidationError |
| 96 | + { |
| 97 | + Code = ErrorCodes.UnexpectedRecordCount, |
| 98 | + Error = "Record count does not match value in header and trailer", |
| 99 | + Scope = ValidationErrorScope.File |
| 100 | + }; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + [GeneratedRegex(@"^\d{8}$")] |
| 105 | + private static partial Regex ExtractIdRegex(); |
| 106 | + |
| 107 | + [GeneratedRegex(@"^(?!000000)\d{6}$")] |
| 108 | + private static partial Regex RecordCountRegex(); |
| 109 | +} |
0 commit comments