This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-06-27
Channels
- # announcements (5)
- # beginners (267)
- # boot (1)
- # calva (37)
- # cider (11)
- # clara (10)
- # clj-kondo (24)
- # cljsrn (13)
- # clojars (1)
- # clojure (119)
- # clojure-europe (5)
- # clojure-italy (19)
- # clojure-nl (11)
- # clojure-spec (18)
- # clojure-uk (99)
- # clojurescript (44)
- # clojurex (57)
- # community-development (6)
- # cursive (13)
- # datomic (92)
- # duct (12)
- # fulcro (1)
- # graalvm (4)
- # jobs (1)
- # kaocha (6)
- # luminus (3)
- # lumo (9)
- # off-topic (20)
- # pathom (6)
- # re-frame (21)
- # reagent (2)
- # reitit (9)
- # remote-jobs (4)
- # shadow-cljs (32)
- # spacemacs (3)
Is it possible to "and the rest of the arguments should match this others spec"? Example:
(s/def ::kid (s/cat :name string? :age int?))
(s/def ::input-explicit-username (s/cat
:username uuid?
:kid ::kid))
(s/def ::input (s/or :implicit-user ::kid, :explicit-user ::input-explicit-username))
The thing is that I either have a short input or something+the short input. How to spec this without copy&paste? Thank you!
Answer: s/cat do not nest, they become one so the code above actually works as desired: (s/conform ::input ["name" 12])
=> [:implicit-user {:name "name", :age 12}]
(s/conform ::input [(java.util.UUID/randomUUID) "name" 12])
=> [:explicit-user {:username #uuid"c19bc1f5-033f-4ca1-b2e0-5877657cfa8d", :kid {:name "name", :age 12}}]
If you want some of regex specs to match nested structure, wrap them in s/spec, @holyjak
thx! I wouldn't figure that one out 🙂
(s/+ int?) matches
1 2 3
(s/spec (s/+ int?)) matches
(1 2 3)
s/def
wraps form in s/spec, so from (s/def ::x (s/+ int?))
it might appear that (s/+ int?)
matches (1 2 3)
(with parens). It does not.
I wouldn't say it that way
user=> (s/valid? (s/+ int?) [1 2 3])
true
user=> (s/valid? (s/spec (s/+ int?)) [1 2 3])
true
the more accurate way to look at it is that (s/+ int?) is a regex op (which is not actually a spec)
regex ops combine
when used, regex ops are automatically "spec-ized"
s/spec is an explicit "spec-ize" step
in all cases, regex ops are matching the elements in a sequential collection though
Is it a good practise to use keyword inheritance for multi-spec dispatch? e.g.
(defmulti example-config :entity/type)
(s/def :entity/type #(contains? (set (methods example-config)) %))
(defmethod example-config :default [_]
(s/keys :req [:entity/type]))
(s/def :entity/config (s/multi-spec example-config :entity/type))
(defmethod example-config :entity.type/generic [x]
(getx x :entity/type))
;; Use keyword inheritance to get to the right spec
(derive :entity.type/specific :entity.type/generic)
(s/def :entity.type/specific integer?)
(s/explain-data :entity/config {:entity/type :entity.type/spfecific})
sounds fine
not sure if there are any gotchas in gen, but might be
Thanks, I'll give it a go. Removes some boiler plate code