Skip to content

Commit 34be813

Browse files
john-shafferlispyclouds
authored andcommitted
Add minLength and maxLength support
- 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.
1 parent d6af7d2 commit 34be813

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

deps.edn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{:deps {io.swagger.parser.v3/swagger-parser {:mvn/version "2.1.22"}}
88
:aliases {:test {:extra-paths ["test"]
99
:extra-deps {io.github.cognitect-labs/test-runner
10-
{:git/tag "v0.5.1" :git/sha "dfb30dd"}}
10+
{:git/tag "v0.5.1" :git/sha "dfb30dd"}
11+
metosin/malli {:mvn/version "0.16.4"}}
1112
:main-opts ["-m" "cognitect.test-runner"]
1213
:exec-fn cognitect.test-runner.api/test}}}

src/navi/core.clj

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,26 @@
9595
(let [content-fn (if (= "uuid" (.getFormat schema))
9696
uuid?
9797
string?)
98-
pattern (.getPattern schema)]
99-
(if pattern
100-
[:and content-fn (re-pattern pattern)]
101-
content-fn)))
98+
max-length (.getMaxLength schema)
99+
min-length (.getMinLength schema)
100+
properties (cond-> nil
101+
max-length (assoc :max max-length)
102+
min-length (assoc :min min-length))
103+
pattern (some-> schema .getPattern re-pattern)]
104+
(cond
105+
(and properties pattern)
106+
[:and content-fn [:string properties] pattern]
107+
108+
(and properties (= string? content-fn))
109+
[:string properties]
110+
111+
properties
112+
[:and content-fn [:string properties]]
113+
114+
pattern
115+
[:and content-fn pattern]
116+
117+
:else content-fn)))
102118

103119
(defmethod spec
104120
"integer"

test/navi/core_test.clj

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
(ns navi.core-test
88
(:require [clojure.test :refer [deftest testing is]]
9+
[malli.core :as m]
910
[navi.core :as core])
1011
(:import [clojure.lang ExceptionInfo]
1112
[io.swagger.v3.oas.models Operation PathItem]
@@ -127,14 +128,21 @@
127128
(is (#{[:or string? int?] [:or int? string?]}
128129
(core/schema->spec strint)))))
129130
(testing "regex string"
130-
(let [[kw f regex] (core/schema->spec (doto (Schema.)
131-
(.addType "string")
132-
(.setPattern "^(\\d+)([KMGTPE]i{0,1})$")))]
133-
(is (= :and kw))
134-
(is (= string? f))
135-
(is (instance? java.util.regex.Pattern regex))
136-
(is (some? (re-matches regex "1024Ki")))
137-
(is (nil? (re-matches regex "1024Kib"))))))
131+
(let [spec (core/schema->spec (doto (Schema.)
132+
(.addType "string")
133+
(.setPattern "^(\\d+)([KMGTPE]i{0,1})$")))]
134+
(is (m/validate spec "1024Ki"))
135+
(is (not (m/validate spec "1024Kib"))))
136+
(testing "minLength and maxLength"
137+
(let [spec (core/schema->spec (doto (Schema.)
138+
(.addType "string")
139+
(.setMinLength (int 3))
140+
(.setMaxLength (int 8))))]
141+
(is (not (m/validate spec "")))
142+
(is (not (m/validate spec "1")))
143+
(is (m/validate spec "123"))
144+
(is (m/validate spec "12345678"))
145+
(is (not (m/validate spec "123456789")))))))
138146

139147
(deftest responses-to-malli-spec
140148
(testing "empty response"

0 commit comments

Comments
 (0)