< Summary

Information
Class: ValidateLib.Metadata.Validators.CommonPropertyValidator
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Metadata\Validators\CommonPropertyValidator.cs
Line coverage
91%
Covered lines: 78
Uncovered lines: 7
Coverable lines: 85
Total lines: 158
Line coverage: 91.7%
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\CommonPropertyValidator.cs

#LineLine coverage
 1using Newtonsoft.Json.Linq;
 2using ValidateLib.ErrorsAndWarnings.Errors;
 3using ValidateLib.ErrorsAndWarnings.Warnings;
 4using ValidateLib.Metadata.Properties;
 5using ValidateLib.UtilityClasses;
 6
 7namespace ValidateLib.Metadata.Validators
 8{
 9    public class CommonPropertyValidator : IMValidator<CommonProperty>
 10    {
 11        CommonProperty _property;
 112        public CommonPropertyValidator(CommonProperty property)
 13        {
 114            _property = property;
 115        }
 16        public List<Warning> Validate(CommonProperty commonProperty)
 17        {
 118            var warnings = new List<Warning>();
 119            if (commonProperty!._value!.Type == JTokenType.Object)
 20            {
 121                var jToken = commonProperty._value;
 122                var jObject = (JObject)jToken!;
 123                if (jObject["@value"] is not null)
 24                {
 125                    VerifyCommonPropertyWithValueDoesNotHaveBothTypeAndLanguageProperty(jObject);
 126                    VerifyCommonPropertyWithValueDoesNotHaveOtherProperties(jObject);
 127                    verifyValueType(jObject);
 128                    if (jObject["@type"] is not null)
 129                        verifyTypeWithValuePresent(jObject.Property("@type")!);
 130                    if (jObject["@language"] is not null)
 131                        verifyLanguageValuePresent(jObject.Property("@language")!);
 32
 33                }
 34                else
 35                {
 36                    // if id value is not string or starts with :_ is checked during normalization, otherwise it would b
 137                    if (jObject["@type"] is not null)
 138                        verifyTypeValueNotPresent(jObject.Property("@type")!);
 139                    if (jObject["@language"] is not null)
 140                        ErrorFactory.ThrowCommonPropertyValueErrorError(commonProperty);
 41                }
 142                verifyObjectDoesNotHaveProhibitedAtProperties(jObject);
 43
 44            }
 145            if (commonProperty._value.Type == JTokenType.Array)
 46            {
 147                foreach (var item in commonProperty._value)
 48                {
 149                    if (item.Type != JTokenType.Object)
 050                        ErrorFactory.ThrowCommonPropertyValueErrorError(commonProperty);
 151                    Validate(new CommonProperty(null, item));
 52                }
 53
 54            }
 155            return warnings;
 56
 57        }
 58
 59        void VerifyCommonPropertyWithValueDoesNotHaveBothTypeAndLanguageProperty(JObject commonPropertyValue)
 60        {
 61
 162            if (commonPropertyValue["@type"] is not null && commonPropertyValue["@language"] is not null)
 163                ErrorFactory.ThrowCommonPropertyValueErrorError(_property);
 164        }
 65        void VerifyCommonPropertyWithValueDoesNotHaveOtherProperties(JObject commonPropertyValue)
 66        {
 167            foreach (var property in commonPropertyValue.Properties())
 68            {
 169                if (property.Name != "@language" && property.Name != "@type" && property.Name != "@value")
 170                    ErrorFactory.ThrowCommonPropertyValueErrorError(_property);
 71            }
 172        }
 73        void verifyValueType(JObject commonPropertyValue)
 74        {
 75
 176            var type = commonPropertyValue!["@value"]!.Type;
 177            if (
 178                type != JTokenType.Object &&
 179                type != JTokenType.String &&
 180                type != JTokenType.Array &&
 181                type != JTokenType.Date
 182                )
 083                ErrorFactory.ThrowCPInvalidValueTypeError(commonPropertyValue);
 184        }
 85
 86        void verifyTypeWithValuePresent(JProperty typeProperty)
 87        {
 188            if (
 189                !DatatypeUtilityClass.IsBuiltInType(typeProperty.Value.ToString()) &&
 190                !CommonPropertiesPrefixes.IsPrefixedName(typeProperty.Value.ToString()) &&
 191                !IriUtilityClass.IsAbsoluteUrl(typeProperty.Value.ToString())
 192                )
 193                ErrorFactory.ThrowCommonPropertyValueErrorError(_property);
 194        }
 95
 96        void verifyLanguageValuePresent(JProperty languageProperty)
 97        {
 198            if (languageProperty.Value.Type == JTokenType.String)
 99            {
 1100                if (!LanguageUtilityClass.IsCorrectLanguageCode(languageProperty.Value.ToString()))
 0101                    ErrorFactory.ThrowCommonPropertyValueErrorError(_property);
 102            }
 1103            else if (languageProperty.Value.Type == JTokenType.Null) return;
 104            else
 1105                ErrorFactory.ThrowCommonPropertyValueErrorError(_property);
 1106        }
 107
 108        void verifyTypeValueNotPresent(JProperty typeProperty)
 109        {
 1110            if (typeProperty.Value.Type == JTokenType.String)
 111            {
 1112                string[] definedTypesCSVW = new string[]
 1113                {
 1114                "TableGroup",
 1115                "Table",
 1116                "Schema",
 1117                "Column",
 1118                "Dialect",
 1119                "Template"
 1120                };
 121
 1122                if (
 1123
 1124                    !definedTypesCSVW.Contains(typeProperty.Value.ToString()) &&
 1125                    !CommonPropertiesPrefixes.IsPrefixedName(typeProperty.Value.ToString()) &&
 1126                    !IriUtilityClass.IsAbsoluteUrl(typeProperty.Value.ToString())
 1127                    )
 1128                    ErrorFactory.ThrowCommonPropertyValueErrorError(_property);
 1129                if (typeProperty.Value.ToString().StartsWith("_:"))
 0130                    ErrorFactory.ThrowCommonPropertyValueErrorError(_property);
 131            }
 1132            else if (typeProperty.Value.Type == JTokenType.Array)
 133            {
 0134                foreach (var item in (JArray)typeProperty.Value)
 135                {
 0136                    if (item.Type != JTokenType.String) ErrorFactory.ThrowCommonPropertyValueErrorError(_property);
 0137                    verifyTypeValueNotPresent(new JProperty("@type", item));
 138                }
 139            }
 140            else
 141            {
 1142                ErrorFactory.ThrowCommonPropertyValueErrorError(_property);
 143            }
 144
 1145        }
 146
 147        void verifyObjectDoesNotHaveProhibitedAtProperties(JObject commonProperty)
 148        {
 1149            string[] allowedFaxedValues = new string[] { "@value", "@type", "@language", "@id" };
 1150            foreach (var property in commonProperty.Properties())
 151            {
 1152                if (!allowedFaxedValues.Contains(property.Name) && property.Name.StartsWith("@"))
 1153                    ErrorFactory.ThrowCommonPropertyValueErrorError(_property);
 154            }
 1155        }
 156
 157    }
 158}