This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-11-10
Channels
- # announcements (4)
- # asami (3)
- # babashka (49)
- # beginners (56)
- # chlorine-clover (42)
- # cider (13)
- # clara (3)
- # cljfx (14)
- # clojure (65)
- # clojure-australia (2)
- # clojure-dev (12)
- # clojure-europe (57)
- # clojure-italy (10)
- # clojure-nl (3)
- # clojure-spec (25)
- # clojure-uk (25)
- # clojuredesign-podcast (11)
- # clojurescript (78)
- # code-reviews (16)
- # community-development (3)
- # cursive (14)
- # datomic (16)
- # depstar (20)
- # emacs (3)
- # figwheel-main (2)
- # fulcro (33)
- # helix (16)
- # jackdaw (15)
- # kaocha (13)
- # leiningen (3)
- # malli (33)
- # reveal (10)
- # shadow-cljs (29)
- # spacemacs (10)
- # sql (13)
I'm getting an error from spec that I don't understand. Maybe someone can help me.
The following example, which I've based on an example in https://clojure.org/guides/spec, works fine, and valid?
returns true
.
(s/valid? (s/keys :req [::first-name ::last-name ::email]
:opt [::phone])
{::first-name "Bugs"
::last-name "Bunny"
::email ""})
However, when I've programmatically generated the following list
(let [sss '(clojure.spec.alpha/keys
:req [:clojure-rte.genus-spec-test/first-name
:clojure-rte.genus-spec-test/last-name
:clojure-rte.genus-spec-test/email]
:opt [:clojure-rte.genus-spec-test/phone])]
(s/valid? sss
{::first-name "Bugs"
::last-name "Bunny"
::email ""}))
I get an uninformative exception
Execution error (ClassCastException) at (REPL:1).
null
is there a way to all s/valid?
with a programmatically generated spec?@jimka.issy An s-expression is not yet a spec. I would assume most people would write a macro for creating specs programmatically
Yes, but you need to understand more about how spec works
valid? works on spec objects
Spec forms need to be evaluated into spec objects first
In the case above, you could do that with eval
Even so, you’re still leaning on eval
Made some fun macros the other day:
(def kws (map keyword (repeatedly gensym)))
(defmacro cat [& preds]
`(clojure.spec.alpha/cat [email protected](interleave kws
(map expand-query preds))))
This allows you to write (g/cat int? string?)
, when you're not interested in the conformed valueSpec 2 has a lot more tools for all this stuff
That’s one
The wiki pages if you haven’t seen those
But not everything is in there and it’s missing a lot of the internal design
There is now an intermediate map data form now that you can work on directly
But will almost certainly change
Working with maps is a lot easier than writing macros (since macros beget more macros)
Some day rich or I will do a talk about the internals - there are several interesting aspects of it
macros wont help in this case. the origin of my spec is not sitting in an evaluation position.
thus, eval
evaluating a spec form gives you a spec object