tapir
tapir copied to clipboard
[QUESTION] What is the expected OpenAPI description of a custom Validator?
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?
I don't think OpenAPI allows representing custom validators? If it does then we should add that logic :)
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]
It's used when creating a 400 Bad Request response (or at least, it should be) :)
Closing this one as I think the problem is solved. Please reopen if that's not the case.