Skip to content

Instantly share code, notes, and snippets.

@ordnungswidrig
Created February 11, 2010 13:50
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 ordnungswidrig/301521 to your computer and use it in GitHub Desktop.
Save ordnungswidrig/301521 to your computer and use it in GitHub Desktop.
(def jabba {:name "Jabba the Hutt"
:age 441
:numbers [1 2 -1]
:location {:planet "Tattoine"}
:foos [{:name "b"} {:name "ab" :size 5}]})
(def expected
{:validator :all,
:level :error,
:reason
[{:info :name,
:validator :in,
:level :error,
:reason
[{:info "Jabba", :validator :forbidden-name, :level :error}]}
{:info :numbers,
:validator :in,
:level :error,
:reason
[{:validator :all,
:level :error,
:reason
[{:validator :coll,
:level :error,
:reason [{:key 2, :validator :positive, :level :error}]}
{:validator :count,
:level :error,
:reason [{:info 4, :validator :is-equal, :level :error}]}]}]}
{:info :location,
:validator :in,
:level :error,
:reason
[{:validator :all,
:level :error,
:reason
[{:info :city, :validator :contains, :level :error}
{:info :planet,
:validator :in,
:level :error,
:reason
[{:info "Tattoine", :validator :not-equal, :level :error}]}]}]}
{:info :foos,
:validator :in,
:level :warn,
:reason
[{:validator :coll,
:level :warn,
:reason
[{:key 0,
:validator :all,
:level :warn,
:reason
[{:validator :or,
:level :warn,
:reason
[{:info :size, :validator :contains, :level :warn}
{:info :length, :validator :contains, :level :warn}]}]}
{:key 1,
:validator :all,
:level :warn,
:reason
[{:info :name,
:validator :in,
:level :warn,
:reason
[{:validator :count,
:level :warn,
:reason
[{:info 2, :validator :less-than, :level :error}]}]}]}]}]}]})
(with-test
(defn forbidden-name [name]
(fn [x] (validate #(not (.contains % name)) :forbidden-name name x)))
(defn jabba-validator [j]
((validate-all (validate-in :name (forbidden-name "Jabba"))
(validate-in :numbers (validate-all (validate-coll positive)
(validate-f count (is-equal 4) :count)))
(validate-in :location (validate-all (coll-contains :city)
(validate-in :planet (not-equal "Tattoine"))))
(validate-in :foos
(validate-coll
(validate-all
(validate-or (warn coll-contains :size)
(warn coll-contains :length))
(validate-in :name
(warn validate-f count (less-than 2) :count)))))) j))
(testing "Jabba the Hutt test"
(is (= expected (jabba-validator jabba)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment