                   'Some {:a 1 :c 3}
                   :c ))
 
+
+(defn miss-matches-type? [actual-type?]
+  (and (not (true? actual-type?))
+       (not (nil? (:alternate-type actual-type?)))))
+
+(defn analyze-for-type-misplacement-helper [root-type type-sig path config collected-path]
+  (when (not-empty type-sig)
+    (let [analysis     (detailed-config-analysis config)
+          actual-type? (analysis-type-matches (last type-sig) analysis)]
+      (prn actual-type?)
+      (if (miss-matches-type? actual-type?)
+        {:Error :wrong-position-for-value
+         :current-path collected-path
+         :expected-type (last type-sig)
+         :value config
+         :path collected-path
+         :actual-type (:alternate-type actual-type?)
+         :correct-paths (get-paths-for-type root-type (:alternate-type actual-type?))}
+        (let [k (last path)]
+          (analyze-for-type-misplacement-helper
+           root-type
+           (butlast type-sig)
+           (butlast path)
+           (get config k)
+           (cons k collected-path)))))))
+
+(defn analyze-for-type-misplacement [root-type {:keys [type-sig path orig-config] :as error}]
+  (analyze-for-type-misplacement-helper root-type type-sig path orig-config '()))
+
 (defn unknown-key-error-helper [root-type parent-type bad-key value error]
-  (let [parent-config-value (error-parent-value error)
-        analysis            (detailed-config-analysis parent-config-value)
-        actual-parent-type? (analysis-type-matches parent-type analysis)]
-    #_(prn root-type actual-parent-type?)
-    #_(pprint/pprint analysis)
-    #_(if (= bad-key :magicl))
-    (if (and (not (true? actual-parent-type?)) (not (nil? (:alternate-type actual-parent-type?))))
-      ;; not parent type we are looking at a misplaced configuration
-      ;; value
-      ;; TODO this is still weak
-      [{:Error :wrong-position-for-value
-        :current-path (-> error :path rest)
-        :expected-type parent-type
-        :value parent-config-value
-        :path (rest (:path error))
-        :actual-type (:alternate-type actual-parent-type?)
-        :correct-paths (get-paths-for-type root-type (:alternate-type actual-parent-type?))}]
-      ;; now we can determing if this is most likely a mispelled key
+  (if-let [error (analyze-for-type-misplacement root-type error)]
+    [error]
+    (let [parent-config-value (error-parent-value error)
+          analysis            (detailed-config-analysis parent-config-value)
+          actual-parent-type? (analysis-type-matches parent-type analysis)]
+      #_(prn root-type actual-parent-type?)
+      (prn "START ----")
+      (prn bad-key)
+      (prn parent-type)
+      (pprint/pprint analysis)
+      (prn "END -----")
       (if-let [mispelled-key-error (misspelled-key? parent-type parent-config-value bad-key analysis)]
         [mispelled-key-error]
         (if-let [misplaced-key-error

    (seq? v) (summerize-seq v)
    :else (pr-str v)))

(defn format-key
  ([k] (pr-str k))
  ([k colr] (color (format-key k) colr)))

                          (if (neg? pos) 100 pos))) res-groups)))

(defn type-check-one-error [root-type value]
  (when-let [results (not-empty (type-checker root-type value {}))]
    (when-let [[[typ errors] & xs] (not-empty (->> results
                                                   (map #(assoc % :orig-config value))
                                                   group-and-sort-first-pass-errors))]
      ;; TODO process one error?
      (when-let [errors (not-empty (doall (handle-type-error-groupp root-type [typ errors])))]
        (when-let [[[typ errors] & xs] (not-empty (->> errors
                                                       (map #(assoc % :orig-config value))
                                                       group-and-sort-first-pass-errors))]
          (first errors))))))

#_(with-schema
  (index-spec
