Fork me on GitHub
#malli
<
2020-02-10
>
ikitommi16:02:18

started to add programmatic helpers to malli: https://github.com/metosin/malli/pull/172

4
ikitommi16:02:14

looks like:

(malli.util/get-in
  [:map {:title "test"}
   [:x [:vector
        [:list
         [:set
          [:sequential
           [:tuple int? [:map [:y [:maybe boolean?]]]]]]]]]]
  [:x 0 0 0 0 1 :y 0])
; => boolean?

ikitommi16:02:12

there is a new Protocol LookupSchema , which is implemented for :map, :multi, :vector, :list, :set, :sequential, :tuple.

ikitommi16:02:03

(malli.util/select-keys
  [:map {:title "map"}
   [:a int?]
   [:b {:optional true} int?]
   [:c string?]]
  [:a ::extra])
;[:map {:title "map"}
; [:a int?]
; [:b {:optional true} int?]]

borkdude16:02:37

so kinda like the new spec2 thing, where you can select from your schema in different contexts?

ikitommi16:02:07

well, almost. like in schema-tools, going to use the clojure.core functions names. There could be malli.util/select to do things like spec2 does.

ikitommi16:02:58

(-> [:map
     [:a int?]
     [:b string?]
     [:c [:map
          [:x int?]
          [:y keyword?]]]]
    (mu/select-keys [:a :c])
    (mu/update-in [:c] mu/dissoc :y))
;[:map
; [:a int?]
; [:c [:map
;      [:x int?]]]]

ikitommi16:02:23

that’s the thing we have been using with prismatic schema for 3+ years now, happy with that 🙂

ikitommi16:02:41

I’m not sure how the spec2 select works with nested sequential specs

ikitommi16:02:12

in malli, I want to keep things explicit: you need to walk those over yourself.

borkdude16:02:30

me neither, I kinda lost track on spec2

ikitommi16:02:51

LookupSchema
(-get [_ key default] (if (= 0 key) schema default)))

ikitommi16:02:29

^:--- for all sequential specs, all have just the 0

ikitommi16:02:53

also, planning to support schamas as maps, via a helper.

ikitommi17:02:58

[:vector {:min 0, :max 10} int?]
; =>
{:name :vector
 :properties {:min 0, :max 10}
 :children [int?]}

ikitommi17:02:29

there is already a walker that recursively transforms between formats.

ikitommi17:02:27

will push these extras into malli.util, so there is just the absolute minimum stuff in the core.

ikitommi21:02:41

should the m/name be m/type? In both React & json schema it's a type

ikitommi21:02:03

[:vector {:min 0, :max 10} int?]
; =>
{:type :vector
 :properties {:min 0, :max 10}
 :children [int?]}