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

Data Annotation Extensions #69834

Closed
mottaghipour opened this issue May 25, 2022 · 2 comments
Closed

Data Annotation Extensions #69834

mottaghipour opened this issue May 25, 2022 · 2 comments

Comments

@mottaghipour
Copy link

Summary

To complete minimal validations(#30666) we should have some data annotations extensions.
I have an idea and I want to know if you agree with it to apply it?

Motivation and goals

The idea is to quickly validate objects

Examples

Validation extensions:

public static class DataAnnotationsExtensions
{
    public static Tuple<bool, ValidationResult> Validate(this object model) { ... }
    public static Tuple<bool, ICollection<ValidationResult>> ValidateAllProperties(this object model) { ... }
}

Minimal Apis:

app.MapPost("/todos", async (Todo todo) =>
{
    var (isValid, result) = todo.Validate();
    
    if (!isValid)
    {
        ...
    }
});

@halter73 @davidfowl
I think this idea is good but I don't know which project or folder for this methods.
If you are agree I implement this extensions and create pull request.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label May 26, 2022
@davidfowl davidfowl transferred this issue from dotnet/aspnetcore May 26, 2022
@davidfowl
Copy link
Member

These aren't tied to ASP.NET. This is a generic data annotations requests for recursive validation. Also extension methods on object are not great. I would prefer to see a static method here Validator class.

cc @DamianEdwards @ajcvickers

@mottaghipour
Copy link
Author

mottaghipour commented May 26, 2022

I agree with you, I have thought about this before.
If objects inherits from IValidatable, This is how we implement it:

public interface IValidatable
{
    Tuple<bool, ICollection<ValidationResult>> Validate();
}

We can implement abstarct class Validatable for defualt base validations, users can customize IValidatable for another validation packages, as the same way if user want use defualt validation with validation attrs:

public class TodoCommand : Validatable
{
    // [ValidationAttr]
    // Prop ...
}

I guess maybe the user wanted to use other packages for validation, they can customize them or even package development teams can create a way to customize it.

If we go this way, we can change the whole structure of requests and use IValidatable everywhere, for example ApiController can validate model with Validate method and object changes state IsValid This also avoids over-validation and finds a clear framework.
@davidfowl

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label May 26, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jun 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants