Fork me on GitHub
#malli
<
2023-03-17
>
ikitommi08:03:28

Add support for default/fallback branch for :map (https://github.com/metosin/malli/pull/871) merged in master. Test/user reports welcome!

ikitommi08:03:23

this is a nice bonus:

(m/decode 
 [:map-of :int :int] 
 {1 1, 2 "2", "3" 3, "4" "4"} 
 (mt/strip-extra-keys-transformer))
; => {1 1}

escherize16:03:19

my colleague showed me some interesting behavior with decode. It has to do with what happens when a transformer throws. minimal example:

(mc/decode :sequential "32" (mtx/string-transformer))
It cropped up during a usage with an :or like this:
(mc/decode [:or :sequential :int] "32" (mtx/string-transformer))
Which you’d expect would return 32, but no. it throws:
clojure.lang.ExceptionInfo: :malli.core/child-error
{:type :malli.core/child-error, :message :malli.core/child-error, :data {:type :sequential, :properties nil, :children nil, :min 1, :max 1}}
 at malli.core$_exception.invokeStatic (core.cljc:138)
    malli.core$_exception.invoke (core.cljc:136)
It’s probably best if a transformer can never throw 1️⃣, but at least we need to handle exceptions in the parent schema 2️⃣ or both 3️⃣. Update: Just needed a child for the :sequential schema. the exception is getting thrown on schema creation.

ikitommi17:03:21

I believe it's the schema creation: try (m/schema :sequential) , it requires 1 child, e.g (m/schema [:sequential :any]). m/decode coerces the data syntax into schema.

escherize18:03:49

Good call, I should have read the error message more carefully.

danielneal21:03:07

heya, I’m trying to build a custom simple-schema using the example in the docs.

(def Between
  (malli/-simple-schema
    {:type `Between
     :compile (fn [_properties [min max] _options]
                (when-not (and (int? min) (int? max))
                  (malli/-fail! ::invalid-children {:min min, :max max}))
                {:pred #(and (int? %) (>= min % max))
                 :min 2 ;; at least 1 child
                 :max 2 ;; at most 1 child
                 :type-properties {:error/fn (fn [error _] (str "should be betweeb " min " and " max ", was " (:value error)))}})}))
However I get a “child error”, when I try to use it
(malli/form [Between 10 20])

 :malli.core/child-error
   {:type :malli.core/child-error, :message :malli.core/child-error, :data {:type sqa.api-reitit.filter/Between, :properties nil, :children [10 20], :min 0, :max 0}}
It looks like malli.core/-simple-schema is not getting the correct min and max values - it expects :max and :min at the top level of the map passed to it - but I changed that and got some other errors using the schema so there may be some other things I’m missing

2
danielneal21:03:07

oh, I guess it's bad timing, this has just changed on master but unreleased

👍 2