/
common.clj
89 lines (76 loc) · 2.3 KB
/
common.clj
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(ns
^{:doc "Random utils, common validators."
:author "Paweł Wilk"
:added "1.0.0"}
io.randomseed.utils.validators.common
(:refer-clojure :exclude [parse-long uuid random-uuid])
(:import [org.apache.commons.validator.routines
EmailValidator
UrlValidator
DateValidator])
(:require
[trptr.java-wrapper.locale :as l]
[phone-number.core :as phone]
[io.randomseed.utils.map :as map]
[io.randomseed.utils :refer :all]))
;;
;; e-mail
;;
(defn valid-email?
(^Boolean [address]
(valid-email? address false false))
(^Boolean [address ^Boolean allow-local]
(valid-email? address allow-local false))
(^Boolean [address ^Boolean allow-local ^Boolean allow-tld]
(and
(string? address)
(when-some [v (.getInstance EmailValidator allow-local allow-tld)]
(.isValid v address)))))
(defn ^Boolean valid-email-domain?
([address]
(valid-email-domain? address false false))
([address ^Boolean allow-local]
(valid-email-domain? address allow-local false))
([address ^Boolean allow-local ^Boolean allow-tld]
(and
(string? address)
(when-some [v (.getInstance EmailValidator allow-local allow-tld)]
(.isValidDomain v address)))))
(defn valid-email-local-part?
(^Boolean [address]
(valid-email-local-part? address false false))
(^Boolean [address ^Boolean allow-local]
(valid-email-local-part? address allow-local false))
(^Boolean [address ^Boolean allow-local ^Boolean allow-tld]
(and
(string? address)
(when-some [v (.getInstance EmailValidator allow-local allow-tld)]
(.isValidUser v address)))))
;;
;; date
;;
(defn ^Boolean valid-date?
([value]
(and
(string? value)
(when-some [v (.getInstance DateValidator)]
(.isValid v value))))
([value pattern]
(and
(string? value)
(string? pattern)
(when-some [v (.getInstance DateValidator)]
(.isValid v value pattern))))
([value pattern ^java.util.Locale locale]
(and
(string? value)
(when-some [v (.getInstance DateValidator)]
(if (nil? pattern)
(.isValid v value (l/locale locale))
(and (string? pattern) (.isValid v value pattern (l/locale locale))))))))
;;
;; phone number
;;
(defn valid-phone?
^Boolean [number]
(phone/valid? number))