generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
RuleSet.cs
40 lines (35 loc) · 1.73 KB
/
RuleSet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using System;
using System.Threading;
using System.Threading.Tasks;
namespace CoreEx.Validation
{
/// <summary>
/// Represents a validation rule set for an entity, in that it groups one or more <see cref="Rules"/> together for a specified condition.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
public class RuleSet<TEntity> : ValidatorBase<TEntity>, IEntityRule<TEntity> where TEntity : class
{
/// <summary>
/// Initializes a new instance of the <see cref="RuleSet{TEntity}"/> class to be invoked where the predicate is true.
/// </summary>
/// <param name="predicate">A function to determine whether the <see cref="RuleSet{TEntity}"/> is to be validated.</param>
internal RuleSet(Predicate<ValidationContext<TEntity>> predicate) => Predicate = predicate.ThrowIfNull(nameof(predicate));
/// <summary>
/// Gets the function to determine whether the <see cref="RuleSet{TEntity}"/> is to be validated.
/// </summary>
public Predicate<ValidationContext<TEntity>> Predicate { get; private set; }
/// <inheritdoc/>
public async Task ValidateAsync(ValidationContext<TEntity> context, CancellationToken cancellationToken = default)
{
// Check the condition before continuing to validate the underlying rules.
if (!Predicate(context))
return;
// Validate each of the property rules.
foreach (var rule in Rules)
{
await rule.ValidateAsync(context, cancellationToken).ConfigureAwait(false);
}
}
}
}