Skip to content

Aggregate Validate ValidationResults with Property-validated ValidationResults #34

Description

@zprobinson

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 IValidatableObjectValidate 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.cspublicsealedrecordMyContract([property:Required(ErrorMessage="An ID must be provided.")]stringId,[property:Required(ErrorMessage="A Favorite Number must be provided.")]intFavoriteNumber):IValidatableObject{publicIEnumerable<ValidationResult>Validate(ValidationContextcontext){if(FavoriteNumber>99)yieldreturnnew("3 digit numbers are not allowed!",new[]{nameof(FavoriteNumber)});}};
// Program.csapp.MapPost("/my-endpoint",([FromBody]MyContractcontract)=>!MiniValidator.TryValidate(contract,outvarerrors)?Results.ValidationProblem(errors):Results.NoContent());
### RequestPOST http://localhost:5000/my-endpointcontent-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."
]
}
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions