malli

itaied 2024-12-15T07:56:22.707729Z

trying to wrap my head around some of malli concepts: 1. what are the differences of using :or and :alt ? i can express

(m/explain
  [:* [:cat string? [:alt string? boolean?]]]
  ["-server" "foo" "-verbose" 11 "-user" "joe"])
as
(m/explain
  [:* [:cat string? [:or string? boolean?]]]
  ["-server" "foo" "-verbose" 11 "-user" "joe"])
2. what are the differences (pros and cons) of attaching schemas to the registry as opposed to a var (using def) and reference it?
(def address
    [:map
     [:street :string]])

  (m/validate
   [:map
    [:id :int]
    [:address #'address]]
   {:id 2
    :address {:street "street"}})

  (m/validate
   [:map {:registry {::address [:map
                                [:street :string]]}}
    [:id :int]
    ::address]
   {:id 2 
    ::address {:street "street"}})

Ben Sless 2024-12-15T10:33:49.997189Z

IIRC sequence schemas get flattened while :or breaks that. Try nesting a cat under the alt and see what happens

itaied 2024-12-15T11:13:43.014739Z

thanks ill check it do you know if there's a recursive mu/optional-keys?

Ben Sless 2024-12-15T14:48:49.150069Z

Like a lens? No

opqdonut 2024-12-16T06:59:22.982279Z

I've written that util like 3 times now 😛

opqdonut 2024-12-16T07:04:23.009899Z

so why can't I find a copy of it anywhere...

opqdonut 2024-12-16T07:05:36.873909Z

here's one:

(defn make-all-keys-optional
  "Recursively apply mu/optional-keys to map schemas"
  [schema]
  (m/walk
   schema
   (m/schema-walker
    (fn [schema]
      (if (= :map (m/type schema))
        (mu/optional-keys schema)
        schema)))))

👍 1
opqdonut 2024-12-16T07:06:09.278229Z

and yeah, re: :or vs :alt, :alt is meant for seqex schemas, it corresponds to | in regex. :or is meant for general use