Fork me on GitHub

Hi all, i think this question already was answered, but - how to restrict collection of keys in map, i.e. no extra keys?


@nicola: You could write a predicate to cover that case and do (s/and (s/keys …) pred)


I assume there isn’t a standard way of doing it to be liberal in what you should be able to receive as data


(seems to be the Clojure philosophy)


I've already read this thread - 🙂 We want to use clojure.spec instead of json schema and we have many cases of miss-typed keys, so validating this would be helpful. :closed true would be nice, but let's start from custom pred.


Then another question: s/and report errors of first failed predicate, could i compose two predicates - so both be applied, even if first failed?


@minimal: same problem with excl-keys, it uses internally s/and 😞


I want report both - extra-keys and ok-keys validation problems


You’ll probably have to come up with your own custom version


Do you mean own version of s/keys? 😞


@nicola I guess so if it doesn’t report problems how you want


You could use the clojure.set namespace to give more interesting reporting


(defn keys-diff [expected provided]
  (let [expected (set expected)
        provided (set provided)
        missing  (set/difference expected provided)
        extra    (set/difference provided expected)]
    {:missing-keys missing
     :extra-keys extra}))


thx @minimal , i've got it. It's not problem to write extra-keys predicate, but force validation and reporting both extra-keys and s/keys


i.e. error-greedy s/and


yeah could be useful


At one point someone here was trying to convert specs to BNF grammars - what came of that?


That could be incredibly valuable - it would be very useful if you could compile a spec into a .proto file, or even C++ code for a JNI interface.


Has anyone had experience setting up clojure.spec.test/check to run automatically (e.g. via lein test)?


I know you can wrap it in a (deftest foo (testing ...)), but the output of an is failure isn't as verbose as the output of check.


@potetm I use something like this (might need to be updated for latest alpha):


@bbrinck Thanks! That'll do!


Are there any examples of non-trivial uses of instrument, esp. showing how :spec, :stub, :gen, and :replace are used?


Yes, I have, and it has been really useful. It’s just we’re trying to figure out how to use instrument and check together for some functions that may not be specced or only partially specced.


The videos on ClojureTV have also been useful, but a more thorough example that shows how to use some of the arguments to instrument to deal with things that might otherwise be considered part of a “test fixture”.

Alex Miller (Clojure team)19:08:14 has one example and I will write a blog on it eventually (but prob not for a while)

Alex Miller (Clojure team)19:08:40

I think Stu might have a screencast in mind for it, but not sure


I've been getting this exception a lot lately when running check: "Additional data must be non-nil." Any thoughts on why?

Alex Miller (Clojure team)19:08:57

that shouldn’t ever happen :)

Alex Miller (Clojure team)19:08:34

it basically means that conform failed, but explain didn’t find any problems so ex-info was created with a nil map

Alex Miller (Clojure team)19:08:06

unfortunately this exception happens while throwing the useful exception so it’s hard to diagnose

Alex Miller (Clojure team)19:08:19

but it’s definitely a bug whatever it is

Alex Miller (Clojure team)19:08:25

a bug in Clojure that is

Alex Miller (Clojure team)19:08:43

and if you can figure out what it is, I’d love a jira on it

Alex Miller (Clojure team)19:08:23

it’s not too hard to hack Clojure itself to dump more info when this happens if you have the interest in doing so

Alex Miller (Clojure team)19:08:31

FYI, I’m doing a half day spec workshop at the Conj this year - tickets available now


If I can come up with a consistent minimal repo, I'll file a ticket.


I'm going to venture a guess, though, based on some recent debugging, which is that it seems to occur when I have a function that returns another spec'ed function, and the returned function throws an exception while spec is checking it. Just a guess, will try to see if that stick consistently when I have a little time.

Alex Miller (Clojure team)20:08:48

hmm, well I guess that’s possible too (although I’d put that one on you :)


Apache library 😞

Alex Miller (Clojure team)21:08:12

ok, I’ll put it on Apache :)


@alexmiller: Any expectation of previous spec knowledge for your Conj workshop, or can we come in blind?

Alex Miller (Clojure team)21:08:42

I will expect basic Clojure knowledge :)


Perfect, I should be able to handle that 🙂


This might be a stupid question, but can anyone give me a hint how to spec the following function:

(defn my-apply [f & args]
  (apply f args))
(This is a simplified version of what I actually have.) My problem is in expressing that (count args) has to be equal to the number of parameters that f takes...