Hi I've a question about input validation. An endp...
# http4k
n
Hi I've a question about input validation. An endpoint accepts a payload described by some data classes. I've added a validation in a data class, like this:
Copy code
data class Payment(
    val type: PaymentType,
    val amount: Float,
    val reason: String,
    val expiryDate: String,
    val code: String,
    val lisHoldingCode: String
) {
    init {
        require(reason.isNotEmpty()) { "Payment reason cannot be empty" }
        require(code.isNotEmpty()) { "Payment code cannot be empty" }
        require(lisHoldingCode.isNotEmpty()) { "Lis code cannot be empty" }
    }
Now I am writing a test to assure that validation rules are respected (ie invalid payload are not accepted). This happens but message is too vague: {"message":"Missing/invalid parameters","params":[{"name":"body","type":"body","datatype":"object","required":true,"reason":"Invalid"}]} I would need a message which clearly states which parameter is wrong, using the message I've putted in the require How can I do that? Thanks in advance
Deserialization is done through a (jackson) lens:
Copy code
val jacksonPaymentRenderRequestDtoLens = Body.auto<Array<PaymentRenderRequestDto>>().toLens()
val dtos = jacksonPaymentRenderRequestDtoLens.extract(req)
Perhaps is it possible to customize these checks in the lens?
(those code lines are from the function controller, the http method is a POST so payload are read from body)
a
I too struggle with this limitation.
n
Not happy to hear this ...
a
Not to throw David under the bus, but he might know better than I
n
We don't want David under any bus, don't we?