Skip to content

Commit f43d88e

Browse files
committed
Add methods for boolean schemas
I wasn't sure whether to add methods to the existing class or to create a new class. I'll see how this pans out.
1 parent 07dabbf commit f43d88e

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

lib/openapi3_parser/node/schema/v3_1.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,42 @@ class Schema < Node::Object
1515
# these complexities and focuses on the core schema as defined in:
1616
# https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01
1717
class V3_1 < Schema # rubocop:disable Naming/ClassAndModuleCamelCase
18+
# Whether this is a schema that is just a boolean value rather
19+
# than a schema object
20+
#
21+
# @return [Boolean]
22+
def boolean?
23+
!boolean.nil?
24+
end
25+
26+
# Returns a boolean for a boolean schema [1] and nil for one based
27+
# on an object
28+
#
29+
# [1]: https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-4.3.2
30+
#
31+
# @return [Boolean, nil]
32+
def boolean
33+
self["boolean"]
34+
end
35+
36+
# Returns true when this is a boolean schema that has a true value,
37+
# returns false for booleans schemas that have a false value or schemas
38+
# that are objects.
39+
#
40+
# @return [Boolean]
41+
def true?
42+
boolean == true
43+
end
44+
45+
# Returns false when this is a boolean schema that has a false value,
46+
# returns false for booleans schemas that have a true value or schemas
47+
# that are objects.
48+
#
49+
# @return [Boolean]
50+
def false?
51+
boolean == false
52+
end
53+
1854
# @return [String, Node::Array<String>, nil]
1955
def type
2056
self["type"]

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,51 @@
33
RSpec.describe Openapi3Parser::Node::Schema::V3_1 do
44
it_behaves_like "schema node", openapi_version: "3.1.0"
55

6+
describe "boolean methods" do
7+
let(:instance) do
8+
factory_context = create_node_factory_context(input)
9+
10+
Openapi3Parser::NodeFactory::Schema::V3_1
11+
.new(factory_context)
12+
.node(node_factory_context_to_node_context(factory_context))
13+
end
14+
15+
context "when given a boolean schema with a true value" do
16+
let(:input) { true }
17+
18+
it "identifies as a boolean" do
19+
expect(instance.boolean?).to be(true)
20+
expect(instance.boolean).to be(true)
21+
expect(instance.true?).to be(true)
22+
expect(instance.false?).to be(false)
23+
end
24+
end
25+
26+
context "when given a boolean schema with a false value" do
27+
let(:input) { false }
28+
29+
it "identifies as a boolean" do
30+
expect(instance.boolean?).to be(true)
31+
expect(instance.boolean).to be(false)
32+
expect(instance.true?).to be(false)
33+
expect(instance.false?).to be(true)
34+
end
35+
end
36+
37+
context "when given an object schema" do
38+
let(:input) { { "type" => "string" } }
39+
40+
it "does not identify as a boolean" do
41+
expect(instance.boolean?).to be(false)
42+
expect(instance.boolean).to be_nil
43+
expect(instance.true?).to be(false)
44+
expect(instance.false?).to be(false)
45+
end
46+
end
47+
end
48+
649
%i[if then else].each do |method_name|
7-
describe method_name.to_s do
50+
describe "##{method_name}" do
851
it "supports a Ruby reserved word as a method name" do
952
factory_context = create_node_factory_context(
1053
{ method_name.to_s => { "type" => "string" } },

0 commit comments

Comments
 (0)