Skip to content

Commit

Permalink
Set additionalProperties if map is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahTheDuke committed Jun 30, 2022
1 parent bb52213 commit 05a5d13
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/malli/json_schema.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@
(defmethod accept :orn [_ _ children _] {:anyOf (map last children)})

(defmethod accept ::m/val [_ _ children _] (first children))
(defmethod accept :map [_ _ children _]
(defmethod accept :map [_ schema children _]
(let [required (->> children (filter (m/-comp not :optional second)) (mapv first))
additional-properties (:closed (m/properties schema))
object {:type "object"
:properties (apply array-map (mapcat (fn [[k _ s]] [k s]) children))}]
(if (empty? required) object (assoc object :required required))))
(cond-> object
(seq required) (assoc :required required)
additional-properties (assoc :additionalProperties false))))

(defmethod accept :multi [_ _ children _] {:oneOf (mapv last children)})

Expand Down
7 changes: 7 additions & 0 deletions test/malli/json_schema_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,10 @@

(deftest function-schema-test
(is (= {} (json-schema/transform [:=> [:cat int? int?] int?]))))

(deftest additional-properties-test
(is (= {:type "object"
:properties {:name {:type "string"}}
:required [:name]
:additionalProperties false}
(json-schema/transform [:map {:closed true} [:name :string]]))))

0 comments on commit 05a5d13

Please sign in to comment.