malli

richiardiandrea 2025-01-27T23:08:17.710049Z

Hi again 😄 What would be the best way to transform keys of [:map ...] schema? I found m/walk but I don't think you can return modified keys as it is a postwalk but does not visit them, correct?

richiardiandrea 2025-01-28T15:10:06.240049Z

I did end up using clojure.walk as ::m/walk-entry-valsstill processes the "schema" side of an entry and not the key itself

richiardiandrea 2025-01-28T15:13:00.533959Z

(cwalk/postwalk (fn [node] (if (vector? node) (let [k (first node)] (if (= "TEST" (namespace k)) (into [(csk/->camelCaseKeyword (name (first node)))] (rest node)) node)) node)) schema)

neural 2025-01-27T23:24:01.329489Z

Is this what you want? https://github.com/metosin/malli?tab=readme-ov-file#programming-with-schemas

richiardiandrea 2025-01-27T23:25:27.243679Z

I'll take a look but basaicaally I would like to transform this [:map [:foo-bar :string]] into this [:map {:FooBar :string]]

richiardiandrea 2025-01-27T23:28:25.702379Z

probably I need some custom clojure.walkfor that

neural 2025-01-27T23:29:18.908909Z

In the link, I think the 3rd example is what you want.

richiardiandrea 2025-01-27T23:29:59.244559Z

you mean mu/update-in Address [:address] mu/assoc :country [:enum "fi" "po"] ?

neural 2025-01-27T23:30:29.494629Z

after that

richiardiandrea 2025-01-27T23:30:51.956049Z

I must be blind 😄

neural 2025-01-27T23:31:06.987989Z

😆

neural 2025-01-27T23:32:53.625129Z

I haven't played with malli in a while. But if this doesn't help, ask again, and I think someone can answer.

2025-01-28T00:58:41.023419Z

I think the ::m/walk-entry-vals walk option could be useful. you can update the keys in a map entry using it.

opqdonut 2025-01-28T05:41:16.642209Z

for a non-recursive transformation it might be easier to just use mu/keys, mu/dissoc and mu/assoc

user=> (def schema [:map [:foo :int] [:bar :string]])
#'user/schema
user=> (mu/keys schema)
(:foo :bar)
user=> (reduce (fn [s k] (-> s
                             (mu/dissoc k)
                             (mu/assoc (keyword (.toUpperCase (name k))) (mu/get s k))))
               schema
               (mu/keys schema))
[:map [:FOO :int] [:BAR :string]]