|
66 | 66 | (map p/transform) |
67 | 67 | (into [compose-as])))) |
68 | 68 |
|
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))] |
80 | 98 | (cond |
81 | 99 | (and properties pattern) |
82 | 100 | [:and content-fn [:string properties] pattern] |
|
93 | 111 | :else |
94 | 112 | content-fn))) |
95 | 113 |
|
| 114 | +(extend-protocol p/Transformable |
| 115 | + StringSchema |
| 116 | + (p/transform [schema] |
| 117 | + (transform-string schema)) |
| 118 | + |
96 | 119 | DateSchema |
97 | 120 | (p/transform [_] inst?) |
98 | 121 |
|
|
133 | 156 | "null" nil? |
134 | 157 | "number" number? |
135 | 158 | "object" (transform-object schema) |
136 | | - "string" string? |
| 159 | + "string" (transform-string schema) |
137 | 160 | (throw (IllegalArgumentException. (format "Unsupported type %s for schema %s" typ schema))))) |
138 | 161 | types (.getTypes schema)] |
139 | 162 | (case (count types) |
|
0 commit comments