/
common_schemas.clj
125 lines (89 loc) · 3.58 KB
/
common_schemas.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(ns common.common-schemas
(:require [malli.core :as m]
[common.validator :as validator]
[common.generators :as g]
[java-time :as jt]
[malli.registry :as mr]
[hyperfiddle.rcf :refer [tests tap]]
[clojure.string :as str]
[malli.generator :as mg]))
(def registry*
(atom (m/default-schemas)))
(defn register! [type ?schema]
(swap! registry* assoc type ?schema))
(mr/set-default-registry!
(mr/mutable-registry registry*))
(register! :date
(m/-simple-schema
{:type :date
:pred #(try (do (jt/local-date %) true)
(catch Exception e false))
:type-properties {:error/message "should be valid date"
:json-schema/type "string"
:json-schema/example "2023-01-01"
:json-schema/format "date"
:gen/gen g/local-date}}))
(register! :datetime
(m/-simple-schema
{:type :datetime
:pred #(try (do (jt/local-date-time %) true)
(catch Exception e false))
:type-properties {:error/message "should be valid datetime"
:json-schema/type "string"
:json-schema/example "2023-01-01T08:08:09"
:json-schema/format "date"
:gen/gen (g/date-time)}}))
(tests
"check date"
(m/validate :date "2023-01-02") := true
(m/validate :date "2023-01-32") := false
(m/validate :date "") := false
(m/validate :date "2999-05-05") := true
(mg/generate :date)
(m/validate :datetime "1778-07-26T05:59:28.450") := true
(mg/generate :datetime)
)
(register! :ipv4
(m/-simple-schema
{:type :ipv4
:pred validator/valid-ipv4?
:type-properties {:error/message "should be valid ip v4 address"
:json-schema/type "string"
:json-schema/example "8.8.8.8"
:json-schema/format "ipv4"
:gen/gen g/ipv4-address}}))
(register! :ipv6
(m/-simple-schema
{:type :ipv6
:pred validator/valid-ipv6?
:type-properties {:error/message "should be valid ip v6 address"
:json-schema/type "string"
:json-schema/example "b6b5:4d8c:0d1f:4dfc:9b51:e8ba:3a39:4621"
:json-schema/format "ipv6"
:gen/gen g/ipv6-address}}))
(tests
(m/validate :ipv4 "192.168.0.1") := true
(m/validate :ipv4 "192.168.0.256") := false
(m/validate :ipv4 nil) := false
(mg/generate :ipv4)
(m/validate :ipv6 "2001:0db8:85a3:0000:0000:8a2e:0370:7334") := true
(m/validate :ipv6 "") := false
(m/validate :ipv6 nil) := false
(m/validate :ipv6 8) := false
(mg/generate :ipv6)
)
(register! :email
(m/-simple-schema
{:type :email
:pred validator/valid-email?
:type-properties {:error/message "should be valid email address"
:json-schema/type "string"
:json-schema/example "test@test.com"
:json-schema/format "email"
:gen/gen g/email}}))
(tests
(m/validate :email "mail@163.com") := true
(m/validate :email "mail.163.com") := false
(m/validate :email "a@.com") := false
(mg/generate :email)
)