Created
March 14, 2014 17:16
-
-
Save tgk/9552360 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(ns user | |
(:require [schema.core :as s] | |
[schema.utils :as s-utils])) | |
(defn filter-walker | |
"Creates a walker which replaces sub-trees in values of maps with nil | |
if they do not conform to schema." | |
[schema] | |
(s/start-walker | |
(fn [schema] | |
(let [walk (s/walker schema)] | |
(fn [x] | |
(let [result (walk x)] | |
(if (and (s-utils/error? result) (map? x)) | |
nil | |
result))))) | |
schema)) | |
(def deep-filter | |
(filter-walker [{:foo {:bar (s/enum 42) | |
s/Any s/Any} | |
s/Any s/Any}])) | |
(clojure.pprint/pprint | |
(deep-filter | |
[{:foo {:bar 32}} | |
{:foo {:bar 42 :baz 1}}])) | |
;; [{:foo nil} {:foo {:baz 1, :bar 42}}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment