This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-11-22
Channels
- # announcements (39)
- # architecture (9)
- # aws (2)
- # babashka (17)
- # beginners (73)
- # calva (6)
- # cider (27)
- # clj-kondo (140)
- # cljdoc (67)
- # cljsrn (1)
- # clojure (99)
- # clojure-dev (4)
- # clojure-europe (35)
- # clojure-nl (7)
- # clojure-spec (19)
- # clojure-uk (2)
- # clojurescript (40)
- # community-development (3)
- # cursive (10)
- # datalevin (2)
- # datavis (2)
- # datomic (27)
- # deps-new (5)
- # events (2)
- # fulcro (38)
- # integrant (6)
- # jobs (3)
- # keyboards (1)
- # leiningen (13)
- # lsp (3)
- # malli (10)
- # meander (5)
- # membrane (1)
- # membrane-term (9)
- # missionary (3)
- # off-topic (29)
- # polylith (3)
- # reagent (3)
- # reitit (5)
- # remote-jobs (2)
- # reveal (7)
- # shadow-cljs (20)
- # tools-build (4)
- # tools-deps (8)
- # vim (10)
- # xtdb (3)
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"}]]
?
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]])
In most cases the meaning depends on the context. I think it should be given by the key .
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
Thank you @U055NJ5CC for adding a section about string dependent types in malli documentation.
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.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
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 decodersIt 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
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