metosin / malli

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

malli.dev.pretty throws when explaining errors in nested maps #1096

Closed bgrabow closed 2 months ago

bgrabow commented 2 months ago

mdp/explain blows up when the schema expects keywords as map keys, but finds strings as map keys instead.

(malli.dev.pretty/explain [:map-of :keyword [:map-of :keyword :any]] {"foo" {:bar 1}})
Execution error (IllegalArgumentException) at malli.error/-masked$fn (error.cljc:194).
find not supported on type: java.lang.String
java.lang.IllegalArgumentException: find not supported on type: java.lang.String
    at clojure.lang.RT.find(RT.java:870)
    at clojure.core$find.invokeStatic(core.clj:1553)
    at clojure.core$find.invoke(core.clj:1549)
    at malli.error$_masked$fn__47144.invoke(error.cljc:194)
    at clojure.lang.PersistentArrayMap.kvreduce(PersistentArrayMap.java:429)
    at clojure.core$fn__8525.invokeStatic(core.clj:6909)
    at clojure.core$fn__8525.invoke(core.clj:6889)
    at clojure.core.protocols$fn__8257$G__8252__8266.invoke(protocols.clj:175)
    at clojure.core$reduce_kv.invokeStatic(core.clj:6920)
    at clojure.core$reduce_kv.invoke(core.clj:6911)
    at malli.error$_masked.invokeStatic(error.cljc:194)
    at malli.error$_masked.invoke(error.cljc:193)
    at malli.error$_masked$fn__47144.invoke(error.cljc:194)
    at clojure.lang.PersistentArrayMap.kvreduce(PersistentArrayMap.java:429)
    at clojure.core$fn__8525.invokeStatic(core.clj:6909)
    at clojure.core$fn__8525.invoke(core.clj:6889)
    at clojure.core.protocols$fn__8257$G__8252__8266.invoke(protocols.clj:175)
    at clojure.core$reduce_kv.invokeStatic(core.clj:6920)
    at clojure.core$reduce_kv.invoke(core.clj:6911)
    at malli.error$_masked.invokeStatic(error.cljc:194)
    at malli.error$_masked.invoke(error.cljc:193)
    at malli.error$error_value.invokeStatic(error.cljc:361)
    at malli.error$error_value.invoke(error.cljc:350)
    at malli.dev.pretty$eval47289$fn__47291.invoke(pretty.cljc:37)
    at clojure.lang.MultiFn.invoke(MultiFn.java:244)
    at malli.dev.virhe$_exception_doc.invokeStatic(virhe.cljc:178)
    at malli.dev.virhe$_exception_doc.invoke(virhe.cljc:176)
    at malli.dev.pretty$reporter$fn__47313.invoke(pretty.cljc:78)
    at malli.dev.pretty$prettifier$fn__47322.doInvoke(pretty.cljc:93)
    at clojure.lang.RestFn.invoke(RestFn.java:397)
    at malli.dev.pretty$explain.invokeStatic(pretty.cljc:99)
    at malli.dev.pretty$explain.invoke(pretty.cljc:95)
    at malli.dev.pretty$explain.invokeStatic(pretty.cljc:96)
    at malli.dev.pretty$explain.invoke(pretty.cljc:95)

Explanation works as expected when there is no further schema nesting below the problematic key:

(malli.dev.pretty/explain [:map-of :keyword :any] {"foo" 1})
-- Validation Error -------------------- NO_SOURCE_FILE:1 --

Value:

  {"foo" "foo"}

Errors:

  {"foo" ["should be a keyword"]}

Schema:

  [:map-of :keyword :any]

More information:

  https://cljdoc.org/d/metosin/malli/CURRENT

------------------------------------------------------------
=>
{:schema [:map-of :keyword :any],
 :value {"foo" 1},
 :errors ({:path [0], :in ["foo"], :schema :keyword, :value "foo", :message "should be a keyword"})}

Explanation also works as expected when the problematic key is at the leaf level of nested maps:

(malli.dev.pretty/explain [:map-of :keyword [:map-of :keyword :any]] {:foo {"bar" 1}})
-- Validation Error -------------------- NO_SOURCE_FILE:1 --

Value:

  {:foo {"bar" "bar"}}

Errors:

  {:foo {"bar" ["should be a keyword"]}}

Schema:

  [:map-of :keyword [:map-of :keyword :any]]

More information:

  https://cljdoc.org/d/metosin/malli/CURRENT

------------------------------------------------------------
ikitommi commented 2 months ago

Thanks for reporting, should be fixed in master.