Fork me on GitHub
#reitit
<
2019-08-24
>
y.khmelevskii13:08:56

Hi gents! Can you please help me to understand how I can disallow extra keys in responses? I added response spec

:responses    {200 {:body ::response}}
and my spec definition
(s/def ::response
  (st/spec
   {:fail-on-extra-keys-transformer true
    :spec (s/keys :opt-un [::one ::two])}))
I need to throw exception when endpoint return not allowed fields in response

ikitommi17:08:03

@y.khmelevskii you need to define a custom :coercion that includes the spec-tools.transform/fail-on-extra-keys transformer for :map type. By default, it's strip-extra-keys-transformer which does the derived recursive select-keys on the data. Not sure if there is an example how to swap that, but if you look at the reitit.coercion.spec ns, you should see how to swap that. Hope this helps

ikitommi17:08:21

btw, about to add malli coercion module, most likely will rewrite some of the internal specs with malli to get better errors on invalid route data etc.

ikitommi17:08:39

malli is the last missing part of "everything as data". With it, one can persist the whole route table with into a file/db. Also, models can be served from the backed, read at runtime in cljs and applied locally.

y.khmelevskii20:08:33

btw, about malli, what the benefits to use malli if new clojure.spec2 which don’t use macros and we will have possibility to transform specs in runtime will available soon?

ikitommi21:08:35

@y.khmelevskii excellent question, don't know yet. I hope spec will do all the things we want, but it's three years in alpha. Haven't heard that the runtime value transformations would ever be in the scope of spec. Stripping out extra keys based on spec was still last week "Haven’t really considered it. Why not just use select-keys?". Have I missed some news?

ikitommi21:08:02

anyway, not looking malli to compete with spec, but it tries to do things right which we feel is important, we have real life use cases for it. Hopefully someone else finds it useful.

ikitommi21:08:38

oh, transforming specs and transforming values based on spec are two different cases. Both are needed.

👍 4