This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-12-11
Channels
- # adventofcode (129)
- # architecture (10)
- # beginners (163)
- # boot (1)
- # cider (34)
- # cljs-dev (9)
- # clojure (210)
- # clojure-austin (11)
- # clojure-czech (2)
- # clojure-gamedev (1)
- # clojure-greece (67)
- # clojure-italy (2)
- # clojure-russia (8)
- # clojure-spec (36)
- # clojure-uk (54)
- # clojurescript (87)
- # cursive (12)
- # data-science (6)
- # datomic (13)
- # devcards (4)
- # editors (2)
- # emacs (34)
- # figwheel (6)
- # fulcro (147)
- # graphql (17)
- # lumo (54)
- # off-topic (37)
- # om (11)
- # onyx (7)
- # parinfer (10)
- # random (1)
- # re-frame (13)
- # ring (10)
- # ring-swagger (2)
- # sfcljs (1)
- # shadow-cljs (1)
- # spacemacs (32)
- # test-check (4)
- # unrepl (84)
Hi, guys. Can someone provide an example how to use spec for custom scalars?
I need to write {:parse-scalar1 #(s/conform ::my-spec %)}
or exist some more convenient way in lacinia?
Yep, I already read it, but it looks strange to write something like (schema/as-conformer #(s/conform ::my-spec %))
so then you could just assert that it passes your spec maybe, then do the transformation logic after?
Look. I already have a spec. And I can easily use it for conforming #(s/conform ::my-spec %)
It also returns invalid-blabla in case it can't parse %. If lacinia requires conformers maybe exist some simple way to use a spec instead of conformer, because it looks so stupid to do (schema/as-conformer #(s/conform ::my-spec %))
(defn as-conformer
"Creates a clojure.spec/conformer as a wrapper around the supplied function.
The function is only invoked if the value to be conformed is non-nil.
Any exception thrown by the function is silently caught and the returned conformer
will return :clojure.spec/invalid or a [[coercion-failure]]."
[f]
(s/conformer
(fn [x]
(try
(when (some? x)
(f x))
(catch Exception e
(if-some [message (.getMessage e)]
(coercion-failure message (ex-data e))
::s/invalid))))))
I could imagine a wrap-conformer
that could accomplish the same work, but invoke (s/conform spec x)
instead of (f x)
.Ok, thanks for information, will write it on my own.