Hello, I would like that when parsing a body reque...
# ktor
l
Hello, I would like that when parsing a body request in
Copy code
call.receive<FooRequest>()
to sanitize all fields declared as String in
FooRequest
What would you suggest?
a
The simplest thing would be to validate them after deserialization. Could you please describe your problem in more detail?
e
Hey @Luca Gentile, you can write validation in the
init
block of the
FooRequest
l
I was thinking of something like: "every time the object mapper should transform a value from the call to a String property of the FooRequest, sanitize the field"
e
It depends on the serialization library you're using
l
at the moment I'm using Jackson
e
Yep, I guess only
kotlinx.serialization
allow you to do this in the init block, it should be default way in Jackson
l
ok, but that way for every DTO like FooRequest used in my api, I need to write a sanitation for string properties
I would like to do something that does that just in time, maybe you have some ideas
so that
val dto = call.receive<AnyRequest>()
inside
dto
every String is already trimmed, html escaped etc.
no matter the <AnyRequest>
without the need via init block in every
FooRequest
BarRequest
a
I didn't find any information on how you can do it with Jackson. You could probably use Kotlin reflection to sanitize all string fields after deserialization without introducing much code duplication.
l
me neither. Yeah, probably I'll go that way
thanks!