Skip to content

Commit

Permalink
Merge pull request #719 from NoahTheDuke/nb/additional-properties
Browse files Browse the repository at this point in the history
Set additionalProperties if map is closed
  • Loading branch information
ikitommi authored Jul 28, 2022
2 parents 51f75ff + 05a5d13 commit 09b18be
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 09b18be

Please sign in to comment.