| | 1 | | namespace Pozitron.QuerySpecification; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Validates specifications. |
| | 5 | | /// </summary> |
| | 6 | | public class SpecificationValidator |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Gets the default instance of the <see cref="SpecificationValidator"/> class. |
| | 10 | | /// </summary> |
| 1 | 11 | | public static SpecificationValidator Default = new(); |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Gets the list of validators. |
| | 15 | | /// </summary> |
| 8 | 16 | | protected List<IValidator> Validators { get; } |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Initializes a new instance of the <see cref="SpecificationValidator"/> class. |
| | 20 | | /// </summary> |
| 2 | 21 | | public SpecificationValidator() |
| | 22 | | { |
| 2 | 23 | | Validators = |
| 2 | 24 | | [ |
| 2 | 25 | | WhereValidator.Instance, |
| 2 | 26 | | LikeValidator.Instance |
| 2 | 27 | | ]; |
| 2 | 28 | | } |
| | 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> |
| 1 | 34 | | public SpecificationValidator(IEnumerable<IValidator> validators) |
| | 35 | | { |
| 1 | 36 | | Validators = validators.ToList(); |
| 1 | 37 | | } |
| | 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 | | { |
| 10 | 48 | | if (specification.IsEmpty) return true; |
| | 49 | |
|
| 30 | 50 | | foreach (var validator in Validators) |
| | 51 | | { |
| 10 | 52 | | if (validator.IsValid(entity, specification) == false) |
| 2 | 53 | | return false; |
| | 54 | | } |
| | 55 | |
|
| 4 | 56 | | return true; |
| 2 | 57 | | } |
| | 58 | | } |