Skip to content

Instantly share code, notes, and snippets.

@ikitommi
Created May 15, 2014 12:49
Show Gist options
  • Save ikitommi/4ccd3ae5446b31737d6e to your computer and use it in GitHub Desktop.
Save ikitommi/4ccd3ae5446b31737d6e to your computer and use it in GitHub Desktop.
Stripping invalid keys from a form based on a Schema
(require '[schema.core :as s])
(require '[plumbing.core :refer :all])
(defn path-vals
"Returns vector of tuples containing path vector to the value and the value."
[m]
(letfn
[(pvals [l p m]
(reduce
(fn [l [k v]]
(if (map? v)
(pvals l (conj p k) v)
(cons [(conj p k) v] l)))
l m))]
(pvals [] [] m)))
(defn strip-dissallowed-keys
"Strips away the keys that are not part of the Schema."
[schema form]
(->> form
(s/check schema)
path-vals
(filter (fn-> second 'disallowed-key))
(map first)
(reduce (partial dissoc-in) form)))
(def Kikka {:a String
:b {:c {:d String}
:c2 String}})
(strip-dissallowed-keys Kikka {:a "kikka"
:b {:c {:d "kukka"
:d2 "kikka"
:d3 "kukka"}}})
;; => {:b {:c {:d "kukka"}}, :a "kikka"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment