| | 1 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | 2 | | using ValidateLib.ErrorsAndWarnings.Warnings; |
| | 3 | | using ValidateLib.Metadata.Descriptors; |
| | 4 | | using ValidateLib.Metadata.Properties; |
| | 5 | | using ValidateLib.UtilityClasses; |
| | 6 | |
|
| | 7 | | namespace ValidateLib.Metadata.Validators |
| | 8 | | { |
| | 9 | | public class TitlesValidator : IMValidator<TableGroupDescriptor>, IMValidator<TableDescriptor> |
| | 10 | | { |
| | 11 | | public List<Warning> Validate(TableGroupDescriptor tableGroupDescriptor) |
| | 12 | | { |
| 1 | 13 | | List<Warning> warnings = new List<Warning>(); |
| | 14 | |
|
| 1 | 15 | | if (tableGroupDescriptor is not null && tableGroupDescriptor.tables is not null) |
| | 16 | | { |
| 1 | 17 | | foreach (var table in tableGroupDescriptor.tables._value!) |
| | 18 | | { |
| 1 | 19 | | if (table.tableSchema is not null) |
| | 20 | | { |
| 1 | 21 | | ProcessOneScheme(table.tableSchema._value!); |
| | 22 | | } |
| | 23 | | } |
| | 24 | | } |
| | 25 | |
|
| 1 | 26 | | return warnings; |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public List<Warning> Validate(TableDescriptor table) |
| | 30 | | { |
| 1 | 31 | | List<Warning> warnings = new List<Warning>(); |
| 1 | 32 | | if (table.tableSchema is not null) |
| | 33 | | { |
| 1 | 34 | | ProcessOneScheme(table.tableSchema._value!); |
| | 35 | | } |
| 1 | 36 | | return warnings; |
| | 37 | | } |
| | 38 | |
|
| | 39 | | void ProcessOneScheme(SchemaDescriptor schemaDescriptor) |
| | 40 | | { |
| 1 | 41 | | if (AreDesiredPropertiesPresent(schemaDescriptor)) |
| | 42 | | { |
| 1 | 43 | | foreach (var column in schemaDescriptor!.columns!._value!) |
| | 44 | | { |
| 1 | 45 | | if (column.titles is not null) |
| | 46 | | { |
| 1 | 47 | | if ( |
| 1 | 48 | | !IsLangCompatibleWithTitle( |
| 1 | 49 | | LanguageUtilityClass.GetMostGeneralLanguagePart(schemaDescriptor.lang!._value!) |
| 1 | 50 | | , column.titles) |
| 1 | 51 | | ) |
| | 52 | | { |
| 1 | 53 | | ErrorFactory.ThrowTitleNotCompatibleWithLangError(column.titles._value!.FirstOrDefault().Val |
| | 54 | | } |
| | 55 | | } |
| | 56 | | } |
| | 57 | | } |
| 1 | 58 | | } |
| | 59 | | bool AreDesiredPropertiesPresent(SchemaDescriptor schemaDescriptor) |
| | 60 | | { |
| 1 | 61 | | return schemaDescriptor is not null && |
| 1 | 62 | | schemaDescriptor.lang is not null && |
| 1 | 63 | | schemaDescriptor.columns is not null && |
| 1 | 64 | | schemaDescriptor.columns._value is not null |
| 1 | 65 | | ; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | bool IsLangCompatibleWithTitle(string lang, NaturalLanguageProperty titles) |
| | 69 | | { |
| 1 | 70 | | if (lang == MetadataConstants.DefaultLang) |
| 1 | 71 | | return true; |
| 1 | 72 | | foreach (var kvp in titles._value!) |
| | 73 | | { |
| 1 | 74 | | var generalKeyPart = LanguageUtilityClass.GetMostGeneralLanguagePart(kvp.Key); |
| 1 | 75 | | if (generalKeyPart == lang || kvp.Key == MetadataConstants.DefaultLang) |
| | 76 | | { |
| 1 | 77 | | return true; |
| | 78 | | } |
| | 79 | | } |
| | 80 | |
|
| 1 | 81 | | return false; |
| 1 | 82 | | } |
| | 83 | | } |
| | 84 | | } |