Skip to content

Commit

Permalink
Add minLength and maxLength support
Browse files Browse the repository at this point in the history
- Rework string spec generation to support the
  use of malli properties
- Add malli as a test dependency
- Update regex tests to use malli/validate instead
  of destructuring the spec so that they pass with
  the new spec generation.
- Test that strings of various lengths validate
  as expected against the generated spec.
  • Loading branch information
john-shaffer authored and lispyclouds committed Oct 19, 2024
1 parent d6af7d2 commit 34be813
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{:deps {io.swagger.parser.v3/swagger-parser {:mvn/version "2.1.22"}}
:aliases {:test {:extra-paths ["test"]
:extra-deps {io.github.cognitect-labs/test-runner
{:git/tag "v0.5.1" :git/sha "dfb30dd"}}
{:git/tag "v0.5.1" :git/sha "dfb30dd"}
metosin/malli {:mvn/version "0.16.4"}}
:main-opts ["-m" "cognitect.test-runner"]
:exec-fn cognitect.test-runner.api/test}}}
24 changes: 20 additions & 4 deletions src/navi/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,26 @@
(let [content-fn (if (= "uuid" (.getFormat schema))
uuid?
string?)
pattern (.getPattern schema)]
(if pattern
[:and content-fn (re-pattern pattern)]
content-fn)))
max-length (.getMaxLength schema)
min-length (.getMinLength schema)
properties (cond-> nil
max-length (assoc :max max-length)
min-length (assoc :min min-length))
pattern (some-> schema .getPattern re-pattern)]
(cond
(and properties pattern)
[:and content-fn [:string properties] pattern]

(and properties (= string? content-fn))
[:string properties]

properties
[:and content-fn [:string properties]]

pattern
[:and content-fn pattern]

:else content-fn)))

(defmethod spec
"integer"
Expand Down
24 changes: 16 additions & 8 deletions test/navi/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

(ns navi.core-test
(:require [clojure.test :refer [deftest testing is]]
[malli.core :as m]
[navi.core :as core])
(:import [clojure.lang ExceptionInfo]
[io.swagger.v3.oas.models Operation PathItem]
Expand Down Expand Up @@ -127,14 +128,21 @@
(is (#{[:or string? int?] [:or int? string?]}
(core/schema->spec strint)))))
(testing "regex string"
(let [[kw f regex] (core/schema->spec (doto (Schema.)
(.addType "string")
(.setPattern "^(\\d+)([KMGTPE]i{0,1})$")))]
(is (= :and kw))
(is (= string? f))
(is (instance? java.util.regex.Pattern regex))
(is (some? (re-matches regex "1024Ki")))
(is (nil? (re-matches regex "1024Kib"))))))
(let [spec (core/schema->spec (doto (Schema.)
(.addType "string")
(.setPattern "^(\\d+)([KMGTPE]i{0,1})$")))]
(is (m/validate spec "1024Ki"))
(is (not (m/validate spec "1024Kib"))))
(testing "minLength and maxLength"
(let [spec (core/schema->spec (doto (Schema.)
(.addType "string")
(.setMinLength (int 3))
(.setMaxLength (int 8))))]
(is (not (m/validate spec "")))
(is (not (m/validate spec "1")))
(is (m/validate spec "123"))
(is (m/validate spec "12345678"))
(is (not (m/validate spec "123456789")))))))

(deftest responses-to-malli-spec
(testing "empty response"
Expand Down

0 comments on commit 34be813

Please sign in to comment.