RunicValidationOptions class

Namespace Runic.Application.Views in Runic.Application.Views 0.23.0

public sealed class RunicValidationOptions

How the Bridge writes a ViewModel's validation. Register one in the application's services (services.AddSingleton(new RunicValidationOptions { … })); the Desktop, WPF, CS-WebUI and test hosts give it to each window they open, or set WindowContentSessionOptions.Validation.

Constructors

RunicValidationOptions

public RunicValidationOptions()

No documentation.

Properties

TextFor

public Func<ValidationAttribute, RunicValidationMember, RunicTextReference?>? TextFor { get; init; }

Gives the message of a ValidationAttribute, such as [Required], [MaxLength] or [Range], a text reference that the page shows in the window's language, or returns null to show the message as it is (feature translation-text/1). The message stays the error's fallback.

Remarks

The Bridge calls it for a validation error of a bridged member that has no text reference of its own: a ValidationResult or a string of INotifyDataErrorInfo.GetErrors for the member, as CommunityToolkit's ObservableValidator and a hand-written Validator call report them. It finds the attribute by evaluating the member's attributes against its current value, as Validator does ([Required] first, then in declared order), and pairs the member's errors with the failing attributes in order when their counts match, so an error formatted in another culture, such as one kept from before a window language switch, keeps its attribute. Otherwise, or when the error is another attribute's message, the message decides: ValidationAttribute.FormatErrorMessage of the member's display name ([Display(Name)], [DisplayName] or the property name) in the current culture. A message that a ViewModel writes itself while the member's attributes pass stays as it is. The attributes are evaluated on fresh instances, with RangeAttribute string limits parsed in the invariant culture.

It runs while the Bridge writes the state, on the window's model context: keep it, and the attributes, fast and free of side effects. An exception it throws is logged (event 1176), and the error shows its message; an attribute that cannot be evaluated leaves its member's errors without a text (event 1177, once per member).

services.AddSingleton(new RunicValidationOptions
{
    TextFor = (attribute, member) => attribute switch
    {
        RequiredAttribute => Text(AppText.Required(member.DisplayName)),
        MaxLengthAttribute maximum => Text(AppText.TooLong(member.DisplayName, maximum.Length)),
        RangeAttribute range => Text(AppText.OutOfRange(member.DisplayName, $"{range.Minimum}", $"{range.Maximum}")),
        _ => null,
    },
});

static RunicTextReference Text(TranslationReference reference) =>
    RunicTextReference.Create(reference, TranslationReferenceJsonContext.Default.TranslationReference);