Table of Contents

Class TlumachValidationAttribute

Namespace
Tlumach.DataAnnotations
Assembly
Tlumach.DataAnnotations.dll

The base class for a validation attribute of an application that takes its error message from a class created by Tlumach Generator.

Derive from this class when none of the localized attributes of Tlumach fits. A derived class obtains the message through MessageResolver, which is also the place to get the raw template from when the message takes more arguments than the display name.

public abstract class TlumachValidationAttribute : ValidationAttribute, ITlumachLocalizedErrorMessage
Inheritance
TlumachValidationAttribute
Implements
Inherited Members

Examples

The following attribute validates that a string is not longer than a fixed limit and takes its message from a translation:

public sealed class ShortTextAttribute : TlumachValidationAttribute
{
    public override bool IsValid(object? value) => value is not string text || text.Length <= 40;
}

// [ShortText(TranslationClass = typeof(Strings), TranslationKey = Strings.tooLongKey)]

Constructors

TlumachValidationAttribute()

Initializes a new instance of the TlumachValidationAttribute class. Set TranslationClass and TranslationKey in the annotation.

protected TlumachValidationAttribute()

TlumachValidationAttribute(Func<string>)

Initializes a new instance of the TlumachValidationAttribute class with a function that provides the error message, used when the annotation carries no Tlumach translation.

protected TlumachValidationAttribute(Func<string> errorMessageAccessor)

Parameters

errorMessageAccessor Func<string>

The function that provides the error message.

TlumachValidationAttribute(string)

Initializes a new instance of the TlumachValidationAttribute class with a literal error message, used when the annotation carries no Tlumach translation.

protected TlumachValidationAttribute(string errorMessage)

Parameters

errorMessage string

The literal error message.

TlumachValidationAttribute(Type, string)

Initializes a new instance of the TlumachValidationAttribute class that takes its message from the given translation.

protected TlumachValidationAttribute(Type translationClass, string translationKey)

Parameters

translationClass Type

The class created by Tlumach Generator, or a nested group class inside one, that provides the message.

translationKey string

The name of the translation unit member in translationClass, or the key of a translation entry.

Properties

CultureSource

Gets the culture, for which the message is read.

public TranslationCultureSource CultureSource { get; set; }

Property Value

TranslationCultureSource

MessageResolver

Gets the resolver that provides the localized message of this attribute.

protected TlumachMessageResolver MessageResolver { get; }

Property Value

TlumachMessageResolver

TranslationClass

Gets the class created by Tlumach Generator, or a nested group class inside one, that provides the message.

public Type? TranslationClass { get; set; }

Property Value

Type

TranslationKey

Gets the name of the translation unit member in TranslationClass, or the key of a translation entry.

public string? TranslationKey { get; set; }

Property Value

string

Methods

FormatErrorMessage(string)

Formats the error message, taking it from the configured translation when there is one.

public override string FormatErrorMessage(string name)

Parameters

name string

The display name of the member being validated.

Returns

string

The formatted message.

Exceptions

InvalidOperationException

Thrown when the Tlumach configuration of the attribute is invalid or the message cannot be resolved.