https://kotlinlang.org logo
#http4k
Title
# http4k
r

Raf Szałański

04/21/2022, 3:21 PM
Hey! I wonder if there is any way of telling which json fields were missing or had incorrect type when parsing request using lenses. Let’s say my endpoint expects requests three fields:
id: String
,
name: String
and
count: Int
and the caller forgets to send the
count
or maybe sends a boolean there. When parsing such request, http4k will likely throw a lens failure, e.g:
Copy code
body 'body' must be object
org.http4k.lens.LensFailure: body 'body' must be object
I found this piece of code recently https://github.com/http4k/http4k/blob/master/http4k-core/src/main/kotlin/org/http4k/lens/Validator.kt and thought it’d be fantastic to make it work with json parsing. Unfortunately, I can only see it being used with web forms and not sure it works with json https://github.com/http4k/http4k/blob/master/http4k-core/src/test/kotlin/org/http4k/lens/ValidatorTest.kt
👋 2
To be specific: I wanted to use
Validator.Feedback
for json parsing but maybe I’m approaching the problem the wrong way?
d

dave

04/22/2022, 4:08 PM
Unfortunately we don't have the technology to provide this because we are reliant on the underlying JSON libraries for unmarshalling. You can extract the first error from a lens failure by interrogating the stack trace (where you get a path-like message of what went wrong) 🙂
r

Raf Szałański

04/22/2022, 4:10 PM
Thanks! I noticed that Lens failures have got very dev-friendly structure and are easy to parse and that it’s the json library which obfuscates the error messages
I’m working on some solution for one json library that we use with lenses and will try sharing!
1
@dave I started building a handler like that which works with jackson and I’d be happy to share it with the community. would you be interested in that?
d

dave

05/04/2022, 12:37 PM
it would be great to see what you've come up with - even if it's just a gist. A Gist with tests even better 😉
40 Views