Fork me on GitHub
#clojure-spec
<
2017-07-12
>
danielcompton01:07:55

I just realised that if you're using a tools.namespace based workflow, the spec registry isn't cleared when you refresh. Is there a recommended way to "clean the registry" safely?

danielcompton01:07:41

I suspect that if I just reset! the registry I might be missing internal/clojure.core.specs specs?

twashing01:07:52

Question. This will break with Clojure telling me that “No such var: eid”

twashing01:07:53

(s/def ::eid (s/with-gen (s/and number?
                                pos?
                                #(instance? java.lang.Long %)
                                (complement nil?))
               gen/int))

(s/fdef foo/bar
        :args (s/cat :a any?
                     :b ::eid)
        :ret (s/coll-of ::some/things))

danielcompton01:07:18

what is es/eid?

twashing01:07:42

::eid is the first form

twashing01:07:54

… sorry wrong namespace

twashing01:07:52

Why can’t I use ::eid in my function spec :args?

danielcompton01:07:58

@twashing I can load this into a REPL fine:

(defn bar [])

(s/def ::eid (s/with-gen (s/and number?
                                pos?
                                #(instance? java.lang.Long %)
                                (complement nil?))
                         gen/int))

(s/fdef bar
        :args (s/cat :a any?
                     :b ::eid)
        )
how are you generating the error?

twashing01:07:06

It’s the function that generates generative tests, based on the function spec.

twashing01:07:14

... one sec, lemme find it.

twashing01:07:15

clojure.spec.test/check

souenzzo03:07:45

::stuff is a map with a fixed key :data that will never change,.

(s/def :foo/data (s/keys :req [:relevant/key :another.relevant/key]))
(s/def ::stuff (s/keys :req-un [:foo/data]))
There is some how to "omit" :foo/data definition?

joost-diepenmaat10:07:42

what’s the recommended way to run spec checks during automated testing? we’re currently using lein test with clojure.test and it would be nice to use something like the solution here https://stackoverflow.com/questions/40697841/howto-include-clojure-specd-functions-in-a-test-suite

joost-diepenmaat10:07:34

if I have to I can copy & paste and adapt that code but it would be nice to have a supported library

gfredericks11:07:46

@joost-diepenmaat if you paste it into https://github.com/gfredericks/schpec and make a PR, I'll release it not sure if that counts as "supported" or not

jpmonettas14:07:26

hi everyone! I'm trying to understand how to match :clojure.spec.alpha/problems inside :clojure.spec.alpha/value in explain-data result independent of the specs that were applied and I'm having some trouble

jpmonettas14:07:19

so the thing is that seeing a problem in [::k 1] you don't know if it means, it's the value of that key, or it's the element at pos 1 of a seq in the value of that key

jpmonettas14:07:23

does it make sense?

bronsa14:07:17

you're right in your analysis, it's been discussed before and it's logged in that ticket

jpmonettas14:07:45

great, thx a lot @bronsa

urbank16:07:27

So I'm wondering, is spec with s/def + s/valid? supposed to replace predicate functions? Otherwise I find it hard to decide what should be a spec and what should just be a function

urbank17:07:42

Oh, I suppose that isn't really a good question. More pertinent would be whether new predicates should be written as anon functions in s/def or should s/def refer to a named predicate function

rickmoynihan10:07:24

depends… sometimes the predicate function is generic and reusable but the s/def can give it a more specific name/abstraction. e.g. (s/def ::user-id integer?) (s/def ::post-id integer?)

rickmoynihan10:07:57

othertimes the predicate is too specific to be reusable, so just keep it anonymous.

rickmoynihan10:07:13

(No idea if this is best-practice btw… but it’s what I’ve been doing)

assoc-in17:07:04

I am wondering how I would write a spec for a nested map. I have something like this {:1 {:user-id 1 :username "bob"} :2 {:user-id 2 :username "jim"}} Where I have a (s/def ::user (s/keys :req-un [::user-id ::username])). I know how to do this if I had a vector of users by using (s/coll-of ::user), but how would I approach this with having the map keys being the ::user-id spec and the values of each map being the ::user?

bfabry17:07:11

@assoc-in (s/map-of keyword? ::user)

assoc-in17:07:21

@bfabry Ah yes I didn't see that looks like just what I needed. Thanks!