::invalid?
can sometimes be incorrect.validate
. Maps that conform
to a validation set are now returned wihtout modification. Maps that do not conform
to a validation set are returned with a validation error sequence transposed onto the
applicable keys, see example below.(validate
[:email email?
:age [between? 18 40]
[:nested :value] [between? 0 10]]
{:email "testemail.com" :age 10 :nested {:value 19}})
;; {:clova.core/results ("email should be a valid email address." "age is 10 but it must be between 18 and 40." "nested value is 19 but it must be between 0 and 10.")
;; :clova.core/invalid? true
;; :email ("email should be a valid email address.")
;; :age ("age is 10 but it must be between 18 and 40."),
;; :nested {:value ("nested value is 19 but it must be between 0 and 10.")}}
results
convenience function(results [:email required? email?] map-to-validate)
exists?
validator.not-exists?
validator.
(let [database {:emails ["test@email.com"]}
emails (fn [email] (filter #{email} (:emails database)))
v-set (validation-set [:email required? [not-exists? emails]])]
(validate v-set {:email "test2@email.com"})
;; {:results (), :valid? true}
(validate v-set {:email "anothertest@email.com"})
;; {:results (), :valid? true}
(validate v-set {:email "test@email.com"})
;; {:results ("test@email.com already exists."),
;; :valid? false}
(validate v-set {})
;; {:results ("email is required."), :valid? false}
(let [validation-set (validation-set [:age [> 18]])])
In failure scenarios a generic message format of "%s has value %s, which is invalid."
is used.
(let [message-func (fn [v-type value args]
(case v-type
:between (str "Age is " value " but it must be between " (first args) " and " (second args))
nil))]
(validate v-set {:age 9} {:default-message-fn message-func}))
alphanumeric?
validator.(alphanumeric? "abc")
(=? {:a 1} {:a 1})
Not to be confused with the date?
validator which tests that a value is a date. The
=date?
validator checks that a value is equal to a given date.
(=date? "01-01-2011" "01-01-2011")
;; or specify an optional date formatter
(=date? "01-01-2011" "02-01-2011" {:formatter "dd-MM-yyyy"})
(after? "01-01-2011" "02-01-2012")
;; or specify an optional date formatter
(after? "01-01-2011" "02-01-2012" {:formatter "dd-MM-yyyy"})
(before? "01-01-2011" "01-01-2012")
;; or specify an optional date formatter
(before? "01-01-2011" "01-01-2012" {:formatter "dd-MM-yyyy"})
:formatter
for the date?
validator was never
coverted to a clj-time/cljs-time formatter.date?
validator. It is now possible to do:(validate (validation-set [:date date?]) {:date "20150101"})
anon
to as-validator
.anon
function. This can be used to declare an on the fly annonymous validator.(validate (validation-set [:name (anon #(= % "mark"))]) {:name "mark"})
stringy?
validator.numeric?
validator.(let [validation-set (core/validation-set [:age required? [greater? 18] [lesser? 30]]))
:short-circuit?
option to validate option map. Can be used to break validation
on the first validation failure.credit-card?
validator based on the Luhn algorithm.Qualify clova specific meta data keys with the clova.core namespace.
:type
becomes :clova.core/type
:default-message
becomes :clova.core/default-message
:allow-missing-key?
becomes :clova.core/allow-missing-key?
:target
becomes :clova.core/target
:args
becomes :clova.core/args
all?
.(let [validation-set (core/validation-set [:age [all? [[greater? 18] [lesser? 30]]]])]
one-of?
validator does not return a true/false value and when used in
isolation can result in erroneous results."Remove required
wrapper function and replace it with a required?
validator.
Instead of wrapping a validator with the required function we can just compose validators. e.g.
;; This will failed because the :age key is required
(validate (core/validation-set
[:age core/required?
:age [core/between? 18 30]]) {})
;; This will fail because age is not between 18 and 30
(validate (core/validation-set
[:age core/required?
:age [core/between? 18 30]]) {:age 1})
Rename present?
validator to not-nil?
.
required
function to disable :clova.core/allow-missing-key? and force a key to be present.Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close