Fork me on GitHub
#malli
<
2023-02-03
>
Noah Bogart02:02:25

Can I instrument outside code?

escherize17:02:13

like in another ns? not currently

Noah Bogart03:02:29

Looks like => would have to be modified to use the given symbols namespace, but otherwise works well! Maybe I’ll open a PR

🙏 2
Michael Gardner23:02:47

what's the right way to "unwrap" a :maybe in a map?

[:map [:x [:maybe :int]]] => [:map [:x :int]]
I can't quite figure out how to use malli.util/update(-in) to accomplish this. Do I need to use a walker instead?

ikitommi08:02:40

Just for one specific field or traversing the schemas and changing all maybe schemas?

ikitommi14:02:34

All schemas implement the LensSchema , so you can work in same way as with clojure.core + normal data: With clojure core:

(update {:x [:int]} :x get 0)
; => {:x :int}
With Malli:
(mu/update [:map [:x [:maybe :int]]] :x mu/get 0)
; => [:map [:x :int]
when implementing malli.util, there was a quick discussion what if we just implemented the Clojure Core protocols for Schemas, so you could use normal clojure core functions with them. So, this would work:
(update (m/schema [:map [:x [:maybe :int]]]) :x get 0)
; => [:map [:x :int]
… but it would have introduced problems when working with forms (which is just clojure data):
;; correct result when working with a schema
(update (m/schema [:maybe [:map [1 :int]]]) 0 get 1)
; => [:maybe :int]

;; wrong result when applied to form
(update [:maybe [:map [1 :int]]] 0 get 1)
;; => [nil [:map [1 :int]]]
… so decided to add dedicated helpers in malli.util for schemas

Michael Gardner18:02:53

nice, thank you. I've been wrapping my schemas with m/schema for fail-fast behavior in case of invalid schemas, but it does mean modifying them requires the m.util helpers

ikitommi14:02:34

All schemas implement the LensSchema , so you can work in same way as with clojure.core + normal data: With clojure core:

(update {:x [:int]} :x get 0)
; => {:x :int}
With Malli:
(mu/update [:map [:x [:maybe :int]]] :x mu/get 0)
; => [:map [:x :int]
when implementing malli.util, there was a quick discussion what if we just implemented the Clojure Core protocols for Schemas, so you could use normal clojure core functions with them. So, this would work:
(update (m/schema [:map [:x [:maybe :int]]]) :x get 0)
; => [:map [:x :int]
… but it would have introduced problems when working with forms (which is just clojure data):
;; correct result when working with a schema
(update (m/schema [:maybe [:map [1 :int]]]) 0 get 1)
; => [:maybe :int]

;; wrong result when applied to form
(update [:maybe [:map [1 :int]]] 0 get 1)
;; => [nil [:map [1 :int]]]
… so decided to add dedicated helpers in malli.util for schemas