< Summary

Information
Class: Pozitron.QuerySpecification.SpecificationValidator
Assembly: Pozitron.QuerySpecification
File(s): /home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification/Validators/SpecificationValidator.cs
Tag: 52_11740816328
Line coverage
100%
Covered lines: 18
Uncovered lines: 0
Coverable lines: 18
Total lines: 58
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
.cctor()100%11100%
get_Validators()100%11100%
.ctor()100%11100%
.ctor(...)100%11100%
IsValid(...)100%66100%

File(s)

/home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification/Validators/SpecificationValidator.cs

#LineLine coverage
 1namespace Pozitron.QuerySpecification;
 2
 3/// <summary>
 4/// Validates specifications.
 5/// </summary>
 6public class SpecificationValidator
 7{
 8    /// <summary>
 9    /// Gets the default instance of the <see cref="SpecificationValidator"/> class.
 10    /// </summary>
 111    public static SpecificationValidator Default = new();
 12
 13    /// <summary>
 14    /// Gets the list of validators.
 15    /// </summary>
 816    protected List<IValidator> Validators { get; }
 17
 18    /// <summary>
 19    /// Initializes a new instance of the <see cref="SpecificationValidator"/> class.
 20    /// </summary>
 221    public SpecificationValidator()
 22    {
 223        Validators =
 224        [
 225            WhereValidator.Instance,
 226            LikeValidator.Instance
 227        ];
 228    }
 29
 30    /// <summary>
 31    /// Initializes a new instance of the <see cref="SpecificationValidator"/> class with the specified validators.
 32    /// </summary>
 33    /// <param name="validators">The validators to use.</param>
 134    public SpecificationValidator(IEnumerable<IValidator> validators)
 35    {
 136        Validators = validators.ToList();
 137    }
 38
 39    /// <summary>
 40    /// Determines whether the specified entity is valid according to the given specification.
 41    /// </summary>
 42    /// <typeparam name="T">The type of the entity.</typeparam>
 43    /// <param name="entity">The entity to validate.</param>
 44    /// <param name="specification">The specification to evaluate.</param>
 45    /// <returns>true if the entity is valid; otherwise, false.</returns>
 46    public virtual bool IsValid<T>(T entity, Specification<T> specification)
 47    {
 1048        if (specification.IsEmpty) return true;
 49
 3050        foreach (var validator in Validators)
 51        {
 1052            if (validator.IsValid(entity, specification) == false)
 253                return false;
 54        }
 55
 456        return true;
 257    }
 58}