clojure-spec

Joerg Schmuecker 2023-07-26T05:18:50.568659Z

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?

Joerg Schmuecker 2023-07-27T07:22:56.659769Z

gotcha thanks!

phronmophobic 2023-07-26T05:55:36.161359Z

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

phronmophobic 2023-07-26T05:57:00.877649Z

(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