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

Support interface-based validated parameter binding #13

Open
DamianEdwards opened this issue Sep 27, 2021 · 0 comments
Open

Support interface-based validated parameter binding #13

DamianEdwards opened this issue Sep 27, 2021 · 0 comments

Comments

@DamianEdwards
Copy link
Owner

Thanks to recent changes in rc.2 (dotnet/aspnetcore#36935), something like the following should now be possible:

app.MapPost("/todos", (Todo todo, TodoDb db) =>
{
    if (!todo.IsValid(out var errors))
    {
        return Result.ValidationProblem(errors);
    }
    db.Todos.Add(todo);
    await db.SaveChanges();
    return Result.Created($"/todos/{todo.Id}", todo);
});

public class Todo : IValidatable<Todo>
{
    [Required]
    public string? Title { get; set; }
}

namespace MiniValidation
{
    public interface IValidatable<TValue> where TValue : IValidatable<TValue>
    {
        public sealed IsValid(out IDictionary<string, string[]> errors)
        {
            var isValid = MiniValidator.TryValidate(this, out var errs);
            errors = errs;
            return isValid;
        }
    }
}
@DamianEdwards DamianEdwards transferred this issue from DamianEdwards/MiniValidation Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant