| | 1 | | using Newtonsoft.Json.Linq; |
| | 2 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | 3 | | using ValidateLib.ErrorsAndWarnings.Warnings; |
| | 4 | | using ValidateLib.Metadata.Properties; |
| | 5 | | using ValidateLib.UtilityClasses; |
| | 6 | |
|
| | 7 | | namespace ValidateLib.Metadata.Validators |
| | 8 | | { |
| | 9 | | public class CommonPropertyValidator : IMValidator<CommonProperty> |
| | 10 | | { |
| | 11 | | CommonProperty _property; |
| 1 | 12 | | public CommonPropertyValidator(CommonProperty property) |
| | 13 | | { |
| 1 | 14 | | _property = property; |
| 1 | 15 | | } |
| | 16 | | public List<Warning> Validate(CommonProperty commonProperty) |
| | 17 | | { |
| 1 | 18 | | var warnings = new List<Warning>(); |
| 1 | 19 | | if (commonProperty!._value!.Type == JTokenType.Object) |
| | 20 | | { |
| 1 | 21 | | var jToken = commonProperty._value; |
| 1 | 22 | | var jObject = (JObject)jToken!; |
| 1 | 23 | | if (jObject["@value"] is not null) |
| | 24 | | { |
| 1 | 25 | | VerifyCommonPropertyWithValueDoesNotHaveBothTypeAndLanguageProperty(jObject); |
| 1 | 26 | | VerifyCommonPropertyWithValueDoesNotHaveOtherProperties(jObject); |
| 1 | 27 | | verifyValueType(jObject); |
| 1 | 28 | | if (jObject["@type"] is not null) |
| 1 | 29 | | verifyTypeWithValuePresent(jObject.Property("@type")!); |
| 1 | 30 | | if (jObject["@language"] is not null) |
| 1 | 31 | | 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 |
| 1 | 37 | | if (jObject["@type"] is not null) |
| 1 | 38 | | verifyTypeValueNotPresent(jObject.Property("@type")!); |
| 1 | 39 | | if (jObject["@language"] is not null) |
| 1 | 40 | | ErrorFactory.ThrowCommonPropertyValueErrorError(commonProperty); |
| | 41 | | } |
| 1 | 42 | | verifyObjectDoesNotHaveProhibitedAtProperties(jObject); |
| | 43 | |
|
| | 44 | | } |
| 1 | 45 | | if (commonProperty._value.Type == JTokenType.Array) |
| | 46 | | { |
| 1 | 47 | | foreach (var item in commonProperty._value) |
| | 48 | | { |
| 1 | 49 | | if (item.Type != JTokenType.Object) |
| 0 | 50 | | ErrorFactory.ThrowCommonPropertyValueErrorError(commonProperty); |
| 1 | 51 | | Validate(new CommonProperty(null, item)); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | } |
| 1 | 55 | | return warnings; |
| | 56 | |
|
| | 57 | | } |
| | 58 | |
|
| | 59 | | void VerifyCommonPropertyWithValueDoesNotHaveBothTypeAndLanguageProperty(JObject commonPropertyValue) |
| | 60 | | { |
| | 61 | |
|
| 1 | 62 | | if (commonPropertyValue["@type"] is not null && commonPropertyValue["@language"] is not null) |
| 1 | 63 | | ErrorFactory.ThrowCommonPropertyValueErrorError(_property); |
| 1 | 64 | | } |
| | 65 | | void VerifyCommonPropertyWithValueDoesNotHaveOtherProperties(JObject commonPropertyValue) |
| | 66 | | { |
| 1 | 67 | | foreach (var property in commonPropertyValue.Properties()) |
| | 68 | | { |
| 1 | 69 | | if (property.Name != "@language" && property.Name != "@type" && property.Name != "@value") |
| 1 | 70 | | ErrorFactory.ThrowCommonPropertyValueErrorError(_property); |
| | 71 | | } |
| 1 | 72 | | } |
| | 73 | | void verifyValueType(JObject commonPropertyValue) |
| | 74 | | { |
| | 75 | |
|
| 1 | 76 | | var type = commonPropertyValue!["@value"]!.Type; |
| 1 | 77 | | if ( |
| 1 | 78 | | type != JTokenType.Object && |
| 1 | 79 | | type != JTokenType.String && |
| 1 | 80 | | type != JTokenType.Array && |
| 1 | 81 | | type != JTokenType.Date |
| 1 | 82 | | ) |
| 0 | 83 | | ErrorFactory.ThrowCPInvalidValueTypeError(commonPropertyValue); |
| 1 | 84 | | } |
| | 85 | |
|
| | 86 | | void verifyTypeWithValuePresent(JProperty typeProperty) |
| | 87 | | { |
| 1 | 88 | | if ( |
| 1 | 89 | | !DatatypeUtilityClass.IsBuiltInType(typeProperty.Value.ToString()) && |
| 1 | 90 | | !CommonPropertiesPrefixes.IsPrefixedName(typeProperty.Value.ToString()) && |
| 1 | 91 | | !IriUtilityClass.IsAbsoluteUrl(typeProperty.Value.ToString()) |
| 1 | 92 | | ) |
| 1 | 93 | | ErrorFactory.ThrowCommonPropertyValueErrorError(_property); |
| 1 | 94 | | } |
| | 95 | |
|
| | 96 | | void verifyLanguageValuePresent(JProperty languageProperty) |
| | 97 | | { |
| 1 | 98 | | if (languageProperty.Value.Type == JTokenType.String) |
| | 99 | | { |
| 1 | 100 | | if (!LanguageUtilityClass.IsCorrectLanguageCode(languageProperty.Value.ToString())) |
| 0 | 101 | | ErrorFactory.ThrowCommonPropertyValueErrorError(_property); |
| | 102 | | } |
| 1 | 103 | | else if (languageProperty.Value.Type == JTokenType.Null) return; |
| | 104 | | else |
| 1 | 105 | | ErrorFactory.ThrowCommonPropertyValueErrorError(_property); |
| 1 | 106 | | } |
| | 107 | |
|
| | 108 | | void verifyTypeValueNotPresent(JProperty typeProperty) |
| | 109 | | { |
| 1 | 110 | | if (typeProperty.Value.Type == JTokenType.String) |
| | 111 | | { |
| 1 | 112 | | string[] definedTypesCSVW = new string[] |
| 1 | 113 | | { |
| 1 | 114 | | "TableGroup", |
| 1 | 115 | | "Table", |
| 1 | 116 | | "Schema", |
| 1 | 117 | | "Column", |
| 1 | 118 | | "Dialect", |
| 1 | 119 | | "Template" |
| 1 | 120 | | }; |
| | 121 | |
|
| 1 | 122 | | if ( |
| 1 | 123 | |
|
| 1 | 124 | | !definedTypesCSVW.Contains(typeProperty.Value.ToString()) && |
| 1 | 125 | | !CommonPropertiesPrefixes.IsPrefixedName(typeProperty.Value.ToString()) && |
| 1 | 126 | | !IriUtilityClass.IsAbsoluteUrl(typeProperty.Value.ToString()) |
| 1 | 127 | | ) |
| 1 | 128 | | ErrorFactory.ThrowCommonPropertyValueErrorError(_property); |
| 1 | 129 | | if (typeProperty.Value.ToString().StartsWith("_:")) |
| 0 | 130 | | ErrorFactory.ThrowCommonPropertyValueErrorError(_property); |
| | 131 | | } |
| 1 | 132 | | else if (typeProperty.Value.Type == JTokenType.Array) |
| | 133 | | { |
| 0 | 134 | | foreach (var item in (JArray)typeProperty.Value) |
| | 135 | | { |
| 0 | 136 | | if (item.Type != JTokenType.String) ErrorFactory.ThrowCommonPropertyValueErrorError(_property); |
| 0 | 137 | | verifyTypeValueNotPresent(new JProperty("@type", item)); |
| | 138 | | } |
| | 139 | | } |
| | 140 | | else |
| | 141 | | { |
| 1 | 142 | | ErrorFactory.ThrowCommonPropertyValueErrorError(_property); |
| | 143 | | } |
| | 144 | |
|
| 1 | 145 | | } |
| | 146 | |
|
| | 147 | | void verifyObjectDoesNotHaveProhibitedAtProperties(JObject commonProperty) |
| | 148 | | { |
| 1 | 149 | | string[] allowedFaxedValues = new string[] { "@value", "@type", "@language", "@id" }; |
| 1 | 150 | | foreach (var property in commonProperty.Properties()) |
| | 151 | | { |
| 1 | 152 | | if (!allowedFaxedValues.Contains(property.Name) && property.Name.StartsWith("@")) |
| 1 | 153 | | ErrorFactory.ThrowCommonPropertyValueErrorError(_property); |
| | 154 | | } |
| 1 | 155 | | } |
| | 156 | |
|
| | 157 | | } |
| | 158 | | } |