This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-10-05
Channels
- # 100-days-of-code (13)
- # announcements (3)
- # beginners (120)
- # boot (17)
- # calva (19)
- # cider (27)
- # cljdoc (3)
- # cljs-dev (1)
- # clojure (138)
- # clojure-dev (5)
- # clojure-italy (5)
- # clojure-nl (20)
- # clojure-russia (3)
- # clojure-spec (14)
- # clojure-uk (119)
- # clojurescript (45)
- # core-async (2)
- # datomic (23)
- # editors (28)
- # emacs (35)
- # figwheel (2)
- # fulcro (26)
- # graphql (2)
- # hyperfiddle (11)
- # jobs (4)
- # luminus (5)
- # mount (2)
- # off-topic (52)
- # onyx (39)
- # reagent (86)
- # ring-swagger (2)
- # spacemacs (20)
- # tools-deps (9)
- # yada (4)
you don’t have to require the namespace
fdef creates an entry in the spec registry keyed by the namespaced symbol, but you don’t need to load that namespace at that point
Wrapping a cat
with an and
is breaking my spec in interesting ways.
What am I missing?
(s/def ::pair
(s/cat :first (s/nilable number?)
:second (s/nilable number?)))
(s/conform ::pair [1 2])
;; => {:first 1, :second 2}
(s/conform (s/? ::pair) [1 2])
;; => {:first 1, :second 2}
(s/def ::ordered-pair
(s/and ::pair
#(<= (:first %) (:second %))))
(s/conform ::ordered-pair [1 2])
;; => {:first 1, :second 2}
(s/conform (s/? ::ordered-pair) [1 2])
;; => :clojure.spec.alpha/invalid
(s/explain-str (s/? ::ordered-pair) [1 2])
;; => "In: [0] val: 1 fails spec: :apij.unit.specs.models.rate/pair predicate: (cat :first (nilable number?) :second (nilable number?))\n"
the s/?
isn't "flattening" the regex spec (`s/cat`) because it's wrapped in s/and
. I think those last tests would conform [[1 2]]
if you wanted a something-or-nothing spec you could try (s/nilable ::ordered-pair)
too but I'm not sure of your use case
Yup, both cases conform correctly! I'm mostly trying to understand the non-flattening, though. Is there an alternative construct to use the ordering function without using s/and
?
hmm what would multiple pair inputs look like for you? one big sequence like [1 2 3 4]
or a sequence of pair seqs like [[1 2] [3 4]]
s/and converts a regex spec to a non-regex. s/& is a regex version
will still continue operating at the same “level” of sequence
ah yeah, you can just replace s/and
with s/&
in ::ordered-pair
if you want to conform inputs like [1 2 3 4]
Oh, Not only it makes perfect sense, but it's pretty obvious in retrospect! Thanks @U064X3EF3 and @U3DAE8HMG!