diff --git a/src/malli/json_schema.cljc b/src/malli/json_schema.cljc index 9011500c2..ef3db923f 100644 --- a/src/malli/json_schema.cljc +++ b/src/malli/json_schema.cljc @@ -124,6 +124,36 @@ :minItems :maxItems)) +(defmethod accept :* [_ _ children _] + {:type "array", :items (first children)}) + +(defmethod accept :? [_ _ children _] + {:type "array", :items (first children), + :maxItems 1}) + +(defmethod accept :+ [_ _ children _] + {:type "array", :items (first children), + :minItems 1}) + +(defmethod accept :repeat [_ schema children _] + (minmax-properties + {:type "array", :items (first children)} + schema + :minItems + :maxItems)) + +(defmethod accept :cat [_ _ children _] + {:type "array", :items children, :additionalItems false}) + +(defmethod accept :catn [_ _ children _] + {:type "array", :items (mapv last children), :additionalItems false}) + +(defmethod accept :alt [_ _ children _] + {:type "array", :items {:oneOf children}, :additionalItems false}) + +(defmethod accept :altn [_ _ children _] + {:type "array", :items {:oneOf (mapv last children)}, :additionalItems false}) + (defmethod accept :set [_ schema children _] (minmax-properties {:type "array", :items (first children), :uniqueItems true} @@ -190,3 +220,6 @@ (let [definitions (atom {}) options (merge options {::m/walk-entry-vals true, ::definitions definitions, ::transform -transform})] (cond-> (-transform ?schema options) (seq @definitions) (assoc :definitions @definitions))))) + +(comment + (m/validate [:* [:catn [:f :int] [:s :string]]] [2 "asdf" 3 "asdf"])) diff --git a/test/malli/json_schema_test.cljc b/test/malli/json_schema_test.cljc index b6b3d5a07..3a8076ede 100644 --- a/test/malli/json_schema_test.cljc +++ b/test/malli/json_schema_test.cljc @@ -51,6 +51,41 @@ :additionalProperties {:type "string"}}] [[:vector string?] {:type "array", :items {:type "string"}}] [[:sequential string?] {:type "array", :items {:type "string"}}] + [[:sequential {:min 1, :max 4} string?] {:type "array", + :items {:type "string"}, + :minItems 1, + :maxItems 4}] + [[:* string?] {:type "array", :items {:type "string"}}] + [[:? string?] {:type "array", + :items {:type "string"}, + :maxItems 1}] + [[:+ string?] {:type "array", + :items {:type "string"}, + :minItems 1}] + [[:repeat string?] {:type "array", :items {:type "string"}}] + [[:repeat {:min 1, :max 4} string?] {:type "array", + :items {:type "string"}, + :minItems 1, + :maxItems 4}] + [[:repeat {:min 1, :max 4} [:tuple string? keyword?]] {:type "array", + :items {:additionalItems false, + :items [{:type "string"}, + {:type "string"}], + :type "array"} + :minItems 1, + :maxItems 4}] + [[:cat :int :string] {:type "array", + :items [{:type "integer"} {:type "string"}], + :additionalItems false}] + [[:catn [:f :int] [:s :string]] {:type "array", + :items [{:type "integer"} {:type "string"}], + :additionalItems false}] + [[:alt :int :string] {:type "array", + :items {:oneOf [{:type "integer"} {:type "string"}]}, + :additionalItems false}] + [[:altn [:f :int] [:s :string]] {:type "array", + :items {:oneOf [{:type "integer"} {:type "string"}]}, + :additionalItems false}] [[:set string?] {:type "array" :items {:type "string"} :uniqueItems true}]