Skip to content

Commit

Permalink
Moved database validation to datumbazo.
Browse files Browse the repository at this point in the history
  • Loading branch information
r0man committed Oct 2, 2013
1 parent 114e3e5 commit 9fdae80
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,3 +8,4 @@ classes
/target
/pom.xml.asc
/checkouts
/.nrepl-port
1 change: 0 additions & 1 deletion project.clj
Expand Up @@ -7,5 +7,4 @@
:lein-release {:deploy-via :clojars}
:dependencies [[org.clojure/clojure "1.5.1"]
[slingshot "0.10.3"]
[datumbazo "0.5.6"]
[geo-clj "0.3.2"]])
25 changes: 0 additions & 25 deletions src/validation/core.clj
@@ -1,7 +1,6 @@
(ns validation.core
(:refer-clojure :exclude [replace])
(:require [clojure.string :refer [blank? join replace capitalize]]
[datumbazo.core :refer [select from limit run1 where]]
[geo.core :refer [latitude? longitude? point point? point-x point-y point]]
[slingshot.slingshot :refer [throw+]])
(:import geo.core.IPoint))
Expand Down Expand Up @@ -200,27 +199,3 @@ characters."
"Validates that the record's attribute is not blank."
[] (not (blank? (str value)))
"can't be blank.")

(defn new-record? [record]
(blank? (str (:id record))))

(defn uniqueness-of
"Validates that the record's attributes are unique."
[db table columns & {:keys [error] :as opts}]
(fn [record]
(let [error (or error "has already been taken")
columns (if (sequential? columns) columns [columns])
vals (map record columns)
condition `(and ~@(map (fn [c v] `(= ~c ~v)) columns vals))]
(if (and (or (nil? (:if opts))
((:if opts) record))
(run1 db (select columns
(from table)
(where condition)
(limit 1))))
(reduce
(fn [record column]
(with-meta record
(assoc-in (meta record) [:errors column] error)))
record columns)
record))))
12 changes: 1 addition & 11 deletions test/validation/test/core.clj → test/validation/core_test.clj
@@ -1,9 +1,7 @@
(ns validation.test.core
(ns validation.core-test
(:require [clojure.test :refer :all]
[datumbazo.core :refer [run1]]
[geo.core :refer [point]]
[slingshot.slingshot :refer [try+]]
[sqlingvo.core :refer [sql]]
[validation.core :refer :all]))

(defn new-user? [user]
Expand Down Expand Up @@ -355,11 +353,3 @@
(are [address]
(is (not (email? address)))
nil "" "root" "@localhost" "root@localhost" "domain.com" "@domain.com")))

(deftest test-uniqueness-of
(with-redefs [run1 (fn [db stmt]
(is (= ["SELECT \"nick\" FROM \"users\" WHERE (\"nick\" = ?) LIMIT 1" "Bob"]
(sql stmt)))
[])]
(let [errors (:errors (meta ((uniqueness-of nil :users :nick) {:nick "Bob"})))]
(is (= "has already been taken" (:nick errors))))))

0 comments on commit 9fdae80

Please sign in to comment.