metosin / malli

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

`:multi` schema throws cryptic error when a string is used as a dispatch key #918

Open DeLaGuardo opened 12 months ago

DeLaGuardo commented 12 months ago

I'm trying to write multi schema to validate a map with strings as a key:

  (def valid?
    (m/validator
     [:multi {:dispatch "type"}
      ["object" [:map-of :keyword :string]]
      [::m/default :string]]))

Here is an exception thrown:

1. Caused by clojure.lang.ExceptionInfo
   :malli.core/sci-not-available
   {:type :malli.core/sci-not-available,
    :message :malli.core/sci-not-available,
    :data {:code "type"}}
                 core.cljc:  138  malli.core$_exception/invokeStatic
                 core.cljc:  136  malli.core$_exception/invoke
                 core.cljc:  142  malli.core$_fail_BANG_/invokeStatic
                 core.cljc:  140  malli.core$_fail_BANG_/invoke
                 core.cljc: 2346  malli.core$eval13299$_fail_BANG___13300/invoke
                 core.cljc: 2355  malli.core$eval13299$eval__13306/invoke
                 core.cljc: 1544  malli.core$_multi_schema$reify__12949/_into_schema
                 core.cljc: 1992  malli.core$into_schema/invokeStatic
                 core.cljc: 1983  malli.core$into_schema/invoke
                 core.cljc: 2052  malli.core$schema/invokeStatic
                 core.cljc: 2034  malli.core$schema/invoke
                 core.cljc: 2116  malli.core$validator/invokeStatic
                 core.cljc: 2110  malli.core$validator/invoke
                 core.cljc: 2114  malli.core$validator/invokeStatic
                 core.cljc: 2110  malli.core$validator/invoke
,,,

It looks like a string interpreted as an SCI expression, which is a bit surprising.

How about adding :dispatch-key property for :multi schema? This prop could be mutually exclusive with :dispatch but when present will be treated as a literal key to dispatch by. This allows to dispatch by any key, including string, var, object etc.

ikitommi commented 10 months ago

ok, could be improved via throwing better exception via m/fail!