Skip to content

Commit a968579

Browse files
committed
[#14] merge main
2 parents 73ec440 + deb08b9 commit a968579

File tree

4 files changed

+73
-13
lines changed

4 files changed

+73
-13
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Currently unsupported (raise an issue if needed!):
2020
- Other coercion libs
2121
- `oneOf` composed schema (mostly can be handled by `anyOf`)
2222
- Some string formats:
23-
- DateTime
2423
- Email
2524
- File
2625

src/navi/transform.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
ObjectSchema
2323
Schema
2424
StringSchema
25+
DateTimeSchema
26+
DateSchema
2527
UUIDSchema]
2628
[io.swagger.v3.oas.models.parameters
2729
CookieParameter
@@ -91,6 +93,12 @@
9193
:else
9294
content-fn)))
9395

96+
DateSchema
97+
(p/transform [_] inst?)
98+
99+
DateTimeSchema
100+
(p/transform [_] inst?)
101+
94102
UUIDSchema
95103
(p/transform [_] uuid?)
96104

test/navi/impl_test.clj

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66

77
(ns navi.impl-test
88
(:require
9+
[clojure.java.io :as io]
910
[clojure.test :refer [deftest is testing]]
1011
[navi.core :as navi]
11-
[navi.impl :as i]
12-
[clojure.java.io :as io])
12+
[navi.impl :as i])
1313
(:import
1414
[clojure.lang ExceptionInfo]
1515
[io.swagger.v3.oas.models Operation PathItem]
1616
[io.swagger.v3.oas.models.media
1717
Content
18+
DateSchema
19+
DateTimeSchema
1820
IntegerSchema
1921
MediaType
2022
ObjectSchema
@@ -39,7 +41,17 @@
3941
(testing "convert an optional OpenAPI Map entry"
4042
(let [property (Map/entry "id" (StringSchema.))]
4143
(is (= [:id {:optional true} string?]
42-
(i/->prop-schema #{"x"} property))))))
44+
(i/->prop-schema #{"x"} property)))))
45+
46+
(testing "convert a DateTime OpenAPI Map entry"
47+
(let [property (Map/entry "timestamp" (DateTimeSchema.))]
48+
(is (= [:timestamp inst?]
49+
(i/->prop-schema #{"timestamp"} property)))))
50+
51+
(testing "convert a Date OpenAPI Map entry"
52+
(let [property (Map/entry "date" (DateSchema.))]
53+
(is (= [:date inst?]
54+
(i/->prop-schema #{"date"} property))))))
4355

4456
(deftest openapi-parameters-to-malli-spec
4557
(testing "convert a required OpenAPI Parameter"
@@ -144,22 +156,23 @@
144156
(is (= {:get {:handler "a handler"
145157
:parameters {:path [:map [:x int?]]}}}
146158
(i/path-item->data path-item handlers))))))
159+
147160
(defn find-route [rts path method]
148-
(some (fn [[p r]]
149-
(when (= p path)
150-
(get r method)))
151-
rts))
161+
(some (fn [[p r]]
162+
(when (= p path)
163+
(get r method)))
164+
rts))
152165

153166
(deftest security-requirements-test
154167
(testing "Verifying security requirements from security-users.yml"
155168
;; A dummy map of operationId to handler (the actual function doesn't matter for this test).
156-
(let [handlers {"listUsers" (constantly :ok)
157-
"listUsersSingle" (constantly :ok)
158-
"listUsersNoScope" (constantly :ok)
169+
(let [handlers {"listUsers" (constantly :ok)
170+
"listUsersSingle" (constantly :ok)
171+
"listUsersNoScope" (constantly :ok)
159172
"listUsersNoSecurity" (constantly :ok)}
160173
api-spec (slurp (io/resource "security-users.yml"))
161-
routes (navi/routes-from api-spec handlers)]
162-
174+
routes (navi/routes-from api-spec handlers)]
175+
163176
(testing "multiple security schemes"
164177
(let [route (find-route routes "/users" :get)]
165178
(is (some? route) "Should have found /users GET route")

test/navi/transform_test.clj

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
ByteArraySchema
1818
ComposedSchema
1919
Content
20+
DateSchema
21+
DateTimeSchema
2022
IntegerSchema
2123
JsonSchema
2224
MediaType
@@ -34,6 +36,10 @@
3436
[java.util LinkedHashMap]))
3537

3638
(deftest primitives
39+
(testing "datetime"
40+
(is (= inst? (p/transform (DateTimeSchema.)))))
41+
(testing "date"
42+
(is (= inst? (p/transform (DateSchema.)))))
3743
(testing "string"
3844
(is (= string? (p/transform (StringSchema.)))))
3945
(testing "integer"
@@ -84,6 +90,40 @@
8490
(testing "nil"
8591
(is (= any? (p/transform nil)))))
8692

93+
(deftest date-schema-transformations
94+
(testing "DateSchema transforms to inst? predicate"
95+
(let [schema (DateSchema.)]
96+
(is (= inst? (p/transform schema)))))
97+
98+
(testing "DateTimeSchema transforms to inst? predicate"
99+
(let [schema (DateTimeSchema.)]
100+
(is (= inst? (p/transform schema)))))
101+
102+
(testing "inst? validates different date types"
103+
(let [schema (DateTimeSchema.)
104+
pred (p/transform schema)]
105+
(testing "java.util.Date"
106+
(is (pred (java.util.Date.))))
107+
(testing "java.time.Instant"
108+
(is (pred (java.time.Instant/now))))
109+
(testing "java.time.LocalDateTime converted to Instant"
110+
(is (pred (-> (java.time.LocalDateTime/now)
111+
(.atZone (java.time.ZoneId/systemDefault))
112+
.toInstant))))
113+
(testing "java.time.ZonedDateTime converted to Instant"
114+
(is (pred (-> (java.time.ZonedDateTime/now)
115+
.toInstant))))
116+
(testing "java.time.OffsetDateTime converted to Instant"
117+
(is (pred (-> (java.time.OffsetDateTime/now)
118+
.toInstant))))))
119+
120+
(testing "inst? rejects invalid inputs"
121+
(let [schema (DateTimeSchema.)
122+
pred (p/transform schema)]
123+
(is (not (pred "2024-01-01")))
124+
(is (not (pred nil)))
125+
(is (not (pred 123))))))
126+
87127
(deftest string-formats
88128
(testing "uuid"
89129
(is (= uuid? (p/transform (UUIDSchema.)))))

0 commit comments

Comments
 (0)