Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/navi/transform.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
ObjectSchema
Schema
StringSchema
DateTimeSchema
DateSchema
UUIDSchema]
[io.swagger.v3.oas.models.parameters
CookieParameter
Expand Down Expand Up @@ -56,6 +58,12 @@

:else
content-fn)))

DateSchema
(p/transform [_] inst?)

DateTimeSchema
(p/transform [_] inst?)

UUIDSchema
(p/transform [_] uuid?)
Expand Down
16 changes: 14 additions & 2 deletions test/navi/impl_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
[io.swagger.v3.oas.models Operation PathItem]
[io.swagger.v3.oas.models.media
Content
DateSchema
DateTimeSchema
IntegerSchema
MediaType
ObjectSchema
Expand All @@ -37,7 +39,17 @@
(testing "convert an optional OpenAPI Map entry"
(let [property (Map/entry "id" (StringSchema.))]
(is (= [:id {:optional true} string?]
(i/->prop-schema #{"x"} property))))))
(i/->prop-schema #{"x"} property)))))

(testing "convert a DateTime OpenAPI Map entry"
(let [property (Map/entry "timestamp" (DateTimeSchema.))]
(is (= [:timestamp inst?]
(i/->prop-schema #{"timestamp"} property)))))

(testing "convert a Date OpenAPI Map entry"
(let [property (Map/entry "date" (DateSchema.))]
(is (= [:date inst?]
(i/->prop-schema #{"date"} property))))))

(deftest openapi-parameters-to-malli-spec
(testing "convert a required OpenAPI Parameter"
Expand Down Expand Up @@ -141,4 +153,4 @@
(.setGet operation))]
(is (= {:get {:handler "a handler"
:parameters {:path [:map [:x int?]]}}}
(i/path-item->data path-item handlers))))))
(i/path-item->data path-item handlers))))))
40 changes: 40 additions & 0 deletions test/navi/transform_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
ByteArraySchema
ComposedSchema
Content
DateSchema
DateTimeSchema
IntegerSchema
JsonSchema
MediaType
Expand All @@ -34,6 +36,10 @@
[java.util LinkedHashMap]))

(deftest primitives
(testing "datetime"
(is (= inst? (p/transform (DateTimeSchema.)))))
(testing "date"
(is (= inst? (p/transform (DateSchema.)))))
(testing "string"
(is (= string? (p/transform (StringSchema.)))))
(testing "integer"
Expand Down Expand Up @@ -81,6 +87,40 @@
(testing "nil"
(is (= any? (p/transform nil)))))

(deftest date-schema-transformations
(testing "DateSchema transforms to inst? predicate"
(let [schema (DateSchema.)]
(is (= inst? (p/transform schema)))))

(testing "DateTimeSchema transforms to inst? predicate"
(let [schema (DateTimeSchema.)]
(is (= inst? (p/transform schema)))))

(testing "inst? validates different date types"
(let [schema (DateTimeSchema.)
pred (p/transform schema)]
(testing "java.util.Date"
(is (pred (java.util.Date.))))
(testing "java.time.Instant"
(is (pred (java.time.Instant/now))))
(testing "java.time.LocalDateTime converted to Instant"
(is (pred (-> (java.time.LocalDateTime/now)
(.atZone (java.time.ZoneId/systemDefault))
.toInstant))))
(testing "java.time.ZonedDateTime converted to Instant"
(is (pred (-> (java.time.ZonedDateTime/now)
.toInstant))))
(testing "java.time.OffsetDateTime converted to Instant"
(is (pred (-> (java.time.OffsetDateTime/now)
.toInstant))))))

(testing "inst? rejects invalid inputs"
(let [schema (DateTimeSchema.)
pred (p/transform schema)]
(is (not (pred "2024-01-01")))
(is (not (pred nil)))
(is (not (pred 123))))))

(deftest string-formats
(testing "uuid"
(is (= uuid? (p/transform (UUIDSchema.)))))
Expand Down