| | 1 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | 2 | | using ValidateLib.ErrorsAndWarnings.Warnings; |
| | 3 | | using ValidateLib.Metadata.Descriptors; |
| | 4 | |
|
| | 5 | | namespace ValidateLib.Metadata.Validators |
| | 6 | | { |
| | 7 | | public class ColumnNamesValidator : IMValidator<SchemaDescriptor> |
| | 8 | | { |
| | 9 | | string? _duplicateValue; |
| | 10 | | public List<Warning> Validate(SchemaDescriptor schemaDescriptor) |
| | 11 | | { |
| | 12 | |
|
| 1 | 13 | | List<Warning> warnings = new List<Warning>(); |
| 1 | 14 | | if (schemaDescriptor.columns?._value?.Count > 0) |
| | 15 | | { |
| 1 | 16 | | var names = from column in schemaDescriptor.columns._value where column.name != null select column?.name |
| 1 | 17 | | bool hasNoDuplicates = HasNoDuplicates(names); |
| 1 | 18 | | if (!hasNoDuplicates) ErrorFactory.ThrowIdenticalColumnNamesError(_duplicateValue!); |
| | 19 | | } |
| 1 | 20 | | return warnings; |
| | 21 | | } |
| | 22 | |
|
| | 23 | | bool HasNoDuplicates(IEnumerable<string> strings) |
| | 24 | | { |
| 1 | 25 | | HashSet<string> seenStrings = new HashSet<string>(); |
| | 26 | |
|
| 1 | 27 | | foreach (string s in strings) |
| | 28 | | { |
| 1 | 29 | | if (!seenStrings.Add(s)) |
| | 30 | | { |
| 1 | 31 | | _duplicateValue = s; |
| 1 | 32 | | return false; // Found a duplicate |
| | 33 | | } |
| | 34 | | } |
| | 35 | |
|
| 1 | 36 | | return true; // No duplicates found |
| 1 | 37 | | } |
| | 38 | | } |
| | 39 | | } |