Created
May 15, 2014 12:49
-
-
Save ikitommi/4ccd3ae5446b31737d6e to your computer and use it in GitHub Desktop.
Stripping invalid keys from a form based on a Schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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
https://groups.google.com/forum/#!msg/prismatic-plumbing/SaOBraHzoHE/jN0XCILScfEJ