Hello all 😃. What is the best way to handle validation errors in Ktor? In spring I used the @ControllerAdvice and @ExceptionHandler (). But I'm a little lost here
d
Dico
07/19/2019, 11:23 PM
Add an early pipeline handler and respond to the call with your status code/message
👍 1
r
Rodrigo Silva
07/20/2019, 12:16 PM
But if I have a form with the following field:
name
last name
phone
And each field has its validation, with its message (minimum number of characters, can not be null, etc). Do I add to the pipeline validation, and will I have the return of every error?
Example: The user has filled in all fields in the wrong way:
status code 400
{
{
field: name
message: Can not be null
}
{
field: last name
message: Can not be null
}
{
field: phone
message: Can not be null
}
}
d
Dico
07/20/2019, 2:59 PM
That's up to you
Dico
07/20/2019, 2:59 PM
Probably doesn't need to be so detailed
r
Rodrigo Silva
07/20/2019, 6:20 PM
Thx @Dico. I got something close to what I wanted using Yavi.
c
coderskitchen
07/21/2019, 9:02 PM
Maybe worth a look for you https://github.com/tyntec/ktor-problem . It provides a consistent pattern for error responses. In Java with Spring I used a similar library that catches the validation errors and translated them into a RFC7807 compatible object.