| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="BoundedValueValidator.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Numerics; |
| | | 8 | | using FluentValidation; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Observable.Validation.Validators; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// FluentValidation rules for <see cref="BoundedValue{T}"/>. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <typeparam name="T">The numeric type.</typeparam> |
| | | 16 | | public sealed class BoundedValueValidator<T> : AbstractValidator<BoundedValue<T>> |
| | | 17 | | where T : struct, INumber<T> |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="BoundedValueValidator{T}"/> class. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="propertyDisplayKey">Translation key used as the field name in messages.</param> |
| | 66 | 23 | | public BoundedValueValidator(string propertyDisplayKey) => RuleFor(x => x.Value) |
| | 33 | 24 | | .Must((model, value) => model.Range.Contains(value!.Value)) |
| | 33 | 25 | | .When(x => x.Value.HasValue) |
| | 33 | 26 | | .WithMessage((model, _) => model.BuildRangeValidationMessage(propertyDisplayKey)); |
| | | 27 | | } |
| | | 28 | | |