This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-08-12
Channels
- # beginners (49)
- # boot (2)
- # cider (62)
- # clara (3)
- # cljdoc (5)
- # cljs-dev (7)
- # cljsrn (2)
- # clojure (68)
- # clojure-spec (23)
- # clojure-uk (4)
- # clojurescript (172)
- # conf-proposals (8)
- # core-async (1)
- # cursive (1)
- # datomic (12)
- # duct (1)
- # figwheel (22)
- # figwheel-main (11)
- # fulcro (23)
- # hoplon (9)
- # jobs-discuss (13)
- # lambdaisland (3)
- # lein-figwheel (119)
- # off-topic (43)
- # onyx (1)
- # re-frame (18)
- # ring (1)
- # shadow-cljs (120)
- # tools-deps (4)
@seancorfield I have an example here: https://pastebin.com/Kuat3MPV . That's what i'm doing now, but is there a better way of "namespacing" specs?
@ackerleytng OK... and what's the question again? That seems OK to me on the face of it...
ah ok. I just thought there might be a better way of namespacing all of that
thanks then!
so I have this spec:
(s/def ::projection
(s/* (s/alt :key keyword?
:sym symbol?
:projection ::projection)))
and when I try and run valid?
, I get StackOverflowError clojure.spec.alpha/deriv (alpha.clj:1526)
I’m not sure if you’re intending to validate nested collections, but you can do this:
(s/def ::projection
(s/* (s/alt :key keyword?
:sym symbol?
:projection (s/spec ::projection))))
(s/valid? ::projection [:a :b 'c])
=> true
(s/valid? ::projection [:a :b 'c "x"])
=> false
what’s the difference between writing :projection ::projection
and :projection (s/spec ::projection)
?
wrapping a regex-type spec in s/spec
prevents that flattening, so you can use them to describe nested sequences of things
the stack overflow is just because of how spec is implemented, if you have a certain form of nested/recursive regex specs it recurses infinitely trying to evaluate it
@lilactown FWIW you can also define your spec using s/or
instead of s/alt
, and then you don’t need the s/spec
call to prevent the regex flattening:
(s/def ::projection
(s/* (s/or :key keyword? :sym symbol? :projection ::projection)))
by “flattening” I mean that when you nest the regex-type specs, by default they merge/compose/combine/maybe-a-better-word to describe a single sequence