| | | 1 | | using System.Text; |
| | | 2 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | | 3 | | using ValidateLib.ErrorsAndWarnings.Errors.ValidationErrors; |
| | | 4 | | using ValidateLib.TabularData.AnnotatedTabularDataModel; |
| | | 5 | | |
| | | 6 | | namespace ValidateLib.TabularData.Validation.ValidationRules |
| | | 7 | | { |
| | | 8 | | public class PrimaryKeyRowValidationRule : IRowValidationRule |
| | | 9 | | { |
| | 1 | 10 | | Dictionary<string, bool> primaryKeyValues = new Dictionary<string, bool>(); |
| | | 11 | | public List<Error> ValidateRow(Row annotatedRow) |
| | | 12 | | { |
| | 1 | 13 | | List<Error> errors = new List<Error>(); |
| | 1 | 14 | | if (annotatedRow.primaryKeyDescriptor is null) |
| | 1 | 15 | | return errors; |
| | | 16 | | |
| | | 17 | | |
| | 1 | 18 | | string primaryKeyString = GetPrimaryKeyValueString(annotatedRow); |
| | | 19 | | |
| | 1 | 20 | | if (primaryKeyValues.ContainsKey(primaryKeyString)) |
| | | 21 | | { |
| | 1 | 22 | | errors.Add(ErrorFactory.GetDuplicateInPKValidationError(annotatedRow, GetPrimaryKeyValueString(annotated |
| | | 23 | | } |
| | | 24 | | else |
| | | 25 | | { |
| | 1 | 26 | | primaryKeyValues[primaryKeyString] = true; |
| | | 27 | | } |
| | 1 | 28 | | return errors; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | string GetPrimaryKeyValueString(Row annotatedRow, string delimiter = "") |
| | | 32 | | { |
| | 1 | 33 | | StringBuilder primaryKey = new StringBuilder(); |
| | 1 | 34 | | foreach (var cell in annotatedRow.cells) |
| | | 35 | | { |
| | 1 | 36 | | if (cell.column is null) |
| | 0 | 37 | | break; |
| | 1 | 38 | | if (cell.column.isPartOfPrimaryKey) |
| | | 39 | | { |
| | 1 | 40 | | primaryKey.Append(delimiter + cell.stringValue); |
| | | 41 | | } |
| | | 42 | | } |
| | 1 | 43 | | return primaryKey.ToString(); |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | } |