diff --git a/src/navi/transform.clj b/src/navi/transform.clj index 7cef333..7dba541 100644 --- a/src/navi/transform.clj +++ b/src/navi/transform.clj @@ -22,6 +22,8 @@ ObjectSchema Schema StringSchema + DateTimeSchema + DateSchema UUIDSchema] [io.swagger.v3.oas.models.parameters CookieParameter @@ -56,6 +58,12 @@ :else content-fn))) + + DateSchema + (p/transform [_] inst?) + + DateTimeSchema + (p/transform [_] inst?) UUIDSchema (p/transform [_] uuid?) diff --git a/test/navi/impl_test.clj b/test/navi/impl_test.clj index abdf8c2..f545e22 100644 --- a/test/navi/impl_test.clj +++ b/test/navi/impl_test.clj @@ -13,6 +13,8 @@ [io.swagger.v3.oas.models Operation PathItem] [io.swagger.v3.oas.models.media Content + DateSchema + DateTimeSchema IntegerSchema MediaType ObjectSchema @@ -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" @@ -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)))))) \ No newline at end of file diff --git a/test/navi/transform_test.clj b/test/navi/transform_test.clj index d6aaf42..7ab00ec 100644 --- a/test/navi/transform_test.clj +++ b/test/navi/transform_test.clj @@ -17,6 +17,8 @@ ByteArraySchema ComposedSchema Content + DateSchema + DateTimeSchema IntegerSchema JsonSchema MediaType @@ -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" @@ -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.)))))