malli icon indicating copy to clipboard operation
malli copied to clipboard

Make it possible to add properties to an existing custom schema

Open Rovanion opened this issue 3 years ago • 0 comments

See the below code example for usage.

(def registry                                                                                               
  (atom {}))

(defn register! [type ?schema]                                                                              
  (swap! registry assoc type ?schema))

;; Combine the default registry with our own mutable registry.                                              
(mreg/set-default-registry!                                                                                 
 (mreg/composite-registry                                                                                   
    (mreg/fast-registry (malli/default-schemas))                                                            
    (mreg/mutable-registry registry)))


(register! :non-empty-string [:string {:min 1}])
(malli/validate :non-empty-string "Bengt")
;; => true                                                                                                  
                                                                                                            
(malli/validate [:map [:namn :non-empty-string]] {:namn "Bengt"})
;; => true                                                                                                  
(malli/validate [:map [:namn [:non-empty-string {:max 2}]]] {:namn "Bengt"})
;; Throws:                                                                                                  
;; Execution error (IllegalArgumentException) at malli.core/eval15223$fn$G (core.cljc:22).                  
;; No implementation of method: :-into-schema of protocol: #'malli.core/IntoSchema found for class: clojure\
.lang.PersistentVector                                                                                      

;; While this works:                                                                                        
(malli/validate [:map [:namn [:string {:max 2}]]] {:namn "Bengt"})

Rovanion avatar Mar 21 '22 07:03 Rovanion