< Summary

Information
Class: ValidateLib.TabularData.Validation.ValidationRules.CellLengthValidationRule
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\TabularData\Validation\ValidationRules\CellLengthValidationRule.cs
Line coverage
90%
Covered lines: 27
Uncovered lines: 3
Coverable lines: 30
Total lines: 64
Line coverage: 90%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBlocks covered Blocks not covered
ValidateCell(...)426
IsLengthValid(...)130

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\TabularData\Validation\ValidationRules\CellLengthValidationRule.cs

#LineLine coverage
 1using ValidateLib.ErrorsAndWarnings.Errors;
 2using ValidateLib.Metadata.Descriptors;
 3using ValidateLib.TabularData.AnnotatedTabularDataModel;
 4using ValidateLib.TabularData.Datatypes;
 5
 6namespace ValidateLib.TabularData.Validation.ValidationRules
 7{
 8    public class CellLengthValidationRule : ICellValidationRule
 9    {
 10        public void ValidateCell(Cell annotatedCell)
 11        {
 112            if (annotatedCell.column is null)
 113                return;
 114            DatatypeDescriptor datatypeDescriptor = annotatedCell.column!.datatype!;
 15
 116            if (datatypeDescriptor._base is null || datatypeDescriptor._base._value is null)
 017                return;
 18
 119            var dt = DatatypeFactory.GetDatatype(null, datatypeDescriptor._base!._value!, null);
 120            if (dt is not ILength)
 121                return;
 22
 123            switch (annotatedCell.cellType)
 24            {
 25                case CellType.NULL:
 126                    var isValidLength = IsLengthValid(0, datatypeDescriptor);
 127                    if (!isValidLength)
 028                        annotatedCell.errors.Add(ErrorFactory.GetDatatypeLengthConstraintVE(annotatedCell));
 029                    break;
 30                case CellType.SIMPLE_VALUE:
 131                    isValidLength = IsLengthValid(((ILength)annotatedCell.value!).Length(), datatypeDescriptor);
 132                    if (!isValidLength)
 133                        annotatedCell.errors.Add(ErrorFactory.GetDatatypeLengthConstraintVE(annotatedCell));
 134                    break;
 35                case CellType.LIST:
 136                    foreach (var value in annotatedCell.cellValues)
 37                    {
 138                        isValidLength = IsLengthValid(((ILength)value).Length(), datatypeDescriptor);
 139                        if (!isValidLength)
 140                            annotatedCell.errors.Add(ErrorFactory.GetDatatypeLengthConstraintVE(annotatedCell));
 41                    }
 42                    break;
 43            }
 144        }
 45
 46        bool IsLengthValid(int length, DatatypeDescriptor datatypeDescriptor)
 47        {
 148            bool isValid = true;
 149            if (datatypeDescriptor.length is not null)
 50            {
 151                isValid = datatypeDescriptor.length._value == length;
 52            }
 153            if (datatypeDescriptor.minLength is not null)
 54            {
 155                isValid &= datatypeDescriptor.minLength._value <= length;
 56            }
 157            if (datatypeDescriptor.maxLength is not null)
 58            {
 159                isValid &= datatypeDescriptor.maxLength._value >= length;
 60            }
 161            return isValid;
 62        }
 63    }
 64}