Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sequence Schemas to JSON Schema generation #795

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 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
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