Fork me on GitHub
#clojure-spec
<
2018-01-16
>
seancorfield00:01:57

@stevep I can tell you what we're doing in production with clojure.spec at World Singles as far as REST APIs are concerned: we have a spec for each handler (i.e., each API endpoint) and the general pattern is roughly:

(let [params (s/conform ::this-api-spec (:params req))]
  (if (s/invalid? params)
    (error-response (s/explain-data ::this-api-spec (:params req)))
    (normal-process params)))

arnaud_bos20:01:35

(how-)do you prevent un-spec-ed keys to propagate further down the normal-process?

seancorfield22:01:27

@U1DLD0WNR In keeping with Clojure's open-for-extension approach, normal-process will just ignore keys it doesn't care about. Similarly, if I only spec certain keys, others don't get checked -- so the constraints are open.

seancorfield00:01:24

We have that wrapped up in a macro to reduce boilerplate but that's the essence of it. The error-response function decodes the spec failure based on a set of heuristics and "expected" failure points so we can return documented error codes and localized messages to the client.

seancorfield00:01:19

We only instrument functions in our dev/test cycle, not in release code. We also have specific "tests" that run clojure.spec.test/check on certain functions.

seancorfield00:01:01

The argument against just relying on instrument to check arguments is that you want better error messages than spec failures for your clients!

vikeri10:01:00

Can I modify to a spec after I’ve defined it? My use case is to add the :fn argument for an fdef but only when I run my tests.

conan11:01:37

@vikeri why can't you just add it anyway? instrument doesn't run the :fn and :ret bits by default anyway

conan11:01:52

i use orchestra.spec.test/instrument to do this

vikeri11:01:27

@conan I also use orchestra and it runs the :fn by default so that’s why I have that issue

conan11:01:03

ah, can you swap out which you use in different circumstances? so you can switch on/off the :fn execution

conan11:01:35

i think you can just call clojure.spec/fdef again fine, and it'll update the spec registry with whatever you give it

Alex Miller (Clojure team)12:01:16

You can specify an alternate spec to use with instrument