< Summary

Information
Class: MyNet.Observable.Validation.ValidationLocalization
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Validation/ValidationLocalization.cs
Tag: 323_28699572109
Line coverage
90%
Covered lines: 9
Uncovered lines: 1
Coverable lines: 10
Total lines: 58
Line coverage: 90%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Configure()100%22100%
UseValidationResourcesLanguageManager()100%210%
ResolveDisplayName(...)75%44100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Validation/ValidationLocalization.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ValidationLocalization.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Linq.Expressions;
 9using System.Reflection;
 10using System.Threading;
 11using FluentValidation;
 12using MyNet.Globalization.Facade;
 13using MyNet.Observable.Localization;
 14
 15namespace MyNet.Observable.Validation;
 16
 17/// <summary>
 18/// Configures FluentValidation to use <see cref="ValidationResources"/> and translated property names.
 19/// </summary>
 20public static class ValidationLocalization
 21{
 22    private static int _configured;
 23
 24    /// <summary>
 25    /// Replaces the global FluentValidation language manager and display name resolver.
 26    /// Safe to call once at application startup.
 27    /// </summary>
 28    public static void Configure()
 29    {
 630        if (Interlocked.Exchange(ref _configured, 1) == 1)
 331            return;
 32
 333        ValidatorOptions.Global.LanguageManager = new ValidationLanguageManager();
 334        ValidatorOptions.Global.DisplayNameResolver = ResolveDisplayName;
 335    }
 36
 37    /// <summary>
 38    /// Assigns <see cref="ValidationLanguageManager"/> without changing the display name resolver.
 39    /// </summary>
 40    public static void UseValidationResourcesLanguageManager()
 041        => ValidatorOptions.Global.LanguageManager = new ValidationLanguageManager();
 42
 43    /// <summary>
 44    /// Resolves property display names from the member name translation key.
 45    /// </summary>
 46    public static string? ResolveDisplayName(Type type, MemberInfo? member, LambdaExpression expression)
 47    {
 48        _ = type;
 49        _ = expression;
 50
 11451        if (member is null)
 352            return null;
 53
 11154        var translated = member.Name.Translate();
 11155        return string.IsNullOrWhiteSpace(translated) ? member.Name : translated;
 56    }
 57}
 58