Class TlumachRuleBuilderExtensions
- Namespace
- Tlumach.FluentValidation
- Assembly
- Tlumach.FluentValidation.dll
Sets the message and the display name of a rule from a Tlumach translation. The text is read when the rule is validated, so it follows the culture of that moment.
public static class TlumachRuleBuilderExtensions
- Inheritance
-
TlumachRuleBuilderExtensions
- Inherited Members
Remarks
Import the namespace Tlumach.FluentValidation wherever these methods are used. TranslationUnit converts implicitly to string, so without
the import, WithMessage(Strings.X) compiles against WithMessage(string) of FluentValidation and keeps the text of the culture that was current when the
validator was created.
The text is read as a template, without the placeholder engine of Tlumach, and FluentValidation then replaces its placeholders, such as {PropertyName}.
A key without text for the culture raises an InvalidOperationException during validation.
Methods
WithMessage<T, TProperty>(IRuleBuilderOptions<T, TProperty>, BaseTranslationUnit, Action<T, TProperty, MessageFormatter>)
Takes the message of the rule from a translation unit and fills placeholders of its own before FluentValidation fills the standard ones.
Values are inserted before FluentValidation formats the message, so a value that itself contains a placeholder of FluentValidation, such as
{PropertyName}, has that placeholder replaced too.
public static IRuleBuilderOptions<T, TProperty> WithMessage<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, BaseTranslationUnit unit, Action<T, TProperty, MessageFormatter> placeholders)
Parameters
ruleIRuleBuilderOptions<T, TProperty>The rule.
unitBaseTranslationUnitThe translation unit with the message template.
placeholdersAction<T, TProperty, MessageFormatter>A callback that adds values with AppendArgument(string, object). Format specifiers, as in
{Limit:N0}, are applied with CurrentCulture (FluentValidation formats without a provider), which may differ from the culture of the message.
Returns
- IRuleBuilderOptions<T, TProperty>
The rule, with the message taken from the unit and filled with the values of the callback.
Type Parameters
TThe type of the validated object.
TPropertyThe type of the validated property.
WithMessage<T, TProperty>(IRuleBuilderOptions<T, TProperty>, TranslationManager, string)
Takes the message of the rule from a translation manager by the key of the translation entry, which is the route for a class generated with onlyDeclareKeys.
public static IRuleBuilderOptions<T, TProperty> WithMessage<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, TranslationManager manager, string key)
Parameters
ruleIRuleBuilderOptions<T, TProperty>The rule.
managerTranslationManagerThe translation manager.
keystringThe full key, including its groups, for example
Messages.NameTooLong.
Returns
- IRuleBuilderOptions<T, TProperty>
The rule, with the message taken from the manager.
Type Parameters
TThe type of the validated object.
TPropertyThe type of the validated property.
WithMessage<T, TProperty>(IRuleBuilderOptions<T, TProperty>, TranslationManager, string, Action<T, TProperty, MessageFormatter>)
Takes the message of the rule from a translation manager by key and fills placeholders of its own before FluentValidation fills the standard ones.
public static IRuleBuilderOptions<T, TProperty> WithMessage<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, TranslationManager manager, string key, Action<T, TProperty, MessageFormatter> placeholders)
Parameters
ruleIRuleBuilderOptions<T, TProperty>The rule.
managerTranslationManagerThe translation manager.
keystringThe full key, including its groups.
placeholdersAction<T, TProperty, MessageFormatter>A callback that adds values with AppendArgument(string, object). Format specifiers are applied with CurrentCulture, which may differ from the culture of the message.
Returns
- IRuleBuilderOptions<T, TProperty>
The rule, with the message taken from the manager and filled with the values of the callback.
Type Parameters
TThe type of the validated object.
TPropertyThe type of the validated property.
WithMessage<T, TProperty, TUnit>(IRuleBuilderOptions<T, TProperty>, TUnit)
Takes the message of the rule from a translation unit, typically one created by Generator.
public static IRuleBuilderOptions<T, TProperty> WithMessage<T, TProperty, TUnit>(this IRuleBuilderOptions<T, TProperty> rule, TUnit unit) where TUnit : BaseTranslationUnit
Parameters
ruleIRuleBuilderOptions<T, TProperty>The rule.
unitTUnitThe translation unit with the message template.
Returns
- IRuleBuilderOptions<T, TProperty>
The rule, with the message taken from the unit.
Type Parameters
TThe type of the validated object.
TPropertyThe type of the validated property.
TUnitThe class of the translation unit.
Remarks
The method is generic on purpose. Every unit class, including the platform ones (Avalonia, WinUI, UWP) that declare their own implicit conversion to string,
binds to it with an identity conversion of the argument, which beats the user-defined conversion that would select WithMessage(string) of FluentValidation
or make the call ambiguous. A string argument does not satisfy the constraint, so this overload leaves it to FluentValidation.
WithName<T, TProperty>(IRuleBuilderOptions<T, TProperty>, TranslationManager, string)
Takes the display name of the property from a translation manager by the key of the translation entry.
public static IRuleBuilderOptions<T, TProperty> WithName<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, TranslationManager manager, string key)
Parameters
ruleIRuleBuilderOptions<T, TProperty>The rule.
managerTranslationManagerThe translation manager.
keystringThe full key, including its groups.
Returns
- IRuleBuilderOptions<T, TProperty>
The rule, with the display name taken from the manager.
Type Parameters
TThe type of the validated object.
TPropertyThe type of the validated property.
WithName<T, TProperty, TUnit>(IRuleBuilderOptions<T, TProperty>, TUnit)
Takes the display name of the property, which becomes {PropertyName} in the message, from a translation unit.
public static IRuleBuilderOptions<T, TProperty> WithName<T, TProperty, TUnit>(this IRuleBuilderOptions<T, TProperty> rule, TUnit unit) where TUnit : BaseTranslationUnit
Parameters
ruleIRuleBuilderOptions<T, TProperty>The rule.
unitTUnitThe translation unit with the display name.
Returns
- IRuleBuilderOptions<T, TProperty>
The rule, with the display name taken from the unit.
Type Parameters
TThe type of the validated object.
TPropertyThe type of the validated property.
TUnitThe class of the translation unit.