< Summary

Information
Class: ValidateLib.Metadata.Validators.DatatypeValidator
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Metadata\Validators\DatatypeValidator.cs
Line coverage
89%
Covered lines: 133
Uncovered lines: 15
Coverable lines: 148
Total lines: 257
Line coverage: 89.8%
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

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Metadata\Validators\DatatypeValidator.cs

#LineLine coverage
 1using ValidateLib.ErrorsAndWarnings.Errors;
 2using ValidateLib.ErrorsAndWarnings.Warnings;
 3using ValidateLib.Metadata.Descriptors;
 4using ValidateLib.TabularData.Datatypes;
 5using ValidateLib.TabularData.Datatypes.NumericDatatypes;
 6using ValidateLib.UtilityClasses;
 7
 8namespace ValidateLib.Metadata.Validators
 9{
 10    public class DatatypeValidator : IMValidator<InheritedPropertiesDescriptor>
 11    {
 12        public List<Warning> Validate(InheritedPropertiesDescriptor inheritedPropertiesDescriptor)
 13        {
 114            List<Warning> warnings = new List<Warning>();
 115            if (inheritedPropertiesDescriptor.datatype is null) return warnings;
 116            DatatypeDescriptor datatypeDescriptor = inheritedPropertiesDescriptor.datatype._value!;
 117            ValidateFormat(datatypeDescriptor, warnings);
 118            ValidateId(datatypeDescriptor);
 119            ValidateLengthConstraints(datatypeDescriptor);
 120            ValidateValueConstraints(datatypeDescriptor, warnings);
 21
 122            return warnings;
 23        }
 24
 25        void ValidateId(DatatypeDescriptor datatypeDescriptor)
 26        {
 127            if (datatypeDescriptor.id is not null && datatypeDescriptor.id._value!.StartsWith("_:"))
 028                ErrorFactory.ThrowInvalidIDPropertyError(datatypeDescriptor.id._value);
 129            if (
 130                datatypeDescriptor.id is not null
 131                && DatatypeUtilityClass.IsIdOfBuiltInType(datatypeDescriptor.id._value!
 132                ))
 33            {
 134                if (
 135                    datatypeDescriptor._base is not null ||
 136                    datatypeDescriptor.format is not null ||
 137                    datatypeDescriptor.length is not null ||
 138                    datatypeDescriptor.minLength is not null ||
 139                    datatypeDescriptor.maxLength is not null ||
 140                    datatypeDescriptor.minimum is not null ||
 141                    datatypeDescriptor.maximum is not null ||
 142                    datatypeDescriptor.minInclusive is not null ||
 143                    datatypeDescriptor.maxInclusive is not null ||
 144                    datatypeDescriptor.minExclusive is not null ||
 145                    datatypeDescriptor.maxExclusive is not null
 146                    )
 47                {
 148                    ErrorFactory.ThrowInvalidIDPropertyError(datatypeDescriptor.id._value);
 49                }
 50            }
 151        }
 52
 53        void ValidateLengthConstraints(DatatypeDescriptor datatypeDescriptor)
 54        {
 55            // check for valid base type
 156            var dt = DatatypeFactory.GetDatatype(null, datatypeDescriptor._base!._value!, null);
 157            if (
 158                datatypeDescriptor.length is not null ||
 159                datatypeDescriptor.maxLength is not null ||
 160                datatypeDescriptor.minLength is not null
 161                )
 162                if (dt is not ILength)
 163                    ErrorFactory.ThrowLengthConstraintOnInvalidDatatypeBaseError(datatypeDescriptor._base._value!);
 64
 165            if (datatypeDescriptor.length is not null)
 66            {
 167                if (datatypeDescriptor.minLength is not null)
 168                    if (datatypeDescriptor.length._value < datatypeDescriptor.minLength._value)
 169                        ErrorFactory.ThrowDatatypeLengthConstraintError($"length: {datatypeDescriptor.length._value} min
 170                if (datatypeDescriptor.maxLength is not null)
 171                    if (datatypeDescriptor.length._value > datatypeDescriptor.maxLength._value)
 172                        ErrorFactory.ThrowDatatypeLengthConstraintError($"length: {datatypeDescriptor.length._value} max
 73
 74            }
 175            if (datatypeDescriptor.minLength is not null & datatypeDescriptor.maxLength is not null)
 176                if (datatypeDescriptor.minLength!._value > datatypeDescriptor.maxLength!._value)
 177                    ErrorFactory.ThrowDatatypeLengthConstraintError($"minLength: {datatypeDescriptor.minLength._value} m
 78
 79
 180        }
 81
 82        void ValidateValueConstraints(DatatypeDescriptor datatypeDescriptor, List<Warning> warnings)
 83        {
 84            // check for valid base type
 185            var dt = DatatypeFactory.GetDatatype(null, datatypeDescriptor._base!._value!, null);
 186            if (
 187                datatypeDescriptor.minimum is not null ||
 188                datatypeDescriptor.maximum is not null ||
 189                datatypeDescriptor.minInclusive is not null ||
 190                datatypeDescriptor.maxInclusive is not null ||
 191                datatypeDescriptor.minExclusive is not null ||
 192                datatypeDescriptor.maxExclusive is not null
 193                )
 194                if (dt is not IValue)
 195                    ErrorFactory.ThrowValueConstraintOnInvalidDatatypeBaseError(datatypeDescriptor._base._value!);
 96
 197            if (dt is not NumericBaseDT)
 198                if (datatypeDescriptor.format is not null &&
 199                    (datatypeDescriptor.format.decimalChar is not null || datatypeDescriptor.format.groupChar is not nul
 100                {
 0101                    warnings.Add(
 0102                        WarningFactory.GetDecimalCharOrGroupCharNotOnNumericWarning(datatypeDescriptor._base!._value!));
 0103                    datatypeDescriptor.format.decimalChar = null;
 0104                    datatypeDescriptor.format.groupChar = null;
 105                }
 106
 1107            if (datatypeDescriptor.minimum is not null && datatypeDescriptor.minInclusive is not null)
 0108                if (datatypeDescriptor.minimum._value != datatypeDescriptor.minInclusive._value)
 109                {
 0110                    ErrorFactory.ThrowDatatypeValueConstraintError
 0111                        (
 0112                        GetMessage("MinimumAndMinInclusive", datatypeDescriptor.minimum._value, datatypeDescriptor.minIn
 0113                        );
 114                }
 115
 1116            if (datatypeDescriptor.maximum is not null && datatypeDescriptor.maxInclusive is not null)
 0117                if (datatypeDescriptor.maximum._value != datatypeDescriptor.maxInclusive._value)
 118                {
 0119                    ErrorFactory.ThrowDatatypeValueConstraintError
 0120                        (
 0121                        GetMessage("MaximumAndMaxInclusive", datatypeDescriptor.maximum._value, datatypeDescriptor.maxIn
 0122                        );
 123                }
 124
 1125            if (datatypeDescriptor.minInclusive is not null && datatypeDescriptor.minExclusive is not null)
 126            {
 1127                ErrorFactory.ThrowDatatypeValueConstraintError
 1128                    (
 1129                    GetMessage("minInclusiveAndMinExclusive", datatypeDescriptor.minInclusive._value, datatypeDescriptor
 1130                    );
 131            }
 1132            if (datatypeDescriptor.maxInclusive is not null && datatypeDescriptor.maxExclusive is not null)
 133            {
 1134                ErrorFactory.ThrowDatatypeValueConstraintError
 1135                    (
 1136                    GetMessage("maxInclusiveAndMaxExclusive", datatypeDescriptor.maxInclusive._value, datatypeDescriptor
 1137                    );
 138            }
 139
 1140            string? minimum = GetMinimumOrMinInclusiveValue(datatypeDescriptor);
 1141            string? maximum = GetMaximumOrMaxInclusiveValue(datatypeDescriptor);
 142
 1143            if (minimum is not null)
 144            {
 1145                var minimumdt = DatatypeFactory.GetDatatype(minimum, datatypeDescriptor._base._value!, null);
 1146                if (maximum is not null)
 147                {
 148
 1149                    var maximumdt = DatatypeFactory.GetDatatype(maximum, datatypeDescriptor._base._value!, null);
 1150                    var result = minimumdt.CompareTo(maximumdt);
 1151                    if (result > 0)
 152                    {
 1153                        ErrorFactory.ThrowDatatypeValueConstraintError(
 1154                            GetMessage("MinimumBiggerThanMaximum", minimum, maximum)
 1155                            );
 156                    }
 157
 158                }
 1159                if (datatypeDescriptor.maxExclusive is not null)
 160                {
 1161                    var maxExDt = DatatypeFactory.GetDatatype(datatypeDescriptor.maxExclusive._value, datatypeDescriptor
 1162                    var result = minimumdt.CompareTo(maxExDt);
 1163                    if (result >= 0)
 1164                        ErrorFactory.ThrowDatatypeValueConstraintError
 1165                            (
 1166                            GetMessage("MinimumBiggerEqualMaxExclusive", minimum, datatypeDescriptor.maxExclusive)
 1167                            );
 168                }
 169            }
 170
 1171            if (datatypeDescriptor.minExclusive is not null)
 172            {
 1173                var minExDt = DatatypeFactory.GetDatatype(datatypeDescriptor.minExclusive._value, datatypeDescriptor._ba
 1174                if (datatypeDescriptor.maxExclusive is not null)
 175                {
 1176                    var maxExDt = DatatypeFactory.GetDatatype(datatypeDescriptor.maxExclusive._value, datatypeDescriptor
 1177                    var result = minExDt.CompareTo(maxExDt);
 1178                    if (result > 0)
 1179                        ErrorFactory.ThrowDatatypeValueConstraintError(
 1180                            GetMessage("MinExclusiveBiggerThanMaxExclusive", datatypeDescriptor.minExclusive, datatypeDe
 1181                            );
 182                }
 1183                if (datatypeDescriptor.maxInclusive is not null)
 184                {
 1185                    var maxInDt = DatatypeFactory.GetDatatype(datatypeDescriptor.maxInclusive._value, datatypeDescriptor
 1186                    var result = minExDt.CompareTo(maxInDt);
 1187                    if (result >= 0)
 1188                        ErrorFactory.ThrowDatatypeValueConstraintError(
 1189                            GetMessage("MinExclusiveBiggerEqualMaxInclusive", datatypeDescriptor.minExclusive, datatypeD
 1190                            );
 191                }
 192            }
 193
 194
 1195        }
 196        string? GetMinimumOrMinInclusiveValue(DatatypeDescriptor datatypeDescriptor)
 197        {
 1198            if (datatypeDescriptor.minimum is not null) return datatypeDescriptor.minimum._value;
 1199            if (datatypeDescriptor.minInclusive is not null) return datatypeDescriptor.minInclusive._value;
 1200            return null;
 201
 202        }
 203
 204        string? GetMaximumOrMaxInclusiveValue(DatatypeDescriptor datatypeDescriptor)
 205        {
 1206            if (datatypeDescriptor.maximum is not null) return datatypeDescriptor.maximum._value;
 1207            if (datatypeDescriptor.maxInclusive is not null) return datatypeDescriptor.maxInclusive._value;
 1208            return null;
 209
 210        }
 211
 212        void ValidateFormat(DatatypeDescriptor datatypeDescriptor, List<Warning> warnings)
 213        {
 1214            var dt = DatatypeFactory.GetDatatype(null, datatypeDescriptor._base!._value!, null);
 215
 1216            if (datatypeDescriptor.format is not null)
 217            {
 1218                if (dt is not NumericBaseDT && datatypeDescriptor.format.wasObject)
 219                {
 1220                    warnings.Add(WarningFactory.GetFormatOnInvalidDTWarning(datatypeDescriptor._base._value!));
 1221                    datatypeDescriptor.format = null;
 222                }
 223
 224            }
 225
 1226            if (datatypeDescriptor.format is not null && datatypeDescriptor.format.pattern is not null)
 227            {
 228
 1229                if (dt is NumericBaseDT)
 230                {
 1231                    if (!dt.IsPatternValid(datatypeDescriptor.format))
 232                    {
 1233                        warnings.Add(WarningFactory.GetInvalidFormatPatternWarning(datatypeDescriptor.format.pattern));
 1234                        if (datatypeDescriptor.format.decimalChar is null & datatypeDescriptor.format.groupChar is null)
 1235                            datatypeDescriptor.format = null;
 236                    }
 237                }
 238                else
 239                {
 1240                    if (!dt.IsPatternValid(datatypeDescriptor.format))
 241                    {
 1242                        warnings.Add(WarningFactory.GetInvalidFormatPatternWarning(datatypeDescriptor.format.pattern));
 1243                        datatypeDescriptor.format = null;
 244                    }
 245
 246                }
 247            }
 1248        }
 249
 250        public string GetMessage(string stringName, params object?[] parameters)
 251        {
 1252            var message = Error.ErrorManagerPublic.GetString(stringName)!;
 1253            message = string.Format(message, parameters);
 1254            return message;
 255        }
 256    }
 257}