From 05a5d13a79283c01566ea8d6a847a50429972ad6 Mon Sep 17 00:00:00 2001 From: Noah Bogart Date: Thu, 30 Jun 2022 16:57:20 -0400 Subject: [PATCH] Set additionalProperties if map is closed --- src/malli/json_schema.cljc | 7 +++++-- test/malli/json_schema_test.cljc | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) 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]]))))