This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-11-02
Channels
- # announcements (21)
- # babashka (1)
- # beginners (67)
- # calva (18)
- # cider (21)
- # clj-kondo (109)
- # cljs-dev (3)
- # clojure (129)
- # clojure-spec (15)
- # clojure-uk (30)
- # clojurescript (3)
- # datomic (2)
- # graalvm (8)
- # graphql (1)
- # juxt (1)
- # malli (28)
- # off-topic (46)
- # reitit (5)
- # rewrite-clj (33)
- # ring-swagger (2)
- # shadow-cljs (199)
- # vim (9)
I'm having a pretty extensive spec, with quite some s/or
parts. When any data structure doesn't conform, it shows the issues, as expected. Conforming structures show that they conform... so all is well so far.
However, what I'd like to have, is to know which 'path' has been taken to conform:
(s/def ::spec (s/and string?
(s/or :path-A ::path-A
:path-B ::path-B)))
Is there a way to know, if a structure conforms, to know if it took path-A
or path-B
?oh yeah, it is... thx.
that was easy 🙂
Hi everyone! Let's say I have a map like this
(def example-map {0 {:children [1 22 55] :parent nil}
22 {:children [14] :parent 0}
14 {:children [] :parent 22}})
How can I ensure with spec that a) a :parent is never the same key as the current line or ideally b) that :parent is a valid key in the map? I read through the website and some tutorials and I don't know if that's even possible with spec. Any advise would be super helpfulYou can write arbitrary functions that return true or false for a given value, and use those in your specs, so if you can write a normal Clojure function to do it, you can write a spec to do it. Some of those reduce the amount of help you get from random generation of values, etc.
This sounds useful, can you hint me what this is called in the docs so I can look it up? Also, I thought it would be cool to have the test cases auto-generated, so that won't work?
This property of functions is mentioned very early in the spec guide article here: https://clojure.org/guides/spec under the heading "Predicates"
I am not a knowledgeable enough user of spec to give you the best answer to how to auto-generate things with custom predicates, but there are places in the docs that describe how to create custom random value generators and use those instead of the default built-in ones.
I guess where I'm stuck at is how I bring that into the s/def of a map-of. I can't put it onto the smaller pieces, because they'd have to reference each other
Ok, I thought of looking into the generators, but was hoping there is an easier solution to define it on the specs. There are not really a lot of examples out there that are more complex than one-liners. Thanks for your help!
And if anyone has an idea about how to link it with the s/def of the map-of I'm still curious. Will be back at the computer later and will try to find out
I believe s/and can combine arbitrary specs into one?
I'll try later - thanks!
Eventually I found some good examples here: https://clojuredocs.org/clojure.spec.alpha/map-of.