This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-09-15
Channels
- # aws-lambda (3)
- # beginners (37)
- # boot (294)
- # carry (1)
- # cider (38)
- # cljs-dev (37)
- # cljsjs (88)
- # clojure (187)
- # clojure-android (2)
- # clojure-austin (1)
- # clojure-dusseldorf (9)
- # clojure-hk (3)
- # clojure-italy (12)
- # clojure-russia (36)
- # clojure-spec (55)
- # clojure-uk (27)
- # clojurescript (75)
- # community-development (5)
- # conf-proposals (2)
- # copenhagen-clojurians (3)
- # cursive (9)
- # datomic (54)
- # devcards (5)
- # devops (3)
- # dirac (69)
- # emacs (6)
- # ethereum (1)
- # euroclojure (1)
- # events (3)
- # funcool (1)
- # hoplon (20)
- # immutant (4)
- # luminus (14)
- # midje (4)
- # om (178)
- # om-next (2)
- # onyx (47)
- # pedestal (19)
- # protorepl (20)
- # re-frame (14)
- # reagent (54)
- # ring (2)
- # ring-swagger (7)
- # test-check (10)
- # uncomplicate (11)
- # untangled (9)
- # yada (9)
Can you set the :default
method for a multi-spec? Just adding a multimethod with :default
for the dispatch value results in no method
when calling explain
. Small example:
(defmulti event-type :event/type)
(defmethod event-type :event/search [_]
(s/keys :req [:event/type :event/timestamp :search/url]))
(defmethod event-type :default [_]
(s/map-of any? any?))
(s/def :event/event (s/multi-spec event-type :event/type))
(s/explain-data :event/event
{:event/type :foo
:event/timestamp 1463970123000
:search/url ""})
=> #:clojure.spec{:problems [{:path [:foo], :pred event-type, :val {:event/type :foo, :event/timestamp 1463970123000, :search/url ""}, :reason "no method", :via [:event/event], :in []}]}
it looks like no, because multi-spec re-implements the multimethod dispatch logic without :default
I think https://github.com/clojure/clojure/blob/master/src/clj/clojure/spec.clj#L888 would need a (or (.-defaultDispatchValue mm) ...)
@seancorfield that would be my guess too
@kenny if :default doesn’t work, it should be made to
at least as far as my thinking atm, so jira appreciated
I did put a patch in alpha12 to make hierarchies work again, but I don’t recall testing :default stuff
Oh shoot, I'm still on alpha11 because of the hash changes. I'll test it out in alpha12 and if it doesn't work I'll file a jira issue.
@alexmiller Yes it is fixed in alpha12. Thanks!
How "safe" am I in using spec in ClojureScript? Is it subject to any large changes in the future?
Any tips for writing a spec for the string version of a positive int? Here's what I'm doing now:
(defn- pos-int-string? [s]
(try
(pos-int? (Integer/parseInt s))
(catch Exception _
false)))
(s/def ::id pos-int-string?)
Could spec be used to parse a vector such as '[1 + 3 * 4] and conform the input in such a way so that operator precedence is preserved?
The first time you see a function spec actually catching invalid input data is absolutely wonderful!
@curlyfry spec is in alpha and subject to change (but probably unlikely to change dramatically, more additive)
Is there a way to use conform to drop all map keys not explicitly mentioned in an s/keys
spec?
Let's say I have something like this:
(s/def ::foo string?)
(s/def ::bar int?)
(s/def ::thingy
(s/keys :req-un [::foo]
:opt-un [::bar])
you could use something like (s/and (s/keys …) (s/conformer #(select-keys % #{::foo ::bar}))
I'd like to do this:
> (s/conform ::thingy {:foo "yup", :bar 42, :baz [1, 2, 3]})
{:foo "yup", :bar 42}
@alexmiller: Thanks!
@alexmiller Wow! You guys thought of everything!
BTW, it is possible to run spec.test/check
on a private function; my failure to do so was operator error. 🙂
Right now at least
No one has filed an enhancement for that yeah afaik
I'm not sure whether it should be supported or not
I personally think it should not be disallowed, since Clojure generally doesn't try too hard to save you from yourself with respect to "private" stuff in a namespace.
how might I spec a map whose keys I do not know, but whose values all have the same shape?
I have a little problem with this small test:
(defn all-divisions []
{:test nil})
(s/fdef all-divisions
:args empty?
:ret #(do (log/error "here") (not (data-format/nil-keys? %))))
:: repl
(test/instrument `all-divisions) ;; => [app.db.queries/all-divisions]
(all-divisions) ;; => {:test nil}
Am I spec-ing it right? (I don't see "here" in the logs)@richiardiandrea instrument
does not check :ret or :fn
only :args
?
and what do I need to use to check :ret
at runtime while developing ?
tnx @bfabry I did not know that
so if I want to instrument all the instrumentable symbols in my project, should I run!
on instrumentable-syms
the function instrument
?
No just call instrument with no args
Also be aware of enumerate-ns
@alexmiller cool, sorry it was right there in the docs
reading through it now