Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

me/-resolve-root-error does not work with :enum #553

Closed
tomasd opened this issue Oct 26, 2021 · 3 comments · Fixed by #561
Closed

me/-resolve-root-error does not work with :enum #553

tomasd opened this issue Oct 26, 2021 · 3 comments · Fixed by #561
Labels
bug Something isn't working help wanted Help most welcome

Comments

@tomasd
Copy link
Contributor

tomasd commented Oct 26, 2021

This snippet always fails in 0.6.2:

(-> [:map
     [:a [:enum "a" "b"]]]
    (m/explain {:a nil})
    (me/humanize {:resolve me/-resolve-root-error}))
Execution error (ExceptionInfo) at malli.core/-fail! (core.cljc:119).
:malli.core/invalid-schema {:schema "a"}

It works with -resolve-direct-error:

(-> [:map
     [:a [:enum "a" "b"]]]
    (m/explain {:a nil})
    (me/humanize ))
=> {:a ["should be either a or b"]}
@tomasd
Copy link
Contributor Author

tomasd commented Oct 26, 2021

The problem is in this line https://github.com/metosin/malli/blob/master/src/malli/error.cljc#L247 :

(error-message {:schema (mu/get-in schema p)} options)

when evaluating errors from above

{:schema [:map [:a [:enum "a" "b"]]],
 :value {:a nil},
 :errors (#Error{:path [:a 0], :in [:a], :schema [:enum "a" "b"]})}

using path [:a 0] gives this result:

(mu/get-in [:map [:a [:enum "a" "b"]]] [:a 0])
=> "a"

(error-message {:schema (mu/get-in schema p)} options) => (error-message {:schema "a"} options)

I was able to fix it in the mu/get-in handling :enum's path explicitly:

(defn get-in
  "Like [[clojure.core/get-in]], but for LensSchemas."
  ([?schema ks]
   (get-in ?schema ks nil nil))
  ([?schema ks default]
   (get-in ?schema ks default nil))
  ([?schema [k & ks] default options]
   (let [schema (m/schema (or ?schema :map) options)]
     (cond
       (not k)
       schema

       ;; handle enum [0] path
       (= (m/type schema) :enum)
       schema

       :else
       (let [sentinel #?(:clj (Object.), :cljs (js-obj))
             schema (get schema k sentinel)]
         (cond
           (identical? schema sentinel) default
           ks (get-in schema ks default)
           :else schema))))))

But I'm not sure if this is the right way. If so I can provide a PR.

@ikitommi
Copy link
Member

the correct fix might be to ask if the retuned value is m/-schema? and only then try to pull error out of it. So all code inside the me/-resolve-root-error. PR most welcome.

@ikitommi ikitommi added bug Something isn't working help wanted Help most welcome labels Oct 30, 2021
@ikitommi
Copy link
Member

ikitommi commented Nov 6, 2021

fixed in master

@ikitommi ikitommi closed this as completed Nov 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Help most welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants