Fork me on GitHub
#malli
<
2021-11-22
>
Yehonathan Sharvit13:11:33

Inside a map, should metadata like title and description be on the field key or in the field value? For instance: [:map [:id {:title "The ID"} :string]] or [:map [:id [:string {:title "The ID"}]] ?

ikitommi17:11:53

both work, up to you. If the values are shared, pushing the properties into value seem like a better idea, e.g.

(def id (m/schema [:string {:title "The ID"}]))

(m/schema [:map [:id id]])

Yehonathan Sharvit03:11:57

In most cases the meaning depends on the context. I think it should be given by the key .

ikitommi17:11:00

about the a/b thing… agree with Ben and that’s how we have used it. the actual domain model could be defined as :multi and there would be encode & decode to&from the string-domain. Actual domain-side would be something like: https://github.com/metosin/malli/blob/master/docs/tips.md#dependent-string-schemas

Yehonathan Sharvit08:11:22

Thank you @U055NJ5CC for adding a section about string dependent types in malli documentation.

respatialized22:11:02

I'm wondering if there's something I'm not quite understanding about how m/decode and transformers work in the context of :or:

(m/decode
   [:cat {:decode/get {:leave second}} [:= :txt] :string]
   [:txt "something"]
   (mt/transformer {:name :get}))
; => "something"

(m/decode
   [:or [:cat {:decode/get {:leave second}} [:= :txt] :string]
    :double]
   [:txt "something"]
   (mt/transformer {:name :get}))
; => [:txt "something"]
what I'd like is for the :or to decode its value using the decoder provided by the matching schema. I had assumed that decoding was recursive by default, but it doesn't seem to be working in the context of the subschema here.

Ben Sless05:11:46

I think if you really wanted correct results here you had to write some sort of backtracking search algorithm and end up implementing half of minikanren

😬 1
respatialized15:11:13

interestingly, encode does the right thing here:

(m/encode
   [:or [:cat {:encode/get {:leave second}} [:= :txt] :string]
    :double]
   [:txt "something"]
   (mt/transformer {:name :get}))
;; => "something"
what I may have been confused about was just the respective purposes of encoders and decoders

ikitommi21:11:55

It works as expected, despite it looks odd. • Decode is a process of turning (potentially invalid) values from external format (e.g. JSON) into values that are valid in the default/clojure domain. • Encode is the opposite: turning valid values from default domain into (potentially invalid) values in another domain (e.g. JSON). here, your decoder is not working correctly, as it emits invalid values. The :or picks the first branch that produces valid values after the transformation. In the example, both paths produce invalid value, so the original value is returned. See the impl: https://github.com/metosin/malli/blob/master/src/malli/core.cljc#L713-L724

☑️ 1
ikitommi21:11:55

It works as expected, despite it looks odd. • Decode is a process of turning (potentially invalid) values from external format (e.g. JSON) into values that are valid in the default/clojure domain. • Encode is the opposite: turning valid values from default domain into (potentially invalid) values in another domain (e.g. JSON). here, your decoder is not working correctly, as it emits invalid values. The :or picks the first branch that produces valid values after the transformation. In the example, both paths produce invalid value, so the original value is returned. See the impl: https://github.com/metosin/malli/blob/master/src/malli/core.cljc#L713-L724

☑️ 1