| | 1 | | using Fluorite.Strainer.Collections; |
| | 2 | | using Fluorite.Strainer.Models.Filtering.Operators; |
| | 3 | | using Fluorite.Strainer.Services.Filtering; |
| | 4 | |
|
| | 5 | | namespace Fluorite.Strainer.Services.Validation; |
| | 6 | |
|
| | 7 | | public class FilterOperatorValidator : IFilterOperatorValidator |
| | 8 | | { |
| 18 | 9 | | public FilterOperatorValidator() |
| | 10 | | { |
| | 11 | |
|
| 18 | 12 | | } |
| | 13 | |
|
| | 14 | | public void Validate(IFilterOperator filterOperator) |
| | 15 | | { |
| 168 | 16 | | Guard.Against.Null(filterOperator); |
| | 17 | |
|
| 168 | 18 | | if (string.IsNullOrWhiteSpace(filterOperator.Symbol)) |
| | 19 | | { |
| 3 | 20 | | throw new InvalidOperationException( |
| 3 | 21 | | $"{nameof(IFilterOperator.Symbol)} for filter operator " + |
| 3 | 22 | | $"\"{filterOperator}\" cannot be null, empty or contain " + |
| 3 | 23 | | $"only whitespace characters."); |
| | 24 | | } |
| | 25 | |
|
| 165 | 26 | | if (filterOperator.ExpressionProvider == null) |
| | 27 | | { |
| 1 | 28 | | throw new InvalidOperationException( |
| 1 | 29 | | $"{nameof(IFilterOperator.ExpressionProvider)} for filter operator " + |
| 1 | 30 | | $"\"{filterOperator}\" cannot be null."); |
| | 31 | | } |
| 164 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Validate(IEnumerable<IFilterOperator> filterOperators, IReadOnlySet<string> excludedBuiltInFilterOperato |
| | 35 | | { |
| 11 | 36 | | Guard.Against.Null(filterOperators); |
| | 37 | |
|
| 11 | 38 | | if (!filterOperators.Any()) |
| | 39 | | { |
| 1 | 40 | | return; |
| | 41 | | } |
| | 42 | |
|
| 345 | 43 | | foreach (var @operator in filterOperators) |
| | 44 | | { |
| 163 | 45 | | Validate(@operator); |
| | 46 | |
|
| 163 | 47 | | if (excludedBuiltInFilterOperators.Contains(@operator.Symbol) |
| 163 | 48 | | && FilterOperatorMapper.DefaultOperators.TryGetValue(@operator.Symbol, out var builtInOperator) |
| 163 | 49 | | && ReferenceEquals(builtInOperator, @operator)) |
| | 50 | | { |
| 1 | 51 | | throw new InvalidOperationException( |
| 1 | 52 | | $"Excluded built-in filter operator symbol {@operator.Symbol} detected in configuration values. " + |
| 1 | 53 | | $"Please ensure that no such filter operators are supplied when marking them as excluded."); |
| | 54 | | } |
| | 55 | | } |
| | 56 | |
|
| 9 | 57 | | var duplicatedSymbols = filterOperators |
| 162 | 58 | | .GroupBy(f => f.Symbol) |
| 170 | 59 | | .FirstOrDefault(g => g.Count() > 1); |
| 9 | 60 | | if (duplicatedSymbols != null) |
| | 61 | | { |
| 1 | 62 | | throw new InvalidOperationException( |
| 1 | 63 | | $"More then one filter operator found with the same" + |
| 1 | 64 | | $"symbol: \"{duplicatedSymbols.Key}\". " + |
| 1 | 65 | | $"Symbol representing filter operator must be unique. " + |
| 1 | 66 | | $"Please remove or change symbol for either of operators. " + |
| 1 | 67 | | $"Conflicting filter operators:\n" + |
| 3 | 68 | | $"{string.Join(Environment.NewLine, duplicatedSymbols.Select(f => f.ToString()))}"); |
| | 69 | | } |
| 8 | 70 | | } |
| | 71 | | } |