Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed tests

  • Loading branch information...
commit 38bbb4f1b11a723ff28800f7dd780717d1b40cc4 1 parent c9c71b2
@logaan authored
View
2  src/vlad.clj
@@ -20,5 +20,5 @@
validate]
[vlad.default-errors
assign-name
- english-translate
+ english-translation
translate-errors])
View
25 src/vlad/default_errors.clj
@@ -10,40 +10,39 @@
[errors selectors-to-names]
(map #(assoc % :name (selectors-to-names (:selector %))) errors))
-(defmulti english-translate
- "The translate function simply takes an error and returns a readable version
- of it."
+(defmulti english-translation
+ "Takes an error and returns a human readable version of it."
:type)
-(defmethod english-translate :vlad.validations/present
+(defmethod english-translation :vlad.validations/present
[{:keys [name]}]
(format "%s is required." name))
-(defmethod english-translate :vlad.validations/length-over
+(defmethod english-translation :vlad.validations/length-over
[{:keys [name size]}]
(format "%s must be over %s characters long." name size))
-(defmethod english-translate :vlad.validations/length-under
+(defmethod english-translation :vlad.validations/length-under
[{:keys [name size]}]
(format "%s must be under %s characters long." name size))
-(defmethod english-translate :vlad.validations/one-of
+(defmethod english-translation :vlad.validations/one-of
[{:keys [name set]}]
(format "%s must be one of %s." name (s/join ", " set)))
-(defmethod english-translate :vlad.validations/not-of
+(defmethod english-translation :vlad.validations/not-of
[{:keys [name set]}]
(format "%s must not be one of %s." name (s/join ", " set)))
-(defmethod english-translate :vlad.validations/equals-value
+(defmethod english-translation :vlad.validations/equals-value
[{:keys [name value]}]
(format "%s must be \"%s\"." name value))
-(defmethod english-translate :vlad.validations/equals-field
+(defmethod english-translation :vlad.validations/equals-field
[{:keys [first-name second-name]}]
(format "%s must be the same as %s." first-name second-name))
-(defmethod english-translate :vlad.validations/matches
+(defmethod english-translation :vlad.validations/matches
[{:keys [name pattern]}]
(format "%s must match the pattern %s." name (.toString pattern)))
@@ -59,10 +58,10 @@
:name \"Password\"
:size 8}])
; => {[:password] \"Password must be under 8 characters long.\"}"
- [errors translate]
+ [errors translation]
(reduce (fn [output-map {:keys [selector] :as error}]
(let [existing-errors (get output-map selector [])
- new-errors (conj existing-errors (translate error))]
+ new-errors (conj existing-errors (translation error))]
(assoc output-map selector new-errors)))
{} errors))
View
16 test/vlad/test/default_errors.clj
@@ -5,31 +5,31 @@
[vlad.validations :as v]))
(tabular
- (fact (translate-errors ?errors) => ?translations)
+ (fact (translate-errors ?errors english-translation) => ?translations)
?errors ?translations
[{:type :vlad.validations/present :selector [:password] :name "Password"}]
- {[:password] "Password is required."}
+ {[:password] ["Password is required."]}
[{:type :vlad.validations/length-over :selector [:password] :name "Password" :size 8}]
- {[:password] "Password must be over 8 characters long."}
+ {[:password] ["Password must be over 8 characters long."]}
[{:type :vlad.validations/one-of :selector [:title] :name "Title" :set #{"Mr" "Ms" "Mrs"}}]
- {[:title] "Title must be one of Mr, Ms, Mrs."}
+ {[:title] ["Title must be one of Mr, Ms, Mrs."]}
[{:type :vlad.validations/not-of :selector [:username] :name "Username" :set #{"login" "logout"}}]
- {[:username] "Username must not be one of login, logout."}
+ {[:username] ["Username must not be one of login, logout."]}
[{:type :vlad.validations/equals-value :selector [:over_18] :name "Over 18" :value "yes"}]
- {[:over_18] "Over 18 must be \"yes\"."}
+ {[:over_18] ["Over 18 must be \"yes\"."]}
[{:type :vlad.validations/equals-field
:first-name "Password" :first-selector [:password]
:second-name "Password confirmation" :second-selector [:password-confirmation]}]
- {nil "Password must be the same as Password confirmation."}
+ {nil ["Password must be the same as Password confirmation."]}
[{:type :vlad.validations/matches :name "Username" :selector [:username] :pattern #"\w+"}]
- {[:username] "Username must match the pattern \\w+."})
+ {[:username] ["Username must match the pattern \\w+."]})
(fact (assign-name (vt/validate (v/present [:foo]) {})
{[:foo] "Foozle"})
View
10 test/vlad/test/readme.clj
@@ -28,7 +28,7 @@
(midje/fact
(-> (validate signup {:password "!"})
(assign-name english-field-names)
- (translate-errors english-translate))
+ (translate-errors english-translation))
=> {[:password] ["Password must be over 6 characters long."
"Password must match the pattern [a-zA-Z]."
@@ -42,9 +42,9 @@
[:password] "密码"
[:confirmation] "确认密码"})
-(defmulti chinese-translate :type)
+(defmulti chinese-translation :type)
-(defmethod chinese-translate :vlad.validations/present
+(defmethod chinese-translation :vlad.validations/present
[{:keys [name]}]
(format "请输入%s" name))
@@ -53,6 +53,6 @@
(midje/fact
(-> (validate update {:name "Rich"})
(assign-name chinese-field-names)
- (translate-errors chinese-translate))
+ (translate-errors chinese-translation))
- => {[:email] ["必须填写邮箱"]})
+ => {[:email] ["请输入邮箱"]})
Please sign in to comment.
Something went wrong with that request. Please try again.