< Summary

Information
Class: Fluorite.Strainer.Services.Validation.SortExpressionValidator
Assembly: Fluorite.Strainer
File(s): /builds/fluorite/strainer/src/Strainer/Services/Validation/SortExpressionValidator.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 47
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Validate(...)100%66100%

File(s)

/builds/fluorite/strainer/src/Strainer/Services/Validation/SortExpressionValidator.cs

#LineLine coverage
 1using Fluorite.Strainer.Exceptions;
 2using Fluorite.Strainer.Models.Metadata;
 3
 4namespace Fluorite.Strainer.Services.Validation;
 5
 6public class SortExpressionValidator : ISortExpressionValidator
 7{
 8    public void Validate(IReadOnlyDictionary<Type, IReadOnlyDictionary<string, IPropertyMetadata>> propertyMetadata)
 9    {
 1110        Guard.Against.Null(propertyMetadata);
 11
 2212        foreach (var typeKeyPair in propertyMetadata)
 13        {
 214            var type = typeKeyPair.Key;
 215            var properties = typeKeyPair.Value.Values;
 516            var defaultSortingProperties = properties.Where(x => x.IsDefaultSorting).ToList();
 17
 218            if (!defaultSortingProperties.Any())
 19            {
 120                var exceptionMessage =
 121                    $"No default sort expression found for type {type.FullName}.\n" +
 122                    $"Mark a property as default sorting to enable fallbacking " +
 123                    $"to it when no sorting information is provided.";
 24
 125                throw new StrainerSortExpressionValidatorException(
 126                    type,
 127                    exceptionMessage);
 28            }
 29
 130            if (defaultSortingProperties.Count > 1)
 31            {
 132                var defaultProperties = string.Join(
 133                    Environment.NewLine,
 334                    defaultSortingProperties.Select(x => x.Name));
 135                var exceptionMessage =
 136                    $"Too many default sort properties found for type {type.FullName}.\n" +
 137                    $"Only one property can be marked as default.\n" +
 138                    $"Default properties:\n" +
 139                    $"{defaultProperties}";
 40
 141                throw new StrainerSortExpressionValidatorException(
 142                    type,
 143                    exceptionMessage);
 44            }
 45        }
 846    }
 47}