tapir icon indicating copy to clipboard operation
tapir copied to clipboard

[QUESTION] What is the expected OpenAPI description of a custom Validator?

Open willemvermeer opened this issue 3 years ago • 3 comments

Tapir version: 1.0.2

Scala version: 2.13.8

Describe the question

The following code snippet:

  val testPath: EndpointInput[Int] =
    path[Int]("someval")
      .validate(
        Validator.all(
          Validator.max(10),
          Validator.custom[Int]((i: Int) => ValidationResult.validWhen(i == 2), Some("When printed?")
          )
        )
      )

  val example =
    unsecureEndpoint
      .get
      .in("test")
      .in(testPath)

produces the following openapi spec (through OpenAPIDocsInterpreter.toOpenAPI()):

...
      parameters:
      - name: someval
        in: path
        required: true
        schema:
          type: integer
          format: int32
          maximum: 10
...

with no reference being made to the custom validator.

The validation does in fact take place:

$ curl http://localhost:8084/test/3
Invalid value for: path parameter someval (expected value to pass validation, but was: 3)%

Is there a way to make a custom Validator appear in the OpenAPI spec?

willemvermeer avatar Jul 25 '22 13:07 willemvermeer

I don't think OpenAPI allows representing custom validators? If it does then we should add that logic :)

adamw avatar Jul 25 '22 14:07 adamw

Aha - that makes sense 😄 thanks! Still, in the following declaration, when does showMessage get invoked and used?

def custom[T](validationLogic: T => ValidationResult, showMessage: Option[String] = None): Validator[T]

willemvermeer avatar Jul 26 '22 09:07 willemvermeer

It's used when creating a 400 Bad Request response (or at least, it should be) :)

adamw avatar Jul 26 '22 10:07 adamw

Closing this one as I think the problem is solved. Please reopen if that's not the case.

adamw avatar Aug 23 '22 12:08 adamw