Fork me on GitHub
#clojure-spec
<
2018-01-24
>
vikeri10:01:18

@stathissideris I don’t have to define them twice, but I want to avoid it happening

misha11:01:58

do you have a specific example of what you are trying to avoid?

vikeri11:01:38

I want this to throw an error/warning

(s/def ::hello int?)
(s/def ::hello string?)

misha11:01:34

then you'll get en exception on the next ns reload in repl, would not want that, would you? )

misha11:01:44

otherwise, write a macro, which throws/warns if spec you are trying to define is already defined in specs registry

misha11:01:08

defmacro sdefonce [sym form]
  `(if (contains? (s/registry) ~sym)
    (throw (ex-info "NO!" {:sym ~sym :existing (s/form (get (s/registry) ~sym))}))
    (s/def ~sym ~form)))

(sdefonce ::foo string?)
=> :user.specs/foo
(sdefonce ::foo string?)
clojure.lang.ExceptionInfo: NO!
    data: {:existing clojure.core/string?, :sym :user.specs/foo}

misha11:01:49

you might check if form is the same as in registry, and it will not blow up on ns reload for unchanged specs, but still will on the ones you are iterating over (developing)

vikeri11:01:15

@misha I only want to deny overrides when I run the tests to make sure I haven’t duplicated it in the code. (Loading some specs from an external lib). Yeah I was considering writing my own macro but then I’d like to override the macro so that normally it uses the normal s/def but when I run my tests it throws the error. I’ll probably go with my own macro actually.

misha11:01:01

sounds like too custom use case for an out of the box solution

pablore15:01:58

Is there a way to use s/assert and s/explain when the assert fails?

pablore16:01:49

is it still good practice to use {:pre [(s/assert …)]} on functions?

pablore16:01:06

for some reason assert is failing but explain is giving me success

schmee16:01:12

@pablore just keep in mind that asserts can be toggled off

pablore16:01:39

Yes but thats not the idea, I need it to fail when I give it a bad input

schmee16:01:21

here’s a thing I’ve found useful many times:

(defn validate [spec thing]
  (if (s/valid? spec thing)
    thing
    (throw (ex-info (s/explain-str spec thing)
                    (s/explain-data spec thing)))))

schmee16:01:36

I use that instead of assert since it can’t be toggled off

ghadi16:01:02

there's another (similar) idea there

mrchance21:01:46

Hi, I want to generate test data based on a swagger spec, but subject to some constraints. Do you think spec is a good tool for that? If yes, how would one best go about that? If not, what would be a better way to do it?

Matthew Davidson (kingmob)22:01:04

Quick sanity check: in Clojurescript, is cljs.spec.test.alpha working for anyone else? I keep getting compilation errors about clojure.test.check.

Matthew Davidson (kingmob)22:01:14

(Since it’s still alpha, I wasn’t sure)

dadair23:01:20

In some cases you need to explicitly import [org.clojure/test.check <ver>], at least on the clj side