Skip to content

Commit 898c429

Browse files
authored
Add support for json schema string with format metadata (#13)
Dedicated transform-string function to handle various string formats Both StringSchema and JsonSchema (with "string" type) use a new transform-string helper. This enables structured handling of formats like uuid, date, and email, while preserving support for maxLength, pattern, and enumerations.
1 parent deb08b9 commit 898c429

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

src/navi/transform.clj

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,35 @@
6666
(map p/transform)
6767
(into [compose-as]))))
6868

69-
(extend-protocol p/Transformable
70-
StringSchema
71-
(p/transform [schema]
72-
(let [content-fn string?
73-
max-length (.getMaxLength schema)
74-
min-length (.getMinLength schema)
75-
properties (cond-> nil
76-
max-length (assoc :max max-length)
77-
min-length (assoc :min min-length))
78-
pattern (some-> schema .getPattern re-pattern)
79-
enums (into [:enum] (.getEnum schema))]
69+
(defn format->malli-predicate
70+
"Given an OpenAPI string format, return a suitable Malli predicate for basic validation."
71+
[fmt]
72+
(case fmt
73+
"uuid" uuid?
74+
"binary" string?
75+
"byte" string?
76+
"date" string?
77+
"date-time" string?
78+
"password" string?
79+
"email" string?
80+
"uri" string?
81+
"hostname" string?
82+
"ipv4" string?
83+
"ipv6" string?
84+
string?))
85+
86+
(defn transform-string
87+
"Given a StringSchema or a JsonSchema that we know is string-typed,
88+
return a Malli schema that respects format, length constraints, pattern, and enum."
89+
[^Schema schema]
90+
(let [content-fn (format->malli-predicate (.getFormat schema))
91+
max-length (.getMaxLength schema)
92+
min-length (.getMinLength schema)
93+
properties (cond-> nil
94+
max-length (assoc :max max-length)
95+
min-length (assoc :min min-length))
96+
pattern (some-> schema .getPattern re-pattern)
97+
enums (into [:enum] (.getEnum schema))]
8098
(cond
8199
(and properties pattern)
82100
[:and content-fn [:string properties] pattern]
@@ -93,6 +111,11 @@
93111
:else
94112
content-fn)))
95113

114+
(extend-protocol p/Transformable
115+
StringSchema
116+
(p/transform [schema]
117+
(transform-string schema))
118+
96119
DateSchema
97120
(p/transform [_] inst?)
98121

@@ -133,7 +156,7 @@
133156
"null" nil?
134157
"number" number?
135158
"object" (transform-object schema)
136-
"string" string?
159+
"string" (transform-string schema)
137160
(throw (IllegalArgumentException. (format "Unsupported type %s for schema %s" typ schema)))))
138161
types (.getTypes schema)]
139162
(case (count types)

0 commit comments

Comments
 (0)