Fork me on GitHub
#malli
<
2022-03-17
>
rovanion07:03:59

Does Malli happen to have a shorthand for making a map out of already defined value-specs like clojure.spec's (s/keys :req-un [:thing/omabob])?

ikitommi07:03:29

[:map {:registry {"kikka" :int, "kukka" :string}} "kikka" "kukka"]

ikitommi07:03:08

e.g. if you have a schema in a registry, you can use the name instead of full entry definition.

rovanion08:03:56

Right, thanks. And if I wanted to make the key in the map unqualified while the entry in the registry is qualified by a namespace, any way around that?

rovanion08:03:25

Well, [:key :ns/key] works of course.

ikitommi10:03:37

yes, and you can make add entry props too: [:map [::id {:optional true}]].

ikitommi10:03:24

not sure was adding all that sugar a good idea, made the parser more complex - and slower

rovanion13:03:39

Key/entry props I do use, so I appreciate them.

rovanion13:03:50

Is it possible to add properties to an already existing custom type?

(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"})

ikitommi16:03:23

currently no, but would be a ~1 line change, haven’t thought of that use case… please write an issue,