This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # announcements (1)
- # aws (5)
- # babashka (15)
- # beginners (204)
- # bristol-clojurians (3)
- # cider (16)
- # clojure (283)
- # clojure-dev (8)
- # clojure-finland (30)
- # clojure-italy (4)
- # clojure-nl (6)
- # clojure-spec (17)
- # clojure-survey (161)
- # clojure-sweden (7)
- # clojure-uk (62)
- # clojurescript (4)
- # core-async (31)
- # cursive (3)
- # datomic (7)
- # defnpodcast (1)
- # fulcro (8)
- # jobs (2)
- # lumo (2)
- # malli (2)
- # off-topic (24)
- # other-languages (1)
- # overtone (1)
- # re-frame (6)
- # remote-jobs (3)
- # shadow-cljs (6)
- # spacemacs (17)
- # tools-deps (20)
I know that you can pull the
:arg spec from a function spec.
Is there a way to decompose/query other kinds of specs? For example, I would like to do something like the following to get the spec for the individual fn argument named
(def my-fn-spec (s/fspec :args (s/cat :x int?) :ret int?)) (s/valid? (:args my-fn-spec) ) ;; true (s/valid? (:ret my-fn-spec) 5) ;; true
(:x (:args my-fn-spec)) ;; or maybe (first (:args my-fn-spec))
I think the TL;DR is not easily with Spec 1 @erp12 but Spec 2 offers more facilities for taking specs apart and programmatically building them.
In Spec 1, you can get the form of a Spec and break it apart, but it isn't easy to turn that back into Spec objects that you can use tho'...
@seancorfield Good to know, thank you! Based on that, would you agree that currently the best option would be to keep the sub-specs in a map and use some utility functions to "materialize" real specs from them. Just spitballin' here but something like ...
(s/def ::spec (s/spec s/spec?)) ; Deconstructed Function Spec (s/def ::arg-specs (s/coll-of ::spec)) (s/def ::ret-spec ::spec) (s/def ::d-fn-spec (s/keys :req [::arg-specs ::ret-spec])) ; Deconstructed Collection Spec (s/def ::coll-kind ::spec) (s/def ::element-spec ::spec) (s/def ::d-coll-spec (s/keys :req [::coll-kind ::element-spec])) ; Deconstructed Map Spec (s/def ::key-spec ::spec) (s/def ::value-spec ::spec) (s/def ::d-map-spec (s/keys :req [::key-spec ::value-spec])) (defn construct-spec [m] ...)
I don't really understand what problem you are trying to solve here... It doesn't look like the sort of thing I've seen anyone trying to do with Spec.
Have you looked at Spec 2? That's much more amenable to programmatic manipulation of specs...
Hi. I'm struggling with generating data from a simple
(s/schema) use case.
(s/def ::x int?) (s/def ::baz (s/schema [::x])) (s/def ::bar ::baz) (s/def ::foo (s/schema [::bar])) (gen/sample (s/gen (s/spec ::foo)))
The call to
(gen/sample) throws a
No implementation of method: :conform* of protocol: #'clojure.alpha.spec.protocols/Spec found for class: clojure.lang.Keyword exception.
While a straightforward translation to
s/keys seems to work fine:
I'm probably getting something wrong in my schema definitions, but I can't figure out the problem.
(s/def ::x int?) (s/def ::baz (s/keys :opt [::x])) (s/def ::bar ::baz) (s/def ::foo (s/keys :opt [::bar])) (gen/sample (s/gen (s/spec ::foo)))
:bar definition to
appears to work around the issue.
(s/register ::bar (s/resolve-spec ::baz))
that makes sense - you're basically copying the spec object rather than relying on resolving through the alias