Fork me on GitHub
#malli
<
2022-12-14
>
rmxm16:12:41

still I have another pickle... so when I run explain-data, I can obtain all error messages into a sec. however, when I define :fn it doesnt put error as "last" as mentioned before, I will paste the real code, the gist of the problem is that map with :error/message is at different index of the vector

rmxm16:12:06

(def schema
 [map
  [:x [:fn #(= % 2) {:error/message "not 2"}]]
  [:y [:string {:min 2 :error/message "longer than 2}]])

(def payload
 {:x 3
  :y "a"})

(->> (mu/explain-data schema payload)
     :errors
     (map #(-> % :schema last :error/message)))

ikitommi17:12:14

not sure what you are doing, but:

(def schema
  [:map
   [:x [:fn {:error/message "not 2"} #(= % 2)]]
   [:y [:string {:min 2 :error/message "longer than 2"}]]])

(def payload
  {:x 3
   :y "a"})

(->> (mu/explain-data schema payload)
     :errors
     (map #(-> % :schema m/properties :error/message)))
; => ("not 2" "longer than 2")

🙌 1
ikitommi17:12:32

or you can use something like me/humanize:

(->> (m/explain schema payload)
     (me/humanize))
; => {:x ["not 2"], :y ["longer than 2"]}

rmxm17:12:18

great thanks... well I am trying to just aggregate error strings to push them as notifications... those examples work, thanks

👍 1
escherize19:12:11

I want to decode a string with an enum like so: [:enum :A :B] + "A" => :A This doesn’t work:

(mc/decode [:enum :A :B] "A" (mtx/string-transformer))
;; => "A"
But all of these will work:
[(mc/decode [:and keyword? [:enum :A :B]]            "A" (mtx/json-transformer))
 (mc/decode [:and keyword? [:enum :A :B]]            "A" (mtx/string-transformer))
 (mc/decode [:enum {:decode/specific keyword} :A :B] "A" (mtx/transformer {:name :specific}))]
;;=> [:A :A :A] 
Is there another (even better?) way to do that?

Ben Sless19:12:43

decode/string instead of specific?

ikitommi19:12:43

not released, but https://github.com/metosin/malli/pull/782 last month 😎

(m/decode [:enum :A :B] "A" (mt/string-transformer))
;=> :A

escherize19:12:00

sorry @UK0810AQ2, I can’t parse that.

escherize19:12:06

Is that coming out in a release soon? (aka when?)

ikitommi19:12:29

if you use deps, you can just point to the latest commit on master.

👏 1
ikitommi19:12:29

will try to cut a release this year, need some work on the providers, a minor breaking change, so a minor bump to 0.10.0

escherize19:12:56

I think I’ll wait for that, then. Thanks for the info!

👍 1
escherize22:12:13

Is there an analogous function to prismatic schema’s check? Here’s the docstring:

"Return nil if x matches schema; otherwise, returns a value that looks like the
   'bad' parts of x with ValidationErrors at the leaves describing the failures.

   If you will be checking many datums, it is much more efficient to create
   a 'checker' once and call it on each of them."

escherize22:12:36

hmm, is that a key in explain?

escherize22:12:48

I have this to work with:

{:schema [:map [:x int?]],
 :value {:y "3"},
 :errors ({:path [:x], :in [:x], :schema [:map [:x int?]], :value nil, :type :malli.core/missing-key})}

escherize22:12:24

seems like I can reduce on errors, and assoc-in using :path or :in?

escherize22:12:00

answering my own question here, using (me/humanize (m/errors $schema $value)) does similar thing to plumatic.schema/check