Fork me on GitHub
#malli
<
2022-08-10
>
ikitommi07:08:19

getting back to computer this week from the long summer vacation, happy to see there the lively discussions here. if there is something clearly not resolved / answered by someone (🙇 for everyone for helping others!), you can mention me in the thread. will try to read the new new issues & prs this week.

❤️ 2
gratitude 2
clj 1
🚀 3
Akiz10:08:05

Hi, I need to change all keys in the schema from :keyword to :string. Is there an utility for this?

ikitommi11:08:27

what do you mean by keys here: map keys in values? or in schemas? something else? example would help

Akiz12:08:04

Sure. Here's an example. I would like to achieve that I don't have to define the output-schema manually. In this case it would be enough to replace keyword with string and vice versa. But It would be great if I could somehow use a transformer to transform only schemas.

(require '[malli.core :as m])
(require '[malli.transform :as mt])

(def input-schema [:map [:name :string] [:type :keyword]])
(def output-schema [:map [:name :string] [:type :string]])

(defn encode
  {:malli/schema [:=> [:cat input-schema] output-schema]}
  [val]
  (m/encode input-schema val mt/string-transformer))

Akiz12:08:54

Yes, this is exactly what I tried unsuccessfully to achieve via schema-walk. Thanks!

ikitommi12:08:21

Merged the error-value helpers into master. In case of validation error, you can get just the values in error and/or mask the valid values with something like , like expound does. Should be useful in pointing out (small set of) errors in huge values. feedback welcome, ping @mauricio.szabo:

(def Address
  [:map {:closed true}
   [:id :string]
   [:tags [:set :keyword]]
   [:numbers [:sequential :int]]
   [:address [:map
              [:street :string]
              [:city :string]
              [:zip :int]
              [:lonlat [:tuple :double :double]]]]])

(def address
  {:id "Lillan"
   :EXTRA "KEY"
   :tags #{:artesan "coffee" :garden "ground"}
   :numbers (list 1 "2" 3 4 "5" 6 7)
   :address {:street "Ahlmanintie 29"
             :zip 33100
             :lonlat [61.4858322, "23.7832851,17"]}})

(-> Address
    (m/explain address)
    (me/error-value {::me/mask-valid-values '...}))
;{:tags #{"coffee" "ground" ...},
; :numbers (... "2" ... ... "5" ... ...),
; :address {:lonlat [... "23.7832851,17"], :street ..., :zip ...},
; :EXTRA "KEY",
; :id ...}

ikitommi12:08:32

next thing would be to integrate this into pretty explaning. I think it would be good to mask out valid values by default? and the options being: 1. mask valid values (default) 2. show full values 3. don’t show value at all

ikitommi12:08:30

the tests also show how to do the same thing for individual errors. e.g. present errors one-by-one - like expound does.

mauricio.szabo13:08:52

Yes, I think masking valid values by default is good - it seems "prettier" this way, and makes for less noise IMHO

☝️ 1
ikitommi14:08:44

https://github.com/metosin/malli/pull/738, feedback and testing most welcome:

(malli.dev.pretty/explain
 [:map
  [:id :int]
  [:tags [:set :keyword]]
  [:address [:map
             [:street :string]
             [:city :string]
             [:zip :int]
             [:lonlat [:tuple :double :double]]]]]
 {:id "123"
  :EXTRA "KEY"
  :tags #{:artesan "coffee" :garden}
  :address {:street "Ahlmanintie 29"
            :city "Tampere"
            :zip 33100
            :lonlat [61.4858322, 23.7832851]}})
=>

👏 5
💯 2