Skip to content

Commit 137f8f8

Browse files
authored
Merge pull request #8 from rcserrano06/improvements/handle_object
Verse Schema: handle Hash and Tempfile schema & fix description
2 parents 863d9d2 + a54ff38 commit 137f8f8

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

lib/verse/schema/json.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def self._build_schema(schema, registry:, definitions:)
4949

5050
obj[field_obj.name] = begin
5151
output = _from_schema(field_obj.type, registry:, definitions:)
52-
desc = field_obj.opts.dig(:meta, :description)
52+
desc = field_obj.opts.dig(:meta, :description) || field_obj.opts.dig(:meta, :desc)
5353

5454
output[:description] = desc if desc
5555

@@ -152,6 +152,14 @@ def self._build_schema(schema, registry:, definitions:)
152152
else
153153
{ anyOf: schema.map { |v| _from_schema(v, registry:, definitions:) } }
154154
end
155+
when Hash.singleton_class
156+
{ type: "object" }
157+
when IO.singleton_class, Tempfile.singleton_class
158+
{
159+
type: "object",
160+
instanceof: "IO",
161+
description: "A native IO stream or file pointer"
162+
}
155163
else
156164
if schema == Object
157165
{} # any type

spec/verse/schema/json_spec.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@
3333
})
3434
end
3535

36+
it "converts a schema with meta description/desc" do
37+
schema = Verse::Schema.define do
38+
field(:title, String).meta(description: "The title of the item")
39+
field(:count, Integer).meta(desc: "The count of items")
40+
end
41+
42+
json_schema = described_class.from(schema)
43+
expect(json_schema).to eq({
44+
type: "object",
45+
properties: {
46+
title: {
47+
type: "string",
48+
description: "The title of the item"
49+
},
50+
count: {
51+
type: "integer",
52+
description: "The count of items"
53+
}
54+
},
55+
required: [:title, :count],
56+
additionalProperties: false
57+
})
58+
end
59+
3660
it "converts a schema with nested structs" do
3761
schema = Verse::Schema.define do
3862
field(:user) do
@@ -288,6 +312,48 @@
288312
})
289313
end
290314

315+
it "converts a hash schema" do
316+
schema = Verse::Schema.define do
317+
field(:settings, Hash)
318+
end
319+
320+
json_schema = described_class.from(schema)
321+
expect(json_schema).to eq({
322+
type: "object",
323+
properties: {
324+
settings: { type: "object" }
325+
},
326+
required: [:settings],
327+
additionalProperties: false
328+
})
329+
end
330+
331+
it "converts an IO/Tempfile schema" do
332+
schema = Verse::Schema.define do
333+
field(:file, IO)
334+
field(:tempfile, Tempfile)
335+
end
336+
337+
json_schema = described_class.from(schema)
338+
expect(json_schema).to eq({
339+
type: "object",
340+
properties: {
341+
file: {
342+
type: "object",
343+
instanceof: "IO",
344+
description: "A native IO stream or file pointer"
345+
},
346+
tempfile: {
347+
type: "object",
348+
instanceof: "IO",
349+
description: "A native IO stream or file pointer"
350+
}
351+
},
352+
required: [:file, :tempfile],
353+
additionalProperties: false
354+
})
355+
end
356+
291357
it "converts a custom schema" do
292358
class CustomSchemaType
293359
def to_json_schema

0 commit comments

Comments
 (0)