如何访问在 Malli 映射模式中分配了键的属性?

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

给定像 [:map [:key {:optional true} :int]] 这样的 Malli 地图架构,我如何在编写 walker 时获取地图中分配给关键字 :key 的属性?在此示例中,这些是 {:optional true}.

正如您在下面的示例中看到的,walker 访问地图的所有“值”,在本例中只是 :int 模式,然后访问 :map 模式。有什么方法可以到达中间步骤,即 :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   

   

您需要将选项 :malli.core/walk-entry-vals 传递给 walker 函数。

(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