Skip to content
Browse files

fixed chain's lazyness. inlined facts. added composition example.

  • Loading branch information...
1 parent 42c7dbb commit fa1d845dbc86a1dc38374d9a2e9efc672c207438 @logaan committed Oct 14, 2013
Showing with 34 additions and 27 deletions.
  1. +10 −5 README.md
  2. +2 −3 src/vlad/validation_types.clj
  3. +22 −19 test/vlad/test/readme.clj
View
15 README.md
@@ -19,12 +19,13 @@ API Docs: <http://logaan.github.io/vlad/vlad.html>
```clojure
(ns vlad.test.readme
- (:require [vlad :refer :all]))
+ (:require [vlad :refer :all]
+ [midje.sweet :refer [fact]]))
+
+(fact (validate (present [:age]) {:name "Logan"})
+ => [{:type :vlad.validations/present
+ :selector [:age]}])
-(midje/fact
- (validate (present [:age]) {:name "Logan"})
- => [{:type :vlad.validations/present
- :selector [:age]}])
```
## Composition
@@ -51,6 +52,10 @@ redundant error messages.
(def update
common)
+
+(fact (validate signup {:name "Logan Campbell"})
+ => '({:selector [:email], :type :vlad.validations/present}
+ {:selector [:password], :type :vlad.validations/present}))
```
And of course all these validations could be run over any data. Whether you're
View
5 src/vlad/validation_types.clj
@@ -40,9 +40,8 @@
(defrecord Chain [left right]
Validation
(validate [{:keys [left right]} data]
- (let [left-errors (validate left data)
- right-errors (validate right data)]
- (if (empty? left-errors) right-errors left-errors))))
+ (let [left-errors (validate left data)]
+ (if (empty? left-errors) (validate right data) left-errors))))
(defn chain
"Example:
View
41 test/vlad/test/readme.clj
@@ -1,13 +1,13 @@
(ns vlad.test.readme
(:require [vlad :refer :all]
- [midje.sweet :as midje]))
+ [midje.sweet :refer [fact]]))
; Basics
-(midje/fact
- (validate (present [:age]) {:name "Logan"})
- => [{:type :vlad.validations/present
- :selector [:age]}])
+(fact (validate (present [:age]) {:name "Logan Campbell"})
+ => [{:type :vlad.validations/present
+ :selector [:age]}])
+; Composition
(def common
(join (present [:name])
(present [:email])))
@@ -25,22 +25,26 @@
(def update
common)
+(fact (validate signup {:name "Logan Campbell"})
+ => '({:selector [:email], :type :vlad.validations/present}
+ {:selector [:password], :type :vlad.validations/present}))
+
+; Translation
(def english-field-names
{[:name] "Full Name"
[:email] "Email Address"
[:password] "Password"
[:confirmation] "Password Confirmation"})
-(midje/fact
- (-> (validate signup {:password "!"})
- (assign-name english-field-names)
- (translate-errors english-translation))
+(fact (-> (validate signup {:password "!"})
+ (assign-name english-field-names)
+ (translate-errors english-translation))
- => {[:password] ["Password must be over 6 characters long."
- "Password must match the pattern [a-zA-Z]."
- "Password must match the pattern [0-9]."],
- [:email] ["Email Address is required."],
- [:name] ["Full Name is required."]})
+ => {[:password] ["Password must be over 6 characters long."
+ "Password must match the pattern [a-zA-Z]."
+ "Password must match the pattern [0-9]."],
+ [:email] ["Email Address is required."],
+ [:name] ["Full Name is required."]})
(def chinese-field-names
{[:name] "姓名"
@@ -56,9 +60,8 @@
; Other validation translations go here.
-(midje/fact
- (-> (validate update {:name "Rich"})
- (assign-name chinese-field-names)
- (translate-errors chinese-translation))
+(fact (-> (validate update {:name "Rich"})
+ (assign-name chinese-field-names)
+ (translate-errors chinese-translation))
- => {[:email] ["请输入邮箱"]})
+ => {[:email] ["请输入邮箱"]})

0 comments on commit fa1d845

Please sign in to comment.
Something went wrong with that request. Please try again.