Skip to content

Instantly share code, notes, and snippets.

@tgk
Created Mar 14, 2014
Embed
What would you like to do?
(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