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

[Feature] There is no way to pass a ValidationContext to an ObservableValidator #3729

Closed
mdelanno opened this issue Feb 4, 2021 · 5 comments · Fixed by #3562
Closed

[Feature] There is no way to pass a ValidationContext to an ObservableValidator #3729

mdelanno opened this issue Feb 4, 2021 · 5 comments · Fixed by #3562
Labels
feature request 📬 A request for new changes to improve functionality open discussion ☎️

Comments

@mdelanno
Copy link

mdelanno commented Feb 4, 2021

Describe the problem this feature would solve

I've written a custom validator that I have added to a property via the CustomValidation attribute. This custom validator needs a service and also use ValidationContext.Items dictionary to pass some flags. But I can not tell the ObservableValidator to use my custom context because it creates a new object when it validates the object.

Describe the solution

Add a way to pass a ValidationContext to the ObservableValidator

@mdelanno mdelanno added the feature request 📬 A request for new changes to improve functionality label Feb 4, 2021
@ghost
Copy link

ghost commented Feb 4, 2021

Hello, 'mdelanno! Thanks for submitting a new feature request. I've automatically added a vote 👍 reaction to help get things started. Other community members can vote to help us prioritize this feature in the future!

@Kyaa-dost
Copy link
Contributor

Thanks for the suggestion @mdelanno! Going to open this up for discussion to get further insight from the community members.

@michael-hawker
Copy link
Member

FYI @Sergio0694

@Sergio0694
Copy link
Member

Hi @mdelanno - thank you for the suggestion!
This seems like a perfectly fair request to me, and it makes sense to make the class more flexible for all kinds of scenarios.

I'm thinking a solution could be to add a second constructor to the class, to basically have something like this:

namespace Microsoft.Toolkit.Mvvm.ComponentModel
{
    public abstract class ObservableValidator
    {
        public ObservableValidator();
        protected ObservableValidator(ValidationContext context);
    }
}

The parameterless constructor would just initialize a default ValidationContext capturing the current instance, which effectively replicates the current behavior for users that don't need this extra functionality. Whereas inheriting types like the ones you mentioned would be able to construct arbitrary validation contexts to inject through whatever method they prefer, and those would be used during validation instead. I have a few questions:

  1. Would this solution solve the issue in your case?
  2. Is changing valaidation context after instantiation something that is ever useful/needed, or is it fine to use the approach mentioned before where consumers would just be able to customize it when a new instance is created? The docs don't really detail this so having more feedbacks from a developer with more experience using this interface in the past is welcome 😄

Let me know what you think!

@mdelanno
Copy link
Author

mdelanno commented Feb 8, 2021

This would be perfect. I don't think changing the context after instantiation is needed but to be totally transparent, this is the first time I use this interface...

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature request 📬 A request for new changes to improve functionality open discussion ☎️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants