Skip to content

Commit

Permalink
Add Sequence Schemas to JSON Schema generation
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahTheDuke committed Dec 6, 2022
1 parent cda6742 commit e11d828
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/malli/json_schema.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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"]))
35 changes: 35 additions & 0 deletions test/malli/json_schema_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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}]
Expand Down

0 comments on commit e11d828

Please sign in to comment.