Skip to content

Instantly share code, notes, and snippets.

@ordnungswidrig
Created February 23, 2010 11:04
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/312071 to your computer and use it in GitHub Desktop.
Save ordnungswidrig/312071 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]
((all (in :name (forbidden-name "Jabba"))
(in :numbers (all (all-in-coll positive)
(applied count (is-equal 4) :count)))
(in :location (all (coll-contains :city)
(in :planet (not-equal "Tattoine"))))
(in :foos
(all-in-coll
(all
(any (warn coll-contains :size)
(warn coll-contains :length))
(in :name
(warn applied 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