metosin / malli

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

fix: _Key must be integer_ when `assoc` called with :kw on a vec #774

Open piotr-yuxuan opened 2 years ago

piotr-yuxuan commented 2 years ago

At some point I stumbled upon this unexpected behaviour:

(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})})

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

(alter-var-root
  (var me/-push)
  (constantly (fn [x k v fill]
                (let [i (when (int? k) k)
                      x' (cond-> x (and i (sequential? x) (> k (count x))) (me/-fill k fill))]
                  (cond (nil? x') (assoc x' k v)
                        (and (associative? x') i) (assoc x' i v)
                        (associative? x') (assoc x' (count x') v)
                        (set? x') (conj x' v)
                        :else (apply list (assoc (vec x') k v)))))))

(me/humanize explanation)
; => {0 [["should be either :created-at or :id"]]}