Search code examples
clojuremalli

How do I access the properties assigned a key in a Malli map schema?


Given a Malli map schema like [:map [:key {:optional true} :int]], how do I get at the properties assigned the keyword :key in the map when writing a walker? In this example, these are {:optional true}.

As you can see in the below example the walker visits all the "values" of the map, in this case just the :int schema, and then out to the :map schema. Is there any way to get to the inbetween step, the "schema" and properties of :key?

(malli/walk                                                                                                   
  [:map [:key {:optional true} :int]]                                                                          
  (fn [schema path children options]                                                                           
    (println "Schema:" schema)                                                                                 
    (println "Path:" path)                                                                                     
    (println "Children:" children)                                                                             
    (println "Options:" options)                                                                               
    (println "Properties:" (malli/properties schema))                                                          
    (println))) => nil 


;; Schema: :int                                                                                                 
;; Path: [:key]                                                                                                  
;; Children: nil                                                                                                 
;; Options: nil                                                                                                  
;; Properties: nil                                                                                               
;;                                                                                                               
;; Schema: [:map [:key {:optional true} :int]]                                                                   
;; Path: []                                                                                                      
;; Children: [[:key {:optional true} nil]]                                                                       
;; Options: nil                                                                                                  
;; Properties: nil   

   

Solution

  • You need to pass the option :malli.core/walk-entry-vals to the walker function.

    (malli/walk
     [:map [:key {:optional true} :int]]
     (fn [schema path children options]
       (println "Schema:" schema)
       (println "Path:" path)
       (println "Children:" children)
       (println "Options:" options)
       (println "Properties:" (malli/properties schema))
       (println))
     {::malli/walk-entry-vals true})
    
    ;; Schema: :int
    ;; Path: [:key]
    ;; Children: nil
    ;; Options: #:malli.core{:walk-entry-vals true}
    ;; Properties: nil
    ;; 
    ;; Schema: [:malli.core/val {:optional true} :int]
    ;; Path: [:key]
    ;; Children: (nil)
    ;; Options: #:malli.core{:walk-entry-vals true}
    ;; Properties: {:optional true}
    ;; 
    ;; Schema: [:map [:key {:optional true} :int]]
    ;; Path: []
    ;; Children: [[:key {:optional true} nil]]
    ;; Options: #:malli.core{:walk-entry-vals true}
    ;; Properties: nil