tapir icon indicating copy to clipboard operation
tapir copied to clipboard

[Feature] Add a way to accumulate multiple validations errors

Open KristianLentino99 opened this issue 2 years ago • 1 comments

Hi guys, I would like to start with congratulate me for your great work, I love use Tapir! I wanted to ask if it was possible to create an accumulator of validation errors when validating the query parameters/body of a request. For example in this piece of code:

import sttp.tapir._

val e = endpoint.in(
  query[Int]("amount")
    .validate(Validator.min(0))
    .validate(Validator.max(100)))
 query[Int]("taxes")
    .validate(Validator.min(0))
    .validate(Validator.max(100)))

This piece of code will fail at the first validate but I would like to return to my client something in this format (RFC 7807):

 {
   "type": "https://example.net/validation-error",
   "title": "Your request parameters didn't validate.",
   "invalid-params": [ {
                         "name": "amount",
                         "reason": "must be a greater than 0"
                       },
                       {
                         "name": "taxes",
                         "reason": "must be less than 100"}
                     ]
   }

Do you think that this is possible?

Thanks a lot!

KristianLentino99 avatar Dec 17 '22 21:12 KristianLentino99

Currently the decoder stops at the first decode failure (see DecodeBasicInputs). It should be possible to modify it so that it returns all decode failures for all basic inputs (the leafs in the structure of the input value), however that wouldn't be that straighforward.

Higher up (for decoders specified for .maps on input tuples etc.), I don't think it would be possible to collect all decode failures, but that's probably much more rare than decode failures on the basic ones.

So in summary: possible, but would require some work :)

adamw avatar Dec 20 '22 15:12 adamw