| | 1 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | 2 | | using ValidateLib.ErrorsAndWarnings.Warnings; |
| | 3 | | using ValidateLib.Metadata.Descriptors; |
| | 4 | | using ValidateLib.TabularData.Datatypes; |
| | 5 | | using ValidateLib.TabularData.Datatypes.NumericDatatypes; |
| | 6 | | using ValidateLib.UtilityClasses; |
| | 7 | |
|
| | 8 | | namespace ValidateLib.Metadata.Validators |
| | 9 | | { |
| | 10 | | public class DatatypeValidator : IMValidator<InheritedPropertiesDescriptor> |
| | 11 | | { |
| | 12 | | public List<Warning> Validate(InheritedPropertiesDescriptor inheritedPropertiesDescriptor) |
| | 13 | | { |
| 1 | 14 | | List<Warning> warnings = new List<Warning>(); |
| 1 | 15 | | if (inheritedPropertiesDescriptor.datatype is null) return warnings; |
| 1 | 16 | | DatatypeDescriptor datatypeDescriptor = inheritedPropertiesDescriptor.datatype._value!; |
| 1 | 17 | | ValidateFormat(datatypeDescriptor, warnings); |
| 1 | 18 | | ValidateId(datatypeDescriptor); |
| 1 | 19 | | ValidateLengthConstraints(datatypeDescriptor); |
| 1 | 20 | | ValidateValueConstraints(datatypeDescriptor, warnings); |
| | 21 | |
|
| 1 | 22 | | return warnings; |
| | 23 | | } |
| | 24 | |
|
| | 25 | | void ValidateId(DatatypeDescriptor datatypeDescriptor) |
| | 26 | | { |
| 1 | 27 | | if (datatypeDescriptor.id is not null && datatypeDescriptor.id._value!.StartsWith("_:")) |
| 0 | 28 | | ErrorFactory.ThrowInvalidIDPropertyError(datatypeDescriptor.id._value); |
| 1 | 29 | | if ( |
| 1 | 30 | | datatypeDescriptor.id is not null |
| 1 | 31 | | && DatatypeUtilityClass.IsIdOfBuiltInType(datatypeDescriptor.id._value! |
| 1 | 32 | | )) |
| | 33 | | { |
| 1 | 34 | | if ( |
| 1 | 35 | | datatypeDescriptor._base is not null || |
| 1 | 36 | | datatypeDescriptor.format is not null || |
| 1 | 37 | | datatypeDescriptor.length is not null || |
| 1 | 38 | | datatypeDescriptor.minLength is not null || |
| 1 | 39 | | datatypeDescriptor.maxLength is not null || |
| 1 | 40 | | datatypeDescriptor.minimum is not null || |
| 1 | 41 | | datatypeDescriptor.maximum is not null || |
| 1 | 42 | | datatypeDescriptor.minInclusive is not null || |
| 1 | 43 | | datatypeDescriptor.maxInclusive is not null || |
| 1 | 44 | | datatypeDescriptor.minExclusive is not null || |
| 1 | 45 | | datatypeDescriptor.maxExclusive is not null |
| 1 | 46 | | ) |
| | 47 | | { |
| 1 | 48 | | ErrorFactory.ThrowInvalidIDPropertyError(datatypeDescriptor.id._value); |
| | 49 | | } |
| | 50 | | } |
| 1 | 51 | | } |
| | 52 | |
|
| | 53 | | void ValidateLengthConstraints(DatatypeDescriptor datatypeDescriptor) |
| | 54 | | { |
| | 55 | | // check for valid base type |
| 1 | 56 | | var dt = DatatypeFactory.GetDatatype(null, datatypeDescriptor._base!._value!, null); |
| 1 | 57 | | if ( |
| 1 | 58 | | datatypeDescriptor.length is not null || |
| 1 | 59 | | datatypeDescriptor.maxLength is not null || |
| 1 | 60 | | datatypeDescriptor.minLength is not null |
| 1 | 61 | | ) |
| 1 | 62 | | if (dt is not ILength) |
| 1 | 63 | | ErrorFactory.ThrowLengthConstraintOnInvalidDatatypeBaseError(datatypeDescriptor._base._value!); |
| | 64 | |
|
| 1 | 65 | | if (datatypeDescriptor.length is not null) |
| | 66 | | { |
| 1 | 67 | | if (datatypeDescriptor.minLength is not null) |
| 1 | 68 | | if (datatypeDescriptor.length._value < datatypeDescriptor.minLength._value) |
| 1 | 69 | | ErrorFactory.ThrowDatatypeLengthConstraintError($"length: {datatypeDescriptor.length._value} min |
| 1 | 70 | | if (datatypeDescriptor.maxLength is not null) |
| 1 | 71 | | if (datatypeDescriptor.length._value > datatypeDescriptor.maxLength._value) |
| 1 | 72 | | ErrorFactory.ThrowDatatypeLengthConstraintError($"length: {datatypeDescriptor.length._value} max |
| | 73 | |
|
| | 74 | | } |
| 1 | 75 | | if (datatypeDescriptor.minLength is not null & datatypeDescriptor.maxLength is not null) |
| 1 | 76 | | if (datatypeDescriptor.minLength!._value > datatypeDescriptor.maxLength!._value) |
| 1 | 77 | | ErrorFactory.ThrowDatatypeLengthConstraintError($"minLength: {datatypeDescriptor.minLength._value} m |
| | 78 | |
|
| | 79 | |
|
| 1 | 80 | | } |
| | 81 | |
|
| | 82 | | void ValidateValueConstraints(DatatypeDescriptor datatypeDescriptor, List<Warning> warnings) |
| | 83 | | { |
| | 84 | | // check for valid base type |
| 1 | 85 | | var dt = DatatypeFactory.GetDatatype(null, datatypeDescriptor._base!._value!, null); |
| 1 | 86 | | if ( |
| 1 | 87 | | datatypeDescriptor.minimum is not null || |
| 1 | 88 | | datatypeDescriptor.maximum is not null || |
| 1 | 89 | | datatypeDescriptor.minInclusive is not null || |
| 1 | 90 | | datatypeDescriptor.maxInclusive is not null || |
| 1 | 91 | | datatypeDescriptor.minExclusive is not null || |
| 1 | 92 | | datatypeDescriptor.maxExclusive is not null |
| 1 | 93 | | ) |
| 1 | 94 | | if (dt is not IValue) |
| 1 | 95 | | ErrorFactory.ThrowValueConstraintOnInvalidDatatypeBaseError(datatypeDescriptor._base._value!); |
| | 96 | |
|
| 1 | 97 | | if (dt is not NumericBaseDT) |
| 1 | 98 | | if (datatypeDescriptor.format is not null && |
| 1 | 99 | | (datatypeDescriptor.format.decimalChar is not null || datatypeDescriptor.format.groupChar is not nul |
| | 100 | | { |
| 0 | 101 | | warnings.Add( |
| 0 | 102 | | WarningFactory.GetDecimalCharOrGroupCharNotOnNumericWarning(datatypeDescriptor._base!._value!)); |
| 0 | 103 | | datatypeDescriptor.format.decimalChar = null; |
| 0 | 104 | | datatypeDescriptor.format.groupChar = null; |
| | 105 | | } |
| | 106 | |
|
| 1 | 107 | | if (datatypeDescriptor.minimum is not null && datatypeDescriptor.minInclusive is not null) |
| 0 | 108 | | if (datatypeDescriptor.minimum._value != datatypeDescriptor.minInclusive._value) |
| | 109 | | { |
| 0 | 110 | | ErrorFactory.ThrowDatatypeValueConstraintError |
| 0 | 111 | | ( |
| 0 | 112 | | GetMessage("MinimumAndMinInclusive", datatypeDescriptor.minimum._value, datatypeDescriptor.minIn |
| 0 | 113 | | ); |
| | 114 | | } |
| | 115 | |
|
| 1 | 116 | | if (datatypeDescriptor.maximum is not null && datatypeDescriptor.maxInclusive is not null) |
| 0 | 117 | | if (datatypeDescriptor.maximum._value != datatypeDescriptor.maxInclusive._value) |
| | 118 | | { |
| 0 | 119 | | ErrorFactory.ThrowDatatypeValueConstraintError |
| 0 | 120 | | ( |
| 0 | 121 | | GetMessage("MaximumAndMaxInclusive", datatypeDescriptor.maximum._value, datatypeDescriptor.maxIn |
| 0 | 122 | | ); |
| | 123 | | } |
| | 124 | |
|
| 1 | 125 | | if (datatypeDescriptor.minInclusive is not null && datatypeDescriptor.minExclusive is not null) |
| | 126 | | { |
| 1 | 127 | | ErrorFactory.ThrowDatatypeValueConstraintError |
| 1 | 128 | | ( |
| 1 | 129 | | GetMessage("minInclusiveAndMinExclusive", datatypeDescriptor.minInclusive._value, datatypeDescriptor |
| 1 | 130 | | ); |
| | 131 | | } |
| 1 | 132 | | if (datatypeDescriptor.maxInclusive is not null && datatypeDescriptor.maxExclusive is not null) |
| | 133 | | { |
| 1 | 134 | | ErrorFactory.ThrowDatatypeValueConstraintError |
| 1 | 135 | | ( |
| 1 | 136 | | GetMessage("maxInclusiveAndMaxExclusive", datatypeDescriptor.maxInclusive._value, datatypeDescriptor |
| 1 | 137 | | ); |
| | 138 | | } |
| | 139 | |
|
| 1 | 140 | | string? minimum = GetMinimumOrMinInclusiveValue(datatypeDescriptor); |
| 1 | 141 | | string? maximum = GetMaximumOrMaxInclusiveValue(datatypeDescriptor); |
| | 142 | |
|
| 1 | 143 | | if (minimum is not null) |
| | 144 | | { |
| 1 | 145 | | var minimumdt = DatatypeFactory.GetDatatype(minimum, datatypeDescriptor._base._value!, null); |
| 1 | 146 | | if (maximum is not null) |
| | 147 | | { |
| | 148 | |
|
| 1 | 149 | | var maximumdt = DatatypeFactory.GetDatatype(maximum, datatypeDescriptor._base._value!, null); |
| 1 | 150 | | var result = minimumdt.CompareTo(maximumdt); |
| 1 | 151 | | if (result > 0) |
| | 152 | | { |
| 1 | 153 | | ErrorFactory.ThrowDatatypeValueConstraintError( |
| 1 | 154 | | GetMessage("MinimumBiggerThanMaximum", minimum, maximum) |
| 1 | 155 | | ); |
| | 156 | | } |
| | 157 | |
|
| | 158 | | } |
| 1 | 159 | | if (datatypeDescriptor.maxExclusive is not null) |
| | 160 | | { |
| 1 | 161 | | var maxExDt = DatatypeFactory.GetDatatype(datatypeDescriptor.maxExclusive._value, datatypeDescriptor |
| 1 | 162 | | var result = minimumdt.CompareTo(maxExDt); |
| 1 | 163 | | if (result >= 0) |
| 1 | 164 | | ErrorFactory.ThrowDatatypeValueConstraintError |
| 1 | 165 | | ( |
| 1 | 166 | | GetMessage("MinimumBiggerEqualMaxExclusive", minimum, datatypeDescriptor.maxExclusive) |
| 1 | 167 | | ); |
| | 168 | | } |
| | 169 | | } |
| | 170 | |
|
| 1 | 171 | | if (datatypeDescriptor.minExclusive is not null) |
| | 172 | | { |
| 1 | 173 | | var minExDt = DatatypeFactory.GetDatatype(datatypeDescriptor.minExclusive._value, datatypeDescriptor._ba |
| 1 | 174 | | if (datatypeDescriptor.maxExclusive is not null) |
| | 175 | | { |
| 1 | 176 | | var maxExDt = DatatypeFactory.GetDatatype(datatypeDescriptor.maxExclusive._value, datatypeDescriptor |
| 1 | 177 | | var result = minExDt.CompareTo(maxExDt); |
| 1 | 178 | | if (result > 0) |
| 1 | 179 | | ErrorFactory.ThrowDatatypeValueConstraintError( |
| 1 | 180 | | GetMessage("MinExclusiveBiggerThanMaxExclusive", datatypeDescriptor.minExclusive, datatypeDe |
| 1 | 181 | | ); |
| | 182 | | } |
| 1 | 183 | | if (datatypeDescriptor.maxInclusive is not null) |
| | 184 | | { |
| 1 | 185 | | var maxInDt = DatatypeFactory.GetDatatype(datatypeDescriptor.maxInclusive._value, datatypeDescriptor |
| 1 | 186 | | var result = minExDt.CompareTo(maxInDt); |
| 1 | 187 | | if (result >= 0) |
| 1 | 188 | | ErrorFactory.ThrowDatatypeValueConstraintError( |
| 1 | 189 | | GetMessage("MinExclusiveBiggerEqualMaxInclusive", datatypeDescriptor.minExclusive, datatypeD |
| 1 | 190 | | ); |
| | 191 | | } |
| | 192 | | } |
| | 193 | |
|
| | 194 | |
|
| 1 | 195 | | } |
| | 196 | | string? GetMinimumOrMinInclusiveValue(DatatypeDescriptor datatypeDescriptor) |
| | 197 | | { |
| 1 | 198 | | if (datatypeDescriptor.minimum is not null) return datatypeDescriptor.minimum._value; |
| 1 | 199 | | if (datatypeDescriptor.minInclusive is not null) return datatypeDescriptor.minInclusive._value; |
| 1 | 200 | | return null; |
| | 201 | |
|
| | 202 | | } |
| | 203 | |
|
| | 204 | | string? GetMaximumOrMaxInclusiveValue(DatatypeDescriptor datatypeDescriptor) |
| | 205 | | { |
| 1 | 206 | | if (datatypeDescriptor.maximum is not null) return datatypeDescriptor.maximum._value; |
| 1 | 207 | | if (datatypeDescriptor.maxInclusive is not null) return datatypeDescriptor.maxInclusive._value; |
| 1 | 208 | | return null; |
| | 209 | |
|
| | 210 | | } |
| | 211 | |
|
| | 212 | | void ValidateFormat(DatatypeDescriptor datatypeDescriptor, List<Warning> warnings) |
| | 213 | | { |
| 1 | 214 | | var dt = DatatypeFactory.GetDatatype(null, datatypeDescriptor._base!._value!, null); |
| | 215 | |
|
| 1 | 216 | | if (datatypeDescriptor.format is not null) |
| | 217 | | { |
| 1 | 218 | | if (dt is not NumericBaseDT && datatypeDescriptor.format.wasObject) |
| | 219 | | { |
| 1 | 220 | | warnings.Add(WarningFactory.GetFormatOnInvalidDTWarning(datatypeDescriptor._base._value!)); |
| 1 | 221 | | datatypeDescriptor.format = null; |
| | 222 | | } |
| | 223 | |
|
| | 224 | | } |
| | 225 | |
|
| 1 | 226 | | if (datatypeDescriptor.format is not null && datatypeDescriptor.format.pattern is not null) |
| | 227 | | { |
| | 228 | |
|
| 1 | 229 | | if (dt is NumericBaseDT) |
| | 230 | | { |
| 1 | 231 | | if (!dt.IsPatternValid(datatypeDescriptor.format)) |
| | 232 | | { |
| 1 | 233 | | warnings.Add(WarningFactory.GetInvalidFormatPatternWarning(datatypeDescriptor.format.pattern)); |
| 1 | 234 | | if (datatypeDescriptor.format.decimalChar is null & datatypeDescriptor.format.groupChar is null) |
| 1 | 235 | | datatypeDescriptor.format = null; |
| | 236 | | } |
| | 237 | | } |
| | 238 | | else |
| | 239 | | { |
| 1 | 240 | | if (!dt.IsPatternValid(datatypeDescriptor.format)) |
| | 241 | | { |
| 1 | 242 | | warnings.Add(WarningFactory.GetInvalidFormatPatternWarning(datatypeDescriptor.format.pattern)); |
| 1 | 243 | | datatypeDescriptor.format = null; |
| | 244 | | } |
| | 245 | |
|
| | 246 | | } |
| | 247 | | } |
| 1 | 248 | | } |
| | 249 | |
|
| | 250 | | public string GetMessage(string stringName, params object?[] parameters) |
| | 251 | | { |
| 1 | 252 | | var message = Error.ErrorManagerPublic.GetString(stringName)!; |
| 1 | 253 | | message = string.Format(message, parameters); |
| 1 | 254 | | return message; |
| | 255 | | } |
| | 256 | | } |
| | 257 | | } |