Fork me on GitHub
#clojure-spec
<
2016-08-25
>
niquola12:08:07

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

tgk12:08:55

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

tgk12:08:30

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

tgk12:08:40

(seems to be the Clojure philosophy)

niquola12:08:57

I've already read this thread - https://clojurians-log.clojureverse.org/clojure-spec/2016-06-09.html 🙂 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.

niquola12:08:52

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

niquola13:08:23

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

niquola13:08:26

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

minimal13:08:03

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

niquola13:08:55

Do you mean own version of s/keys? 😞

minimal13:08:59

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

minimal13:08:51

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

minimal13:08:19

(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}))

niquola13:08:43

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

niquola13:08:22

i.e. error-greedy s/and

minimal13:08:35

yeah could be useful

xcthulhu13:08:31

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

xcthulhu13:08:34

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.

potetm14:08:38

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

potetm14:08:16

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.

bbrinck14:08:57

@potetm I use something like this (might need to be updated for latest alpha): https://gist.github.com/alexanderkiel/931387c7a86c1879b2267ad064067af7

potetm14:08:53

@bbrinck Thanks! That'll do!

sattvik17:08:58

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

sattvik19:08:21

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.

sattvik19:08:03

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

http://clojure.org/guides/spec#_combining_code_check_code_and_code_instrument_code 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

sparkofreason19:08:17

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 https://ti.to/cognitect/clojure-conj-2016

sparkofreason20:08:05

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

sparkofreason20:08:16

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 :)

sparkofreason21:08:59

Apache library 😞

Alex Miller (Clojure team)21:08:12

ok, I’ll put it on Apache :)

shaun-mahood21:08:17

@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 :)

shaun-mahood21:08:28

Perfect, I should be able to handle that 🙂

sebastianpoeplau21:08:24

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...