Question: Is there any way of making sure that the specs I write point to other specs? I can write (s/def ::my-spec (s/keys [:not-a-spec/key]) And :not-a-spec/key doesn’t have to be a spec. Can I somehow enforce that it really is a spec that it’s pointing to? Also, if I could somehow avoid defining specs twice that would be fantastic

Asked By
Asked At
2018-01-23 15:54:26

Found 15 possible answers.

User Answered At Possible Answer
stathissideris 2018-01-23 16:59:58 why do you have to define them twice?
bronsa 2018-01-23 17:04:34
vikeri 2018-01-24 10:10:18 @stathissideris I don’t have to define them twice, but I want to avoid it happening
misha 2018-01-24 11:26:26 @vikeri "them" who? do you have a specific example of what you are trying to avoid?
vikeri 2018-01-24 11:27:38 I want this to throw an error/warning
(s/def ::hello int?)
(s/def :: hello string?)
misha 2018-01-24 11:28:34 then you'll get en exception on the next ns reload in repl, would not want that, would you? ) 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) @vikeri
    data: {:existing clojure.core/string?, :sym :user.specs/foo}
clojure.lang.ExceptionInfo: NO!
(sdefonce ::foo string?)
=> :user.specs/foo
(sdefonce ::foo string?)
    (s/def ~sym ~form)))
    (throw (ex-info "NO!" {:sym ~sym :existing (s/form (get (s/registry) ~sym))}))
  `(if (contains? (s/registry) ~sym)
defmacro sdefonce [sym form] otherwise, write a macro, which throws/warns if spec you are trying to define is already defined in specs registry
vikeri 2018-01-24 11:43: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. @bronsa Thanks!
misha 2018-01-24 11:45:01 sounds like too custom use case for an out of the box solution
pablore 2018-01-24 15:53:58 Is there a way to use s/assert and s/explain when the assert fails? for some reason assert y failing but explain is giving me success is it still good practice to use {:pre [(s/assert …)]} on functions?
schmee 2018-01-24 16:27:12 @pablore just keep in mind that asserts can be toggled off
pablore 2018-01-24 16:27:39 Yes but thats not the idea, I need it to fail when I give it a bad input
schmee 2018-01-24 16:28:21 here’s a thing I’ve found useful many times:
(defn validate [spec thing]
I use that instead of assert since it can’t be toggled off
(s/explain-data spec thing))))) (throw (ex-info (s/explain-str spec thing) thing (if (s/valid? spec thing)
ghadi 2018-01-24 16:28:53 there's another (similar) idea there
mrchance 2018-01-24 21:05: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?
kingmob 2018-01-24 22:46:05 Quick sanity check: in Clojurescript, is cljs.spec.test.alpha working for anyone else? I keep getting compilation errors about clojure.test.check. (Since it’s still alpha, I wasn’t sure)

Related Questions