File tree 2 files changed +34
-4
lines changed
2 files changed +34
-4
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ module RelationshipType
5
5
extend T ::Sig
6
6
extend T ::Helpers
7
7
8
+ InvalidRelationshipSpec = Class . new ( StandardError )
9
+
8
10
abstract!
9
11
10
12
# This enables `is_a?` check type to sorbet
@@ -25,6 +27,7 @@ module RelationshipType
25
27
sig { params ( spec : T ::Hash [ String , T . untyped ] ) . void }
26
28
def initialize ( spec )
27
29
@spec = spec
30
+ validate_spec!
28
31
end
29
32
30
33
sig { abstract . returns ( String ) }
@@ -110,7 +113,7 @@ def field
110
113
111
114
sig { returns ( Symbol ) }
112
115
def primary_key
113
- @spec [ "primary_key" ] &.to_sym || :id
116
+ @spec [ "primary_key" ] &.to_sym
114
117
end
115
118
116
119
sig do
@@ -127,5 +130,22 @@ def valid_relationship_field?(read_dto)
127
130
sig { overridable . returns ( T . nilable ( Symbol ) ) }
128
131
def relationship_field_name
129
132
end
133
+
134
+ private
135
+
136
+ sig { void }
137
+ def validate_spec!
138
+ errors = [ ]
139
+ errors << "name is required" unless @spec [ "name" ]
140
+ errors << "resource_id is required" unless @spec [ "resource_id" ]
141
+ errors << "field is required" unless @spec [ "field" ]
142
+ errors << "primary_key is required" unless @spec [ "primary_key" ]
143
+ errors << "name must be a string" unless @spec [ "name" ] . is_a? ( String )
144
+
145
+ return if errors . empty?
146
+
147
+ Kernel . raise InvalidRelationshipSpec ,
148
+ "The given relationship spec is not valid: #{ errors . join ( ", " ) } . The spec is: #{ @spec } "
149
+ end
130
150
end
131
151
end
Original file line number Diff line number Diff line change 6
6
7
7
RSpec . describe ResourceRegistry ::Relationship do
8
8
describe "#load" do
9
- let ( :type ) { ResourceRegistry ::RelationshipTypes ::HasOne . new ( { } ) }
10
9
let ( :params ) do
11
10
{
12
11
"name" => "test" ,
13
- "type" => type . serialize ,
12
+ "type" => "has_one" ,
14
13
"field" => :test ,
15
14
"resource_id" => :test ,
16
- "optional" => true
15
+ "optional" => true ,
16
+ "primary_key" => :test
17
17
}
18
18
end
19
19
20
20
subject { described_class . load ( params ) }
21
21
22
22
it { expect ( subject ) . to be_a ( described_class ) }
23
23
24
+ context "when the spec is not valid" do
25
+ let ( :params ) { { "type" => "has_one" } }
26
+
27
+ it "raises an error" do
28
+ expect { subject } . to raise_error (
29
+ ResourceRegistry ::RelationshipType ::InvalidRelationshipSpec
30
+ )
31
+ end
32
+ end
33
+
24
34
# TODO: Check how to test this
25
35
xcontext "with a custom type" do
26
36
let ( :type ) { "policy_resolution" }
You can’t perform that action at this time.
0 commit comments