Fork me on GitHub
#malli
<
2023-02-15
>
Luke Johnson15:02:29

Good day! Is there a way to pretty print a Malli schema? pprint is just one long line.

delaguardo16:02:37

malli schema is data so any library that offers pretty printing for clojure data would do. For example https://github.com/kkinnear/zprint

Luke Johnson16:02:27

I suppose I was asking if there was a pre-built function ready for my use. pprint works fine for some schemas in my REPL, but I think the property maps and reader tags cause problems. And I was hoping not to have to roll my own solution, but that may be where I’m headed. Thanks.

delaguardo16:02:09

Could you share an example when pprint doesn't work as you expected?

Luke Johnson16:02:46

Coming up with an example, it seems that mu/optional-keys is causing problems.

Luke Johnson16:02:51

Really big schemas also break pprint, I think.

delaguardo16:02:58

user=> (clojure.pprint/pprint (m/-form (mu/optional-keys (into [:map] (map (fn [ch] [(keyword (str (char ch))) :int]) (range (int \a) (int \z)))))))
[:map
 [:a {:optional true} :int]
 [:b {:optional true} :int]
 [:c {:optional true} :int]
 [:d {:optional true} :int]
 [:e {:optional true} :int]
 [:f {:optional true} :int]
 [:g {:optional true} :int]
 [:h {:optional true} :int]
 [:i {:optional true} :int]
 [:j {:optional true} :int]
 [:k {:optional true} :int]
 [:l {:optional true} :int]
 [:m {:optional true} :int]
 [:n {:optional true} :int]
 [:o {:optional true} :int]
 [:p {:optional true} :int]
 [:q {:optional true} :int]
 [:r {:optional true} :int]
 [:s {:optional true} :int]
 [:t {:optional true} :int]
 [:u {:optional true} :int]
 [:v {:optional true} :int]
 [:w {:optional true} :int]
 [:x {:optional true} :int]
 [:y {:optional true} :int]]
nil
mu/optional-keys returns custom schema type. There is m/-form function that turn the type to its data representation. This data is suitable for pprinting

Luke Johnson17:02:38

That’s really helpful! Thanks @U04V4KLKC

Ben Sless17:02:27

Did you try calling malli.core/form on it first?

Luke Johnson17:02:13

TIL: m/form is what I needed!

joshkh20:02:30

i’ve read through the docs and i’m sure that i missed the obvious 😇. how can i validate that a map’s keys are all keywords, and their values are all strings, given that the k/vs are unknown?

2
joshkh20:02:42

and like magic, i found it after asking. :map-of

🙂 2
😂 6
☝️ 2
Noah Bogart21:02:36

i love/hate when that happens. glad you found it!

joshkh21:02:19

story of my life 😎