Fork me on GitHub
#malli
<
2022-01-19
>
ikitommi06:01:00

@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.

coltnz07:01:22

cool i'll do a PR

rovanion16:01:07

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}

Ben Sless16:01:50

How did you define coercion for the router?

rovanion16:01:48

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]}}

Ben Sless17:01:25

I think you need to specify it should use string coercion

rovanion17:01:45

I'm not sure I understand, where should I tell what what?

Ben Sless17:01:19

I'll dig up an example

Ben Sless17:01:28

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}}})

Ben Sless17:01:32

idea - use string coercion instead of json coercion by default for your params

rovanion09:01:52

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.

rovanion09:01:15

Now my "We are in the decoder" message is printed and all is fine!

Noah Bogart19:01:13

Is there a shorter form of

(def FxSchema
  (m/schema
    [:* [:or
         nil?
         [:tuple qualified-keyword?]
         [:tuple qualified-keyword? [:maybe any?]]]]))

Ben Sless20:01:20

or nil? T -> maybe T

Ben Sless20:01:34

[:sequence qualified-keyword? [:? any?]]

Ben Sless20:01:07

[:* [:maybe [:sequence qualified-keyword? [:? any?]]]]

Noah Bogart20:01:16

very nice, thank you

Noah Bogart20:01:38

hm, that doesn’t work because :sequential requires homogenous input. maybe i need :cat?

Noah Bogart20:01:13

that’s it: :sequential -> :cat works

Noah Bogart20:01:17

thanks for the help!

Ben Sless20:01:32

When in doubt, grab a catjam

😆 1