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

[:set [:enum …]] results in error in some case for reitit + malli request coercion #775

Open
piotr-yuxuan opened this issue Oct 27, 2022 · 1 comment
Labels
bug Something isn't working question Further information is requested

Comments

@piotr-yuxuan
Copy link
Contributor

piotr-yuxuan commented Oct 27, 2022

In some code I have a schema that is as close as possible to this one (edited for confidentiality):

(def MySchema
  [:map
   [:my-field {:default #{:created-at :id}}
    [:set {:min 1 :decode {:string #(if (string? %) (str/split % #",") %)}}
     [:enum {:decode {:string keyword
                      :json keyword}}
      :created-at :id]]]])

I use reitit 0.5.18 with malli coercion. When a request body contains a slightly invalid value, like:

{:my-field #{:credated-at}}

then with a debugger I can catch humanize being called twice. First with an explanation value that essentially boils down to:

{:value {:my-field #{:credated-at :id}}
 :errors '({:path [:my-field 0]
            :in [:my-field :credated-at]
            :schema [:enum :created-at :id]
            :value :credated-at})}

and the second time with an explanation value that is:

(def explanation
  {:value {:my-field ["id" "credated-at"]} ; Come from an http request, decoded with malli.
   :errors (list {:path [:my-field 0]
                  :in [:my-field :credated-at]
                  :schema [:enum :created-at :id]
                  :value :credated-at})})

This latter value looks like it comes from the json-decoded, but malli-undecoded value. The issue is that it somehow breaks humanize:

(malli.error/humanize explanation)
; => (IllegalArgumentException. "Key must be integer")

However, I'm very puzzled, because I can't reproduce the bug above:

(me/humanize
  (m/explain
    MySchema
    {:my-field #{:credated-at}}))
=> {:my-field #{["should be either :created-at or :id"]}}

I have observed that the issue disappear when I use a :vector instead of a `:set:

(associative? []) ;  => true
(associative? #{}) ; => false

(def MySchema
  [:map
   [:my-field {:default #{:created-at :id}}
    [:vector {:min 1 :decode {:string #(if (string? %) (str/split % #",") %)}}
     [:enum {:decode {:string keyword
                      :json keyword}}
      :created-at :id]]]])

I tried to suggest a fix in #774 but obviously more work, and a better understanding, are needed. I tried to make this bug report as accurate as possible but I know it doesn't pinpoint the root cause – I haven't been able to find it.

@piotr-yuxuan
Copy link
Contributor Author

In my use case, passing down to malli.error/humanize the decoded value instead of the input value removes the exception. Not sure if it is a good way to fix it though.

@vharmain vharmain added bug Something isn't working question Further information is requested labels Dec 10, 2022
cap10morgan added a commit to fluree/malli that referenced this issue Apr 12, 2023
cap10morgan added a commit to fluree/malli that referenced this issue Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants