Fork me on GitHub
#clojure-spec
<
2019-03-27
>
zclj11:03:18

Do anyone know of a library that can produce specs given a json schema?

ikitommi12:03:46

@zclj haven’t heard, but would have use for that too. there is an issue in spec-tools, but no work going on: https://github.com/metosin/spec-tools/issues/154. Happy to help if someone starts doing that.

zclj12:03:45

@ikitommi thanks for the info. I will do some experiments on my specific case and if it is generic enough I would be happy to contribute to the issue in spec-tools

👍 4
ikitommi13:03:07

Is there a way to force a fdef to validate always the inputs? Schema had the :always-validate metadata for this.

ikitommi13:03:48

e.g.

(require '[schema.core :as s])

(s/defn ^:always-validate interceptor-x
  [opts :- {:string? s/Bool}]
  ...)

(interceptor-x {})
; Syntax error (ExceptionInfo) compiling at (test.cljc:150:1).
; Input to interceptor-x does not match schema:
;
;       [(named {:string? missing-required-key} opts)]

ikitommi13:03:44

Something like stest/instrument but for non-test use.

rickmoynihan13:03:12

Not that I know of. You can use a :pre condition on a function that calls s/valid?, though you’ll not get an s/explain-* style error if you do.

rickmoynihan13:03:32

If you actually want that though, why can’t you just call

(st/instrument #{`the.fdef.i.want.to.always/validate})

rickmoynihan13:03:09

I’d probably just call s/valid? and raise an s/explain-data if the check failed explicitly inside the function though if I wanted that — or however you want to report the error.

ikitommi13:03:58

That would work, but that would not contribute to the function documentation.

rickmoynihan13:03:25

It would if it was both an fdef and that 🙂

ikitommi13:03:18

calling clojure.spec.test.alpha from non-test code also doesn’t sound like a good idea.

ikitommi13:03:07

I’ll write an issue out of that.

rickmoynihan14:03:09

regarding configuring components on startup you might want to look at what integrant does: https://github.com/weavejester/integrant/#specs

ikitommi14:03:15

I would still like a support functional way of doing that: have a function that takes options and returns the component (that has a spec). And the function args should be validated too.

drone14:03:59

@ikitommi I’m not sure it addresses your exact issue, but orchestra’s instrument enables checking of :ret and :fn

drone14:03:10

this has been better for how we use spec; where we enable instrumentation during development to try and get type-checking-like feedback while writing code

Alex Miller (Clojure team)14:03:03

@ikitommi it is intentional that this functionality doesn't exist for functions as the goal is to have no overhead for function specs at runtime

Alex Miller (Clojure team)14:03:56

but I'll leave it open for now as maybe it would be useful to have an opt-in feature for that

5
ikitommi14:03:21

opt-in would be great.

5
ikitommi14:03:50

@mrevelle orchestra is great, but it also has the dynamic *instrument-enabled* which can disable the instrumentation.

jeaye23:03:16

We just use something like the following in our code:

(when-not-prod
    (let [instrumented (sort (stest/instrument))] ; [orchestra.spec.test :as stest]
      (timbre/trace :instrumenting instrumented)))