diff --git a/src/navi/core.clj b/src/navi/core.clj index ac30ec8..17f9261 100644 --- a/src/navi/core.clj +++ b/src/navi/core.clj @@ -85,6 +85,13 @@ .getSchema schema->spec)))) +(defn matches-regex? + [exp s] + (-> exp + (re-pattern) + (re-matches s) + (some?))) + (defmulti spec (fn [^Schema schema] (or (first (.getTypes schema)) "null"))) @@ -92,9 +99,13 @@ (defmethod spec "string" [^Schema schema] - (if (= "uuid" (.getFormat schema)) - uuid? - string?)) + (let [content-fn (if (= "uuid" (.getFormat schema)) + uuid? + string?) + pattern (.getPattern schema)] + (if pattern + [:and content-fn [:fn #(matches-regex? pattern %)]] + content-fn))) (defmethod spec "integer" @@ -283,4 +294,6 @@ (-> "api.yaml" slurp (routes-from handlers) - pp/pprint)) + pp/pprint) + + (matches-regex? "^(\\d+)([KMGTPE]i{0,1})$" "1024Mi")) diff --git a/test/navi/core_test.clj b/test/navi/core_test.clj index 9f7e2a8..b1469da 100644 --- a/test/navi/core_test.clj +++ b/test/navi/core_test.clj @@ -121,12 +121,20 @@ (core/schema->spec (doto (Schema.) (.addType "string") (.setFormat "uuid")))))) - (testing "jsonschemas with multiple types" (let [strint (-> (JsonSchema.) (.types #{"string" "integer"}))] (is (#{[:or string? int?] [:or int? string?]} - (core/schema->spec strint)))))) + (core/schema->spec strint))))) + (testing "regex string" + (let [[kw1 fn1 [kw2 fn2]] (core/schema->spec (doto (Schema.) + (.addType "string") + (.setPattern "^(\\d+)([KMGTPE]i{0,1})$")))] + (is (= :and kw1)) + (is (= string? fn1)) + (is (= :fn kw2)) + (is (fn2 "1024Ki")) + (is (not (fn2 "1024Kib")))))) (deftest responses-to-malli-spec (testing "empty response"