Fork me on GitHub
#malli
<
2022-09-01
>
souenzzo12:09:45

Hello In the context of this issue: https://github.com/metosin/malli/issues/739#issuecomment-1232938081 Should :my/int represent something different from [:my/int] ? or they both different notations with the same meaning? This behavior can be considered a bug?

(m/ast :my/int opts) #_#_=> {:type :malli.core/schema, :value :my/int}
(m/ast [:my/int] opts) #_#_=> {:type :my/int}

ikitommi12:09:30

oh, doesn’t look right. works the same, but extra wrapping :thinking_face:

ikitommi12:09:32

need to think about the issue, thanks for a gentle reminder 🙂

souenzzo12:09:38

the extra wrapping makes one turn into $ref and another turn into a simple type, in json-schema generation.

souenzzo12:09:26

I'm unsure if I should consider it a json-schema bug, or a malli bug.

☝️ 1
Justin Reed22:09:23

How do I modify this:

[:map
   [:key-0 uuid?]
   [:key-1 string?]
   [:key-2 [:string {:max 9 :min 9}]]
such that a map must have one of these three keys? For example,
{:key-0 (UUID/randomUUID)}
{:key-1 "HELLO WORLD"}
{:key-2 "123456789"}
{:key-1 "HELLO WORLD" :key-2 "123456789"} 
are all valid, but
{}
{:key-4 "SOME OTHER KEY"}
are not. I didn't find a direct way of doing this in the docs, so I thought I'd ask before I ventured down the path of cobbling it together with :and, :or, etc.

iarenaza10:09:36

While it's not explicitly described as such, there is a hint about that possibility in the example shown in the paragraph with the text "Finding all subschemas with paths, retaining order:" There, the :fn schema is used to check that either the :streeet or the :lonlat keys are present in the :map schema (using the :and schema to tie both conditions). So something like this should work:

(mapv (fn [m]
        (malli/validate [:and
                         [:map
                          [:key-0 {:optional true} uuid?]
                          [:key-1 {:optional true} string?]
                          [:key-2 {:optional true} [:string {:max 9 :min 9}]]]
                         [:fn (fn [{:keys [key-0 key-1 key-2]}]
                                (or key-0 key-1 key-2))]]
                        m))
      [{:key-0 (UUID/randomUUID)}
       {:key-1 "HELLO WORLD"}
       {:key-2 "123456789"}
       {:key-1 "HELLO WORLD" :key-2 "123456789"}
       {}
       {:key-4 "SOME OTHER KEY"}])

emccue15:09:45

[:or 
  [:map {:closed true}
    [:key-0 uuid?]]
  [:map {:closed true}
    [:key-1 string?]]
  [:map {:closed true}
    [:key-2 [:string {:max 9 :min 9}]]
  [:map {:closed true}
    [:key-0 uuid?]
    [:key-1 string?]]
  [:map {:closed true}
    [:key-1 string?]
    [:key-2 [:string {:max 9 :min 9}]]]
  [:map {:closed true}
    [:key-0 uuid?]
    [:key-2 [:string {:max 9 :min 9}]]]
  [:map {:closed true}
    [:key-0 uuid?]
    [:key-1 string?]
    [:key-2 [:string {:max 9 :min 9}]]]
  

emccue15:09:02

is my first go at it - obviously you'd write something that would produce that

Justin Reed17:09:10

Thank you both for your help. I was about to go down the road @U3JH98J4R demonstrated, but was hoping for something less redundant. @U8T05KBEW’s nudge is exactly what I was looking for.