zprint icon indicating copy to clipboard operation
zprint copied to clipboard

Formatting for malli schemas

Open bolivier opened this issue 6 months ago • 5 comments

I haven't been able to find a good way to format my malli schemas.

What I'd like to do is have a macro like defschema that (for now) just aliases def and tell zprint that it should only manage indentation there. This doesn't throw an error, but it doesn't respect my newlines either. Any ideas?

:fn-map {"defschema" [:arg1-body {:style :indent-only}]}

bolivier avatar Jun 24 '25 21:06 bolivier

My mistake, this handled it:

"defschema" [:arg1-body {:style :indent-only
                                   :vector {:style :indent-only}}]

bolivier avatar Jun 24 '25 21:06 bolivier

I was wrong, this does not work.

bolivier avatar Jun 24 '25 22:06 bolivier

Thanks for asking. It would help me to come up with something that would meet your needs if you gave me a couple of examples of your malli schemas, formatted the way that you would like to see zprint format them. Thanks!On Jun 24, 2025, at 5:46 PM, Brandon Olivier @.***> wrote:bolivier created an issue (kkinnear/zprint#359) I haven't been able to find a good way to format my malli schemas. What I'd like to do is have a macro like defschema that (for now) just aliases def and tell zprint that it should only manage indentation there. This doesn't throw an error, but it doesn't respect my newlines either. Any ideas? :fn-map {"defschema" [:arg1-body {:style :indent-only}]}

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

kkinnear avatar Jun 25 '25 00:06 kkinnear

@kkinnear thanks for responding!

I don't know if there's a way to really detect which vectors are schemas and which are hiccup or plain data. Seems like a highly contextual thing, which is why I created an alias of def to try it.

Mostly I think they're formatted like

[:map ;; or :enum or something
  [:key :value {:opts? true}]]

Something like

[:arg1-body {:vector {:style :arg1-body 
                      :vector :triplet}}]

I don't totally understand the format of those options, so that's probably not right, but something like that is what I'd want.

That said, I'd also be perfectly happy to just not have them automatically formatted, since there are a lot of specifics for one library, and I sometimes like throwing additional space in there for clarity.

bolivier avatar Jun 25 '25 13:06 bolivier

My apologies, I wrote this weeks ago, but apparently never actually pushed the "comment" button. Sorry about that!!


Thanks for the input. I looked at malli, which (now that you mention it), I have seem before. Not a lot of clear examples to work from, but here is what I tried out. I like your idea of defining defschema so that you can use the :fn-map to tell zprint how to format it differently than a normal vector.

In the simple examples I tried, I found that the :style :hiccup seemed to replicate the examples in the malli respository reasonably well. If you want additional blank lines, you can add :style :respect-nl. Which a lot like :indent-only unless the lines are really long. :indent-only doesn't combine well with other things, so I would try to avoid that if you can. :respect-nl does almost the same thing, but does combine fine with other things.

Here is one example:

zprint.core=> (print i359d)
(defschema Address
  [:map
   [:id :string]
   [:tags [:set :keyword]]
   [:address

    [:map

     [:street :string]
     [:city :string]
     [:zip :int]
     [:lonlat [:tuple :double :double]]]]])
nil
zprint.core=> (czprint i359d {:parse-string? true  :fn-map {"defschema" [:arg1-body {:style [:hiccup :respect-nl]}]}})
(defschema Address
  [:map
   [:id :string]
   [:tags [:set :keyword]]
   [:address

    [:map

     [:street :string]
     [:city :string]
     [:zip :int]
     [:lonlat [:tuple :double :double]]]]])
nil
zprint.core=> (czprint i359d {:parse-string? true  :fn-map {"defschema" [:arg1-body {:style [:hiccup]}]}})
(defschema Address
  [:map
   [:id :string]
   [:tags [:set :keyword]]
   [:address
    [:map
     [:street :string]
     [:city :string]
     [:zip :int]
     [:lonlat [:tuple :double :double]]]]])
nil
zprint.core=> (czprint i359d {:parse-string? true  :fn-map {"defschema" [:arg1-body {}]}})
(defschema Address
  [:map [:id :string] [:tags [:set :keyword]]
   [:address
    [:map [:street :string] [:city :string] [:zip :int]
     [:lonlat [:tuple :double :double]]]]])

The last one shows what you get with nothing special, which isn't what you want. I think you might find the first one, with :style [:hiccup :respect-nl], to be the most like what I think you are looking for.

Maybe give that a try, and if it doesn't work quite right, put the example that doesn't work here and show me how you would like it to be formatted.

kkinnear avatar Jul 23 '25 16:07 kkinnear