Let's say I have a Request data class:
data class Request(
val firstName: String,
val lastName: String
)
which I want to serialize when getting to a specific api route. Using Ktor, it would look like this:
call.receive<Request>()
This would work perfectly if I get a valid json such as { "firstName: "TestFirst", "lastName": "TestLast" }
But, what if we get a json object or array instead of the expected string? { "firstName: [], "lastName": {} }
?
The library would throw an exception and I wouldn't be able to know we had two different validation problems:
- firstName must be a valid string (and not an array)
- lastName must be a valid string (and not an object).
How can I find these errors so that I would be able to map them nicely back to the user in the rest api response?