Skip to content

Commit a35b54b

Browse files
committed
Add prefixItems field to Schema
Based on definition from [1] where it's a list of schemas that defaults to an empty array [1]: https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-10.3.1.1
1 parent 70b10af commit a35b54b

4 files changed

Lines changed: 46 additions & 1 deletion

File tree

json-schema-for-3.1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ then - single schema - done
8181
else - single schema - done
8282
dependentSchemas - map of schemas - somewhat complex, is it related to dependentRequired ?
8383

84-
prefixItems: array of schema
84+
prefixItems: array of schema - done
8585
items: array of schema - in 3.0
8686
contains: schema
8787

lib/openapi3_parser/node/schema/v3_1.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ def then
6969
def else
7070
self["else"]
7171
end
72+
73+
# @return [Node::Array<Schema>]
74+
def prefix_items
75+
self["prefixItems"]
76+
end
7277
end
7378
end
7479
end

lib/openapi3_parser/node_factory/schema/v3_1.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class V3_1 < NodeFactory::Object # rubocop:disable Naming/ClassAndModuleCamelCas
3232
field "if", factory: :referenceable_schema
3333
field "then", factory: :referenceable_schema
3434
field "else", factory: :referenceable_schema
35+
field "prefixItems", factory: :prefix_items_factory
3536

3637
def build_node(data, node_context)
3738
Node::Schema::V3_1.new(data, node_context)
@@ -77,6 +78,13 @@ def validate_type(validatable)
7778
validatable.add_error("type must be a string or an array")
7879
end
7980
end
81+
82+
def prefix_items_factory(context)
83+
NodeFactory::Array.new(
84+
context,
85+
value_factory: NodeFactory::Schema.factory(context)
86+
)
87+
end
8088
end
8189
end
8290
end

spec/lib/openapi3_parser/node_factory/schema/v3_1_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,36 @@
167167
.with_message('"not a media type" is not a valid media type')
168168
end
169169
end
170+
171+
describe "prefixItems field" do
172+
it "is valid with an array with schema values" do
173+
instance = described_class.new(
174+
create_node_factory_context({ "prefixItems" => [{ "type" => "string" }] })
175+
)
176+
177+
expect(instance).to be_valid
178+
end
179+
180+
it "is invalid for a type other than array" do
181+
instance = described_class.new(
182+
create_node_factory_context({ "prefixItems" => "string" })
183+
)
184+
185+
expect(instance).not_to be_valid
186+
expect(instance)
187+
.to have_validation_error("#/prefixItems")
188+
.with_message("Invalid type. Expected Array")
189+
end
190+
191+
it "is invalid for values other than objects" do
192+
instance = described_class.new(
193+
create_node_factory_context({ "prefixItems" => %w[string] })
194+
)
195+
196+
expect(instance).not_to be_valid
197+
expect(instance)
198+
.to have_validation_error("#/prefixItems/0")
199+
.with_message("Invalid type. Expected Object")
200+
end
201+
end
170202
end

0 commit comments

Comments
 (0)