Fork me on GitHub
#clojure-spec
<
2023-07-26
>
Joerg Schmuecker05:07:50

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?

phronmophobic05:07:00

(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