Skip to content

Commit 702fb6a

Browse files
committed
Add Sequence Schemas to JSON Schema generation
1 parent cda6742 commit 702fb6a

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/malli/json_schema.cljc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,36 @@
124124
:minItems
125125
:maxItems))
126126

127+
(defmethod accept :* [_ _ children _]
128+
{:type "array", :items (first children)})
129+
130+
(defmethod accept :? [_ _ children _]
131+
{:type "array", :items (first children),
132+
:maxItems 1})
133+
134+
(defmethod accept :+ [_ _ children _]
135+
{:type "array", :items (first children),
136+
:minItems 1})
137+
138+
(defmethod accept :repeat [_ schema children _]
139+
(minmax-properties
140+
{:type "array", :items (first children)}
141+
schema
142+
:minItems
143+
:maxItems))
144+
145+
(defmethod accept :cat [_ _ children _]
146+
{:type "array", :items children, :additionalItems false})
147+
148+
(defmethod accept :catn [_ _ children _]
149+
{:type "array", :items (mapv last children), :additionalItems false})
150+
151+
(defmethod accept :alt [_ _ children _]
152+
{:type "array", :items {:oneOf children}, :additionalItems false})
153+
154+
(defmethod accept :altn [_ _ children _]
155+
{:type "array", :items {:oneOf (mapv last children)}, :additionalItems false})
156+
127157
(defmethod accept :set [_ schema children _]
128158
(minmax-properties
129159
{:type "array", :items (first children), :uniqueItems true}

test/malli/json_schema_test.cljc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,41 @@
5151
:additionalProperties {:type "string"}}]
5252
[[:vector string?] {:type "array", :items {:type "string"}}]
5353
[[:sequential string?] {:type "array", :items {:type "string"}}]
54+
[[:sequential {:min 1, :max 4} string?] {:type "array",
55+
:items {:type "string"},
56+
:minItems 1,
57+
:maxItems 4}]
58+
[[:* string?] {:type "array", :items {:type "string"}}]
59+
[[:? string?] {:type "array",
60+
:items {:type "string"},
61+
:maxItems 1}]
62+
[[:+ string?] {:type "array",
63+
:items {:type "string"},
64+
:minItems 1}]
65+
[[:repeat string?] {:type "array", :items {:type "string"}}]
66+
[[:repeat {:min 1, :max 4} string?] {:type "array",
67+
:items {:type "string"},
68+
:minItems 1,
69+
:maxItems 4}]
70+
[[:repeat {:min 1, :max 4} [:tuple string? keyword?]] {:type "array",
71+
:items {:additionalItems false,
72+
:items [{:type "string"},
73+
{:type "string"}],
74+
:type "array"}
75+
:minItems 1,
76+
:maxItems 4}]
77+
[[:cat :int :string] {:type "array",
78+
:items [{:type "integer"} {:type "string"}],
79+
:additionalItems false}]
80+
[[:catn [:f :int] [:s :string]] {:type "array",
81+
:items [{:type "integer"} {:type "string"}],
82+
:additionalItems false}]
83+
[[:alt :int :string] {:type "array",
84+
:items {:oneOf [{:type "integer"} {:type "string"}]},
85+
:additionalItems false}]
86+
[[:altn [:f :int] [:s :string]] {:type "array",
87+
:items {:oneOf [{:type "integer"} {:type "string"}]},
88+
:additionalItems false}]
5489
[[:set string?] {:type "array"
5590
:items {:type "string"}
5691
:uniqueItems true}]

0 commit comments

Comments
 (0)