Skip to content

Clarification needed on "Writing custom validation" article, regarding async void event handlers #60

@ShahryarSaljoughi

Description

@ShahryarSaljoughi

I read your article titled "Writing custom validation" and ran into some issues while learning it. I'd appreciate if you could clarify this.

One - Fact:

In the article, the methods passed to EditContext.OnValidationRequested and EditContext.OhFieldChanged both are async void methods:

async void ValidationRequested(object sender, ValidationRequestedEventArgs args)
{
    ValidationMessageStore.Clear();
    var validationContext =
        new ValidationContext<object>(EditContext.Model);
    ValidationResult result =
        await Validator.ValidateAsync(validationContext);
    AddValidationResult(EditContext.Model, result);
}

Two - Fact:

EditContext.Validate() is not asynchronous and won't await the async void delegates passed to EditContext.OnValidationRequested:

    public bool Validate()
    {
        OnValidationRequested?.Invoke(this, ValidationRequestedEventArgs.Empty);
        return !GetValidationMessages().Any();
    }

Three - Problem:

I think, given the setup described, we won't have fluent validations occur by calling EditContext.Validate(), as the returned value simply indicates whether there are validation errors or not, and on the other hand there are high chances that validation errors will be added to the message stores after the Validate() has returned its value.

Am I missing anything? If the issue I described is valid, the FluentValidationValidator developed in this article can become a source of bugs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions