| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="AbstractValidatorLocalizedExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Linq.Expressions; |
| | | 9 | | using FluentValidation; |
| | | 10 | | using MyNet.Observable.Validation; |
| | | 11 | | using MyNet.Primitives; |
| | | 12 | | |
| | | 13 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 14 | | namespace MyNet.Observable; |
| | | 15 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Starts a localized validation rule from a property expression. |
| | | 19 | | /// </summary> |
| | | 20 | | public static class AbstractValidatorLocalizedExtensions |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Begins a rule for <paramref name="expression"/> with the property display key inferred from the member name. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="validator">The validator instance.</param> |
| | | 26 | | /// <param name="expression">The property expression (e.g. <c>x => x.Email</c>).</param> |
| | | 27 | | /// <param name="propertyDisplayKey">Optional translation key when it differs from the member name.</param> |
| | | 28 | | public static LocalizedRuleBuilder<T, TProperty> RuleForLocalized<T, TProperty>( |
| | | 29 | | this AbstractValidator<T> validator, |
| | | 30 | | Expression<Func<T, TProperty>> expression, |
| | | 31 | | string? propertyDisplayKey = null) |
| | | 32 | | { |
| | 30 | 33 | | ArgumentNullException.ThrowIfNull(validator); |
| | 30 | 34 | | ArgumentNullException.ThrowIfNull(expression); |
| | | 35 | | |
| | 30 | 36 | | var key = propertyDisplayKey ?? expression.PropertyName(); |
| | 30 | 37 | | return new(validator.RuleFor(expression), key); |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |