diff --git a/src/malli/json_schema.cljc b/src/malli/json_schema.cljc index 5952689be..131bf976c 100644 --- a/src/malli/json_schema.cljc +++ b/src/malli/json_schema.cljc @@ -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)}) diff --git a/test/malli/json_schema_test.cljc b/test/malli/json_schema_test.cljc index 28896d4b0..380838b6c 100644 --- a/test/malli/json_schema_test.cljc +++ b/test/malli/json_schema_test.cljc @@ -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]]))))