malli 2025-03-24

Is there a good way to get invalid-schema error explanations in CLJS land? I looked at the dev mode stuff but hit a bit of a dead end trying to get it to run in a shadow-cljs-supplied JS repl.

you should be able to call malli.error/humanize in cljs

the implementation is CLJC, as is most of malli

👍 1

there's also http://malli.dev.cljs, but I haven't used it

Thanks! Ill try it

Another probably stupidly simple question: doing a schema for a regular US postal address and want to assert there can be no :address3 if there is no :address2 on the root of the map. Any clues? do I just wrap the three scenarios in an :or?

:or and :fn are likely the alternatives. I'd probably use :or

So either

[:or
   [:map {:closed true}
    [:address1 :string]
    [:address2 :string]
    [:address3 :string]]
   [:map {:closed true}
    [:address1 :string]
    [:address2 :string]]
   [:map {:closed true}
    [:address1 :string]]]
[:and
   [:map
    [:address1 :string]
    [:address2 :string]
    [:address3 :string]]
   [:fn (fn [{:keys [address1 address2 address3]}]
        ;; your logic here
          )]]

🙏 1

^^^ is the solution today. You might be interested in watching this PR, which I'm hoping to wrap up soon https://github.com/metosin/malli/pull/1161 Will be:

[:and
 [:map
  [:address1 :string]
  [:address2 {:optional true} :string]
  [:address3 {:optional true} :string]]
 [:implies [:has :address3] [:has :address2]]]

👏 2

@ambrosebs golly, every time I have a malli issue you either think of a great way to solve it, or already have thought of it! Thanks!

❤️ 1

@valtteri aha, you know that concrete example of using a fn to do it just turned on many lightbulbs here!

👍 1