generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ValueValidationConfiguration.cs
35 lines (32 loc) · 1.9 KB
/
ValueValidationConfiguration.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
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Localization;
using System.Threading;
using System.Threading.Tasks;
namespace CoreEx.Validation
{
/// <summary>
/// Enables the ability to configure the <see cref="ValueValidator{T}"/>.
/// </summary>
public class ValueValidatorConfiguration<T> : PropertyRuleBase<ValidationValue<T>, T>
{
/// <summary>
/// Initializes a new instance of the <see cref="ValueValidatorConfiguration{T}"/> class.
/// </summary>
/// <param name="name">The property name.</param>
/// <param name="text">The friendly text name used in validation messages (defaults to <paramref name="name"/> as <see cref="Text.SentenceCase.ToSentenceCase(string)"/>).</param>
/// <param name="jsonName">The JSON property name (defaults to <paramref name="name"/>).</param>
internal ValueValidatorConfiguration(string name, LText? text = null, string? jsonName = null) : base(name, text, jsonName) { }
/// <summary>
/// Performs the underlying validation.
/// </summary>
/// <param name="validationValue">The <see cref="ValidationValue{T}"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The <see cref="ValueValidatorResult{TEntity, TProperty}"/>.</returns>
internal async Task<ValueValidatorResult<ValidationValue<T>, T>> ValidateAsync(ValidationValue<T> validationValue, CancellationToken cancellationToken)
{
var ctx = new PropertyContext<ValidationValue<T>, T>(new ValidationContext<ValidationValue<T>>(validationValue, new ValidationArgs()), validationValue.Value, Name, JsonName, Text);
await InvokeAsync(ctx, cancellationToken).ConfigureAwait(false);
return new ValueValidatorResult<ValidationValue<T>, T>(ctx);
}
}
}