Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adding failures programmatically #243

Open
ramduq opened this issue Jan 7, 2025 · 4 comments
Open

Allow adding failures programmatically #243

ramduq opened this issue Jan 7, 2025 · 4 comments
Labels
Feature Request Request to add a new feature Triage Issue needs to be triaged

Comments

@ramduq
Copy link

ramduq commented Jan 7, 2025

FluentValidationValidator does not allow adding failures to the failure collection. This is needed in the following scenario:

Scenario
Client is a Blazor web assembly app. Server is an API project. Both perform validations using fluent validation. Thus, an API request from the client might get a response containing a List<ValidationFailure>. If it were possible to add these (backend) failures to the current collection, the UI would be updated accordingly (e.g. coloring the related fields in red).

Proposed solution

A new method

AddFailures(Dictionary<FieldIdentifier, List<ValidationFailure> failures)

** Alternatives considered**
I considered extending the class FluentValidationValidator in my project. However LastValidationResult (the dictionary of failures) is an internal field, which prevents accessing it from external projects.

An alternative solution would be to make this field protected, making it possible to manipulate this collection in derived classes.

@ramduq ramduq added Feature Request Request to add a new feature Triage Issue needs to be triaged labels Jan 7, 2025
@it4factory
Copy link

Upvote :)
Im actually trying to find out how to do exactly the same thing - display validation errors received from the server on WASM client form.

@Albiniivari
Copy link

You can add it to the EditContext messagestore, but you have to create a FieldIdentifier from the backend. It's a bit hacky when you get to an error in a collection. Cause then the backend would return something like "Key: Model.People[2].Name" which doesn't directly work as a fieldidentifier. If it's a simple property (not collection), you can do something like this

fieldIdentifier = new FieldIdentifier(myModel, err.Key); messageStore.Add(fieldIdentifier, err.Value);

@MarredCheese
Copy link

Cause then the backend would return something like something like "Key: Model.People[2].Name" which doesn't directly work as a fieldidentifier.

This repo already has the ability to translate paths to field identifiers:

private static FieldIdentifier ToFieldIdentifier(in EditContext editContext, in string propertyPath)

And it already uses that to copy the fluent validation results into the message store:

foreach (var validationResult in validationResults.Errors)
{
var fieldIdentifier = ToFieldIdentifier(editContext, validationResult.PropertyName);
messages.Add(fieldIdentifier, validationResult.ErrorMessage);

So a method for manually adding backend errors could have a friendlier parameter type that doesn't involve FieldIdentifier. How about IDictionary<string, string[]> (mapping paths to error message lists)? It's the same type returned by ValidationResult.ToDictionary() and the same type as ValidationProblemDetails.Errors, which you'd likely have available in your server response.

@Albiniivari
Copy link

foreach (var validationResult in validationResults.Errors)
{
var fieldIdentifier = ToFieldIdentifier(editContext, validationResult.PropertyName);
messages.Add(fieldIdentifier, validationResult.ErrorMessage);

So a method for manually adding backend errors could have a friendlier parameter type that doesn't involve FieldIdentifier. How about IDictionary<string, string[]> (mapping paths to error message lists)? It's the same type returned by ValidationResult.ToDictionary() and the same type as ValidationProblemDetails.Errors, which you'd likely have available in your server response.

Yes that's exactly what I'd like. But when I try to add paths with collections to the errors, it doesn't correctly connect to the correct field. If I do it with a simple property such as "Name", then messages.Add(FieldIdentifier.Create(() => model.Name), validationResult.ErrorMessage); works correctly. But If it's a collection property such as "Addresses", so the path looks like "Model.Addresses[2].Street", then this doesn't work. (At least for me.)

Perhaps you have another way of doing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request Request to add a new feature Triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

4 participants