| | 1 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | 2 | | using ValidateLib.Metadata.Descriptors; |
| | 3 | | using ValidateLib.TabularData.AnnotatedTabularDataModel; |
| | 4 | | using ValidateLib.TabularData.Datatypes; |
| | 5 | |
|
| | 6 | | namespace ValidateLib.TabularData.Validation.ValidationRules |
| | 7 | | { |
| | 8 | | public class CellLengthValidationRule : ICellValidationRule |
| | 9 | | { |
| | 10 | | public void ValidateCell(Cell annotatedCell) |
| | 11 | | { |
| 1 | 12 | | if (annotatedCell.column is null) |
| 1 | 13 | | return; |
| 1 | 14 | | DatatypeDescriptor datatypeDescriptor = annotatedCell.column!.datatype!; |
| | 15 | |
|
| 1 | 16 | | if (datatypeDescriptor._base is null || datatypeDescriptor._base._value is null) |
| 0 | 17 | | return; |
| | 18 | |
|
| 1 | 19 | | var dt = DatatypeFactory.GetDatatype(null, datatypeDescriptor._base!._value!, null); |
| 1 | 20 | | if (dt is not ILength) |
| 1 | 21 | | return; |
| | 22 | |
|
| 1 | 23 | | switch (annotatedCell.cellType) |
| | 24 | | { |
| | 25 | | case CellType.NULL: |
| 1 | 26 | | var isValidLength = IsLengthValid(0, datatypeDescriptor); |
| 1 | 27 | | if (!isValidLength) |
| 0 | 28 | | annotatedCell.errors.Add(ErrorFactory.GetDatatypeLengthConstraintVE(annotatedCell)); |
| 0 | 29 | | break; |
| | 30 | | case CellType.SIMPLE_VALUE: |
| 1 | 31 | | isValidLength = IsLengthValid(((ILength)annotatedCell.value!).Length(), datatypeDescriptor); |
| 1 | 32 | | if (!isValidLength) |
| 1 | 33 | | annotatedCell.errors.Add(ErrorFactory.GetDatatypeLengthConstraintVE(annotatedCell)); |
| 1 | 34 | | break; |
| | 35 | | case CellType.LIST: |
| 1 | 36 | | foreach (var value in annotatedCell.cellValues) |
| | 37 | | { |
| 1 | 38 | | isValidLength = IsLengthValid(((ILength)value).Length(), datatypeDescriptor); |
| 1 | 39 | | if (!isValidLength) |
| 1 | 40 | | annotatedCell.errors.Add(ErrorFactory.GetDatatypeLengthConstraintVE(annotatedCell)); |
| | 41 | | } |
| | 42 | | break; |
| | 43 | | } |
| 1 | 44 | | } |
| | 45 | |
|
| | 46 | | bool IsLengthValid(int length, DatatypeDescriptor datatypeDescriptor) |
| | 47 | | { |
| 1 | 48 | | bool isValid = true; |
| 1 | 49 | | if (datatypeDescriptor.length is not null) |
| | 50 | | { |
| 1 | 51 | | isValid = datatypeDescriptor.length._value == length; |
| | 52 | | } |
| 1 | 53 | | if (datatypeDescriptor.minLength is not null) |
| | 54 | | { |
| 1 | 55 | | isValid &= datatypeDescriptor.minLength._value <= length; |
| | 56 | | } |
| 1 | 57 | | if (datatypeDescriptor.maxLength is not null) |
| | 58 | | { |
| 1 | 59 | | isValid &= datatypeDescriptor.maxLength._value >= length; |
| | 60 | | } |
| 1 | 61 | | return isValid; |
| | 62 | | } |
| | 63 | | } |
| | 64 | | } |