-
Notifications
You must be signed in to change notification settings - Fork 6
/
validate.cljc
28 lines (23 loc) · 1.06 KB
/
validate.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(ns axel-f.excel.validate)
(defn presence*
"Validate and return given value if not null, and throw an exception with given message (default is 'Argument is required')."
[^{:doc "Object to check for presence."} x
& [^{:doc "Optional message to be thrown."} msg]]
(let [msg (or msg "Argument required")]
(if (some? x) x (throw (ex-info msg {:type ::validate-error
:subtype ::presence})))))
(def presence #'presence*)
(defn not-empty**
"Validate and return given value if not empty collection, and throw an exception with given message (default is 'Argument can not be empty'). "
[^{:doc "Object to check for emptiness."} x
& [^{:doc "Optional message to be thrown."} msg]]
(let [msg (or msg "Argument can not be empty")]
(or (if ((some-fn coll? string?) x)
(not-empty x)
x)
(throw (ex-info msg {:type ::validate-error
:subtype ::empty})))))
(def not-empty* #'not-empty**)
(def env
{"validate" {"presence" presence
"not-empty" not-empty*}})