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

Codecraig

12/12/2018, 12:41 PM
Hi, I read that http4k can generate JSON Schema models (https://www.http4k.org/guide/modules/contracts/) ...is there an example/tutorial for that?
d

dave

12/12/2018, 1:02 PM
No real tutorial, but you can look at this example https://www.http4k.org/cookbook/typesafe_http_contracts/ or there is a test which shows all of the options (ContractRendererContract)
There are also example apps which use the contracts (see the site)
c

Codecraig

12/12/2018, 1:06 PM
So, if I understand correctly, the contract allows http4k to validate a request against the "OpenApi" contract, which takes that work away from the developer, right?
How does JSON Schema fit in? My experience with JSON Schema, outside of http4k, has been 1) define a schema, 2) validate JSON against it (such as the body of an HTTP request)
d

dave

12/12/2018, 1:10 PM
http4k will only validate the contents of the path (so it can match the handler) before dispatching the request to the handler. the rest of the parameters are checked only when they are accessed by your code, and the CatchLensFailure filter will convert errors to a 400. so you will have to write zero validation code for any defined "primitives" (including datetimes and anything else the lens API supports).
Json schema is really there only for the Swagger interface (it is generated on demand from examples defined in the code). We generally use the Jackson auto marshalling to provide automatic validation of the body schema by defining data classes in the shape that we require (it will blow up in the lens when the body is unmarshalled and cause a 400)
c

Codecraig

12/12/2018, 1:16 PM
Ok, thanks for the info.
d

dave

12/12/2018, 1:25 PM
np
10 Views