Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions src/navi/transform.clj
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,35 @@
(map p/transform)
(into [compose-as]))))

(extend-protocol p/Transformable
StringSchema
(p/transform [schema]
(let [content-fn string?
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)
enums (into [:enum] (.getEnum schema))]
(defn format->malli-predicate
"Given an OpenAPI string format, return a suitable Malli predicate for basic validation."
[fmt]
(case fmt
"uuid" uuid?
"binary" string?
"byte" string?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytes?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for this, test coercion in the server

"date" string?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

date and date-time can use inst?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this needs a transform date-time fn too when #11 comes in

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have #11 merged now, can use the date schemas from that.

"date-time" string?
"password" string?
"email" string?
"uri" string?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use uri?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we test it a bit in a reitit server to see what type the coercion produces?

"hostname" string?
"ipv4" string?
"ipv6" string?
string?))

(defn transform-string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can make this private

"Given a StringSchema or a JsonSchema that we know is string-typed,
return a Malli schema that respects format, length constraints, pattern, and enum."
[^Schema schema]
(let [content-fn (format->malli-predicate (.getFormat schema))
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)
enums (into [:enum] (.getEnum schema))]
(cond
(and properties pattern)
[:and content-fn [:string properties] pattern]
Expand All @@ -93,6 +111,11 @@
:else
content-fn)))

(extend-protocol p/Transformable
StringSchema
(p/transform [schema]
(transform-string schema))

DateSchema
(p/transform [_] inst?)

Expand Down Expand Up @@ -133,7 +156,7 @@
"null" nil?
"number" number?
"object" (transform-object schema)
"string" string?
"string" (transform-string schema)
(throw (IllegalArgumentException. (format "Unsupported type %s for schema %s" typ schema)))))
types (.getTypes schema)]
(case (count types)
Expand Down