Skip to content

Instantly share code, notes, and snippets.

@tgk
Created March 14, 2014 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgk/9552360 to your computer and use it in GitHub Desktop.
Save tgk/9552360 to your computer and use it in GitHub Desktop.
(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