Aggregate Validate ValidationResults with Property-validated ValidationResults
When MiniValidator is performing validation on an IValidatableObject, it seems like it partitions the validation into two halves. The property based validation is done first, and then the IValidatableObject Validate implementation is done second. If there are any errors in the first half, those are returned early and the 2nd half of validation is not ran.
I would like for all errors to be returned back if possible. Is there a workaround that I am missing?
// MyContract.cs
public sealed record MyContract(
[property:Required(ErrorMessage = "An ID must be provided.")]
string Id,
[property:Required(ErrorMessage = "A Favorite Number must be provided.")]
int FavoriteNumber)
: IValidatableObject
{
public IEnumerable<ValidationResult> Validate(ValidationContext context)
{
if (FavoriteNumber > 99)
yield return new("3 digit numbers are not allowed!", new[] { nameof(FavoriteNumber) });
}
};
// Program.cs
app.MapPost("/my-endpoint", ([FromBody] MyContract contract) =>
!MiniValidator.TryValidate(contract, out var errors)
? Results.ValidationProblem(errors)
: Results.NoContent());
### Request
POST http://localhost:5000/my-endpoint
content-type: application/json
{
"id": " ",
"favoriteNumber": 101,
}
### Response
{
"type": "https://yada.yada/bing/bong",
"title": "One or more validation errors occurred.",
"status": 400,
"errors": {
"Id": [
"An ID must be provided."
]
}
}
I think this is somewhat related to #25
My initial intent was to mimic what ASP.NET Core MVC model validation does by default but it turns out I haven't even done that yet. I'd also like to avoid too many options, modes, etc. so I think the change here is to just make it have the same behavior as MVC. I can revisit whether there should be options to adjust what gets validated or shortcutting, etc. later on.
I'm not terribly familiar with base ASP.NET Core data annotation validation functionality... I was hoping to explore some of that alongside minimal apis through an experiment with this library!
I'll try forking this guy and playing around and seeing if I can learn anything and provide some value. I appreciate the response.
Sounds good. If you haven't already, check out my library for Minimal APIs that includes validation primitives build on top of MiniValidation.