Skip to content

Commit

Permalink
Merge pull request #41 from Skinney/optionals
Browse files Browse the repository at this point in the history
Fixes #36: All validators are optional by default
  • Loading branch information
theleoborges committed Nov 4, 2015
2 parents c805aeb + b81d7bd commit 690e414
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -348,7 +348,7 @@ As an example, here's a simplified version of the `bouncer.validators/number` va
Options is a map of key/value pairs where:

- `:default-message-format` - to be used when clients of this validator don't provide one
- `:optional` - a boolean indicating if this validator should only trigger for keys that have a value different than `nil`. Defaults to false.
- `:optional` - a boolean indicating if this validator should only trigger for keys that have a value different than `nil`. Defaults to true.

That's all syntactic sugar for:

Expand Down
16 changes: 9 additions & 7 deletions src/bouncer/validators.cljx
Expand Up @@ -21,7 +21,7 @@
provide a message (consider using custom message functions)
- `:optional` whether the validation should be run only if the given key has
a non-nil value in the map. Defaults to false.
a non-nil value in the map. Defaults to true.
or any other key-value pair which will be available in the validation result
under the `:metadata` key.
Expand Down Expand Up @@ -53,8 +53,9 @@
[fn-meta [args & body]] (if (map? (first options))
[(first options) (next options)]
[nil options])
fn-meta (assoc fn-meta
:validator (keyword (str *ns*) (str name)))]
fn-meta (merge {:optional true}
fn-meta
{:validator (keyword (str *ns*) (str name))})]
(let [arglists ''([name])]
`(do (def ~name (with-meta (fn ~name
([~@args]
Expand All @@ -69,7 +70,8 @@
For use with validation functions such as `validate` or `valid?`
"
{:default-message-format "%s must be present"}
{:default-message-format "%s must be present"
:optional false}
[value]
(if (string? value)
(not (empty? value))
Expand All @@ -79,7 +81,7 @@
"Validates maybe-a-number is a valid number.
For use with validation functions such as `validate` or `valid?`"
{:default-message-format "%s must be a number" :optional true}
{:default-message-format "%s must be a number"}
[maybe-a-number]
(number? maybe-a-number))

Expand Down Expand Up @@ -120,7 +122,7 @@
"Validates number is a number and is greater than zero.
For use with validation functions such as `validate` or `valid?`"
{:default-message-format "%s must be a positive number" :optional true}
{:default-message-format "%s must be a positive number"}
[number]
(> number 0))

Expand Down Expand Up @@ -153,7 +155,7 @@
"Validates value satisfies the given regex pattern.
For use with validation functions such as `validate` or `valid?`"
{:default-message-format "%s must satisfy the given pattern" :optional true}
{:default-message-format "%s must satisfy the given pattern"}
[value re]
((complement empty?) (re-seq re value)))

Expand Down
7 changes: 4 additions & 3 deletions test/bouncer/core_test.cljx
Expand Up @@ -202,9 +202,9 @@
:mobile '({:path [:mobile], :value nil, :args nil, :message "wrong format"
:metadata {:default-message-format "Custom validation failed for %s"
:optional false}})
:car '({:path [:car], :value nil, :args [["Ferrari" "Mustang" "Mini"]], :message nil
:car '({:path [:car], :value "Volvo", :args [["Ferrari" "Mustang" "Mini"]], :message nil
:metadata {:default-message-format "%s must be one of the values in the list"
:optional false
:optional true
:validator :bouncer.validators/member}})
:dob '({:path [:dob], :value "NaN", :args nil, :message nil
:metadata {:default-message-format "%s must be a number"
Expand All @@ -221,11 +221,12 @@
:address {:past (list {:path [:address :past], :value [{:country nil} {:country "Brasil"}],
:args [pred-fn] :message nil
:metadata {:default-message-format "All items in %s must satisfy the predicate"
:optional false
:optional true
:validator :bouncer.validators/every}})}
}
invalid-map {:name nil
:age ""
:car "Volvo"
:passport {:number -7 :issued_by "Australia"}
:dob "NaN"
:address {:current { :country "Australia"}
Expand Down
1 change: 0 additions & 1 deletion test/bouncer/validators_test.cljx
Expand Up @@ -172,7 +172,6 @@
(is (core/valid? {:email "test+blabla@googlexyz.com"} :email [[v/email]]))
(is (core/valid? {:email "test@googlexyz.co.uk"} :email [[v/email]])))
(testing "will reject invalid emails"
(is (not (core/valid? {:email nil} :email [[v/email]])))
(is (not (core/valid? {:email ""} :email [[v/email]])))
(is (not (core/valid? {:email "test"} :email [[v/email]])))
(is (not (core/valid? {:email "test@"} :email [[v/email]])))
Expand Down

0 comments on commit 690e414

Please sign in to comment.