Fork me on GitHub
#malli
<
2020-08-02
>
ikitommi16:08:58

Oh my. Accumulating both :in and :path in explain errors seems redundant. If we know Schema and either of schema-path (`:path`) or a value-path (`:in`), we can calculate the other. Simplifies both -explain and the malli utils. Sweet.

ikitommi16:08:21

and the code is dead simple:

(defn in->path [schema in]
  (loop [i 0, s schema, acc []]
    (or (and (>= i (count in)) acc)
        (recur (inc i) (get s (in i)) (if-not (m/-key s) (conj acc (in i)) acc)))))

(defn path->in [schema path]
  (loop [i 0, s schema, acc []]
    (or (and (>= i (count path)) acc)
        (let [[i k] (if-let [k (m/-key s)] [i k] [(inc i) (path i)])]
          (recur i (get s k) (conj acc k))))))

ikitommi16:08:35

(also, performant, as it’s only protocols and vector indexes)