This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-07-26
Channels
- # babashka (16)
- # beginners (38)
- # calva (11)
- # cider (35)
- # clj-kondo (3)
- # clj-otel (3)
- # clojure (28)
- # clojure-europe (11)
- # clojure-gamedev (14)
- # clojure-norway (42)
- # clojure-spec (4)
- # datalevin (10)
- # datomic (2)
- # emacs (8)
- # events (2)
- # fulcro (3)
- # gratitude (5)
- # hyperfiddle (3)
- # kaocha (1)
- # nbb (14)
- # nrepl (12)
- # portal (1)
- # re-frame (5)
- # releases (1)
- # shadow-cljs (36)
- # squint (167)
Sorry, another question that I couldn’t find a good answer for via G. How do I spec a nested structure? Here is the code snippet that I tried.
(s/def ::my-spec (s/cat :a-b (s/cat :s string? :i int?) :s2 string?))
(s/explain-data ::my-spec [["test" 1] "world"])
;; => #:clojure.spec.alpha{:problems [{:path [:a-b :s], :pred clojure.core/string?, :val ["test" 1], :via [:ct-test/my-spec :ct-test/my-spec], :in [0]}], :spec :ct-test/my-spec, :value [["test" 1] "world"]}
I guess I could use a tuple in the spec and that would resolve it but that’s more limiting that s/cat
.
Different question? Is there any good way to put these answers into Google?I think you can get the nesting you want by wrapping the inner s/cat
with s/spec
.
https://clojurians.slack.com/archives/C1B1BB2Q3/p1664745079479859?thread_ts=1664739041.707149&cid=C1B1BB2Q3
(require '[clojure.spec.alpha :as s])
(s/def ::my-spec (s/cat :a-b (s/spec (s/cat :s string? :i int?)) :s2 string?))
(s/valid? ::my-spec [["test" 1] "world"]) ;; true
gotcha thanks!