-
Notifications
You must be signed in to change notification settings - Fork 9
Add support for json schema string with format metadata #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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? | ||
"date" string? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could use uri? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
@@ -93,6 +111,11 @@ | |
:else | ||
content-fn))) | ||
|
||
(extend-protocol p/Transformable | ||
StringSchema | ||
(p/transform [schema] | ||
(transform-string schema)) | ||
|
||
DateSchema | ||
(p/transform [_] inst?) | ||
|
||
|
@@ -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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bytes?
There was a problem hiding this comment.
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