This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-02-26
Channels
- # aws-lambda (2)
- # beginners (10)
- # boot (17)
- # cider (19)
- # clara (1)
- # cljs-dev (13)
- # cljsjs (22)
- # cljsrn (1)
- # clojure (132)
- # clojure-austin (2)
- # clojure-berlin (2)
- # clojure-dusseldorf (1)
- # clojure-germany (2)
- # clojure-italy (7)
- # clojure-spec (6)
- # clojure-uk (5)
- # clojurescript (45)
- # core-matrix (3)
- # cursive (4)
- # datomic (8)
- # emacs (3)
- # keechma (3)
- # lein-figwheel (1)
- # leiningen (2)
- # lumo (24)
- # nyc (1)
- # off-topic (29)
- # om (68)
- # onyx (5)
- # perun (50)
- # planck (5)
- # protorepl (5)
- # re-frame (128)
- # reagent (10)
- # remote-jobs (1)
- # ring (4)
- # rum (41)
- # untangled (28)
- # yada (4)
Hey guys, so if I have s/keys
spec, that enforces a predicate on one of the keys (so it actually requires a generator, since it cannot satisfy predicate after so many tries), how the generator part would look like for the spec like that?
trivial example:
(s/with-gen
(s/and
(s/keys :req [:foo :bar :baz :qux :zap :dap])
(fn [m]) (= (:foo m) 42))
(fn []
;; how the generator would look like?
))
Do I have to generate a hashmap with (s/gen)
for each key and then some logic that satisfies the constraint? Is there a better way?I guess I would create a spec without the field(s) that I want to constraint and then merge it with another spec (with the constraint(s) and generator)
Yeah, splitting it in two would be my suggestion.
Since then you can (g/fmap #(assoc % :foo 42) (s/gen ::base-spec))
or something like that...
It seems like I can't unform collections. What am I missing?
(require '[clojure.spec :as s])
(s/def ::text
(s/and string?
(s/conformer
(fn [s] {:type ::text :value s :errors []})
(fn [m] (:value m)))))
; GOOD: string conforms correctly
(s/conform ::text "blah")
=> {:type :user/text, :value "blah", :errors []}
; GOOD: map unforms back to string
(s/unform ::text (s/conform ::text "blah"))
=> "blah"
; Bad: collection unform has no effect
(s/unform (s/coll-of ::text) (s/conform (s/coll-of ::text) ["blah" "blah"]))
=> [{:type :user/text, :value "blah", :errors []} {:type :user/text, :value "blah", :errors []}]
Ah, it's a known bug. http://dev.clojure.org/jira/browse/CLJ-2076