metosin / malli

High-performance data-driven data specification library for Clojure/Script.
Eclipse Public License 2.0
1.51k stars 214 forks source link

Clarify and maybe change `unparse` behavior #1123

Open tomconnors opened 1 week ago

tomconnors commented 1 week ago

As posted on slack, data equal to the result of malli.core/parse cannot be unparsed.

(def int-or-string [:orn [:int :int] [:str :string]])

(malli.core/parse int-or-string 1) ;; -> [:int 1]

(= [:int 1] (malli.core/parse int-or-string 1)) ;; -> true

(malli.core/unparse int-or-string [:int 1]) ;=> :malli.core/invalid

(->> 1
     (malli.core/parse int-or-string)
     (malli.core/unparse int-or-string)) ;; => 1

I have some data in my schema's parsed format, which was not produced by parse, but which I would like to unparse. It appears that's not currently possible.

ikitommi commented 1 week ago

result of parse is actually a MapEntry. Clojure = matches it to vector of two elements. See https://github.com/metosin/malli/blob/master/src/malli/impl/util.cljc#L8-L9. But, I see this being a problem.

opqdonut commented 16 hours ago

To clarify, here's how to get the original example to work:

(malli.core/unparse int-or-string (malli.impl.util/-tagged :int 1)) ; => 1
;; OR
(malli.core/unparse int-or-string (clojure.lang.MapEntry. :int 1)) ; => 1

I agree that this is unwieldy. It would be easy to changed tagged? to accept vectors of size two, but I'm not sure what implications this would have. Are people passing in arbitrary data to unparse? Is there some possibility for confusion?

opqdonut commented 16 hours ago

After thinking about it for a bit, I think this is a safe change. I've prepared a PR (#1140).