This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-01-19
Channels
- # announcements (14)
- # babashka (16)
- # beginners (81)
- # calva (18)
- # chlorine-clover (8)
- # cider (4)
- # clj-kondo (11)
- # cljdoc (4)
- # cljsrn (5)
- # clojure (163)
- # clojure-europe (30)
- # clojure-nl (3)
- # clojure-spec (7)
- # clojure-uk (4)
- # cursive (3)
- # emacs (12)
- # events (1)
- # figwheel-main (3)
- # fulcro (60)
- # introduce-yourself (3)
- # jackdaw (12)
- # juxt (3)
- # lsp (77)
- # malli (23)
- # mid-cities-meetup (9)
- # music (2)
- # off-topic (12)
- # portal (6)
- # releases (2)
- # shadow-cljs (3)
- # spacemacs (7)
- # sql (1)
- # tools-deps (2)
- # vim (3)
@coltnz no at the moment. But we could add an option to mt/default-value-transformer
to fill those, here’s the code for it: https://github.com/metosin/malli/blob/master/src/malli/transform.cljc#L394. Issue & PR welcome.
Anyone ever had their data thrown away during coercion in reitit when using their own custom malli schema type? I've defined mine like:
(defn- -string-gen [{:keys [min max]}]
(cond
(and min (= min max)) (gen/fmap string/join (gen/vector gen/char min))
(and min max) (gen/fmap string/join (gen/vector gen/char min max))
min (gen/fmap string/join (gen/vector gen/char min (* 2 min)))
max (gen/fmap string/join (gen/vector gen/char 0 max))
:else gen/string))
(def postgres-string
"Postgres does not allow the null byte \0 in strings.
The predicate only forbids null bytes, but the generator only generates ascii characters. This
severely limits the range of test strings. The latter should be changed at some later point."
(malli/-simple-schema
(fn [opts _]
{:type :postgres/string
:pred #(and (string? %) (not (string/includes? \0 %)))
:type-properties {:error/message "Unable to decode postgres string"
:decode/string (fn [x] (prn "We are in the decoder") x)
:json-schema/type "string"
:json-schema/format "string"
:json-schema/minimum 0
:gen/gen (-string-gen opts)}})))
But when defining a schema with it like
(def kasse
(malli/schema
[:map
[:odlingsplats [postgres-string {:foreign-key "odlingsplatser"}]]
[:diameter_m int?]
[:djup_m int?]]))
and then running a request through my router like
(tove.handler/app-routes {:request-method :post, :uri "/kassar/-/new" :form-params {:odlingsplats "En bra plats" :diameter_m "1" :djup_m "1"}})
the data is without any error conformed into
{:diameter_m 1, :djup_m 1}
If I replace postgres-string
with string?
in the schema the data gets passed on and the conformed map looks like:
{:odlingsplats "En bra plats", :diameter_m 1, :djup_m 1}
Here's the relevant section of the router:
["/-/new" {:post {:handler (partial page/create-entry! "kassar" "id")
:coercion mc/coercion
:parameters {:form s/kasse-user-data}}}]]
I also have this at the very end of the router definition:
{:data {:middleware [rrc/coerce-request-middleware]}}
I think you need something like
(mcoercion/create
{:transformers
{:body
{:default string-transformer-provider
:formats {"application/json" string-transformer-provider}}
:string
{:default string-transformer-provider}
:response
{:default mcoercion/default-transformer-provider}}})
What seems to have worked is that I set up my own spec-style mutable registry that I set as the default registry. And by referring to the type as a key in kasse instead of as a var it all worked out.
Is there a shorter form of
(def FxSchema
(m/schema
[:* [:or
nil?
[:tuple qualified-keyword?]
[:tuple qualified-keyword? [:maybe any?]]]]))
very nice, thank you
hm, that doesn’t work because :sequential
requires homogenous input. maybe i need :cat
?
that’s it: :sequential
-> :cat
works
thanks for the help!