Fork me on GitHub
#malli
<
2023-11-19
>
FlavaDave00:11:44

Hello, I would like to define a schema with namespaced keywords and then coerce a map that does not have namespaced keywords into a valid map with the appropriate namespaced keywords. Something like 👇 Can anyone help me come up with a good way to do this?

(ns super.cool.namespace
    (:require
     [malli.core :as mc]
     [malli.error :as me]))

  (def Schema
    [:map
     [::foo :string]
     [::bar :into]])

  (def data {:foo "clojure"
             :bar 2})

  (mc/coerce Schma data ?)
  => {:super.cool.namespace/foo "clojure"
      super.cool.namespace/bar 2}

ikitommi09:11:14

Maybe something like this?

ikitommi09:11:38

I think it would be simpler if it just would create a transforming map in :compile

{:foo ::foo, :bar ::bar}
… so, no :ns declaration would be needed.

yes 1
ikitommi09:11:06

maybe this could be a built-in transformer in malli, with both decode & encode?

FlavaDave22:11:57

Thanks @U055NJ5CC that example worked for me. I agreed that needing the :ns key isn't ideal. I think that having a map could be helpful if you want to do something articulate. But wouldn't it be best to just be able to get the namespace from the namespace of the key?