Skip to content

Commit 0753bb2

Browse files
committed
Improve HashChecker. Reuse ClassChecker.
1 parent fce8114 commit 0753bb2

6 files changed

Lines changed: 30 additions & 19 deletions

File tree

lib/graphql_devise/mount_method/option_sanitizers/class_checker.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module MountMethod
33
module OptionSanitizers
44
class ClassChecker
55
def initialize(klass)
6-
@klass = klass
6+
@klass_array = Array(klass)
77
end
88

99
def call!(value, key)
@@ -13,8 +13,9 @@ def call!(value, key)
1313
raise GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. Class expected."
1414
end
1515

16-
unless value.ancestors.include?(@klass)
17-
raise GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. #{@klass} expected. Got #{value}."
16+
unless @klass_array.any? { |klass| value.ancestors.include?(klass) }
17+
raise GraphqlDevise::InvalidMountOptionsError,
18+
"`#{key}` option has an invalid value. #{@klass_array.join(', ')} or descendants expected. Got #{value}."
1819
end
1920

2021
value

lib/graphql_devise/mount_method/option_sanitizers/hash_checker.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ def call!(value, key)
1414
raise GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. Hash expected. Got #{value.class}."
1515
end
1616

17-
unless value.all? { |_, element| element.instance_of?(Class) && @element_type_array.any? { |type| element.ancestors.include?(type) } }
18-
raise GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has invalid elements. [#{@element_type_array.join(', ')}] or descendants expected. Got #{value}."
19-
end
17+
value.each { |internal_key, klass| ClassChecker.new(@element_type_array).call!(klass, "#{key} -> #{internal_key}") }
2018

2119
value
2220
end

spec/services/mount_method/option_sanitizers/array_checker_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
context 'when provided value is not an array' do
1717
let(:value) { 'not an array' }
1818

19-
it 'railses an error' do
19+
it 'raises an error' do
2020
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. Array expected.")
2121
end
2222
end
2323

2424
context 'when provided array contains invalid elements' do
2525
let(:value) { [:valid, 'invalid'] }
2626

27-
it 'railses an error' do
27+
it 'raises an error' do
2828
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has invalid elements. #{element_type} expected.")
2929
end
3030
end

spec/services/mount_method/option_sanitizers/class_checker_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
context 'when provided value is not a class' do
1717
let(:value) { 'I\'m not a class' }
1818

19-
it 'railses an error' do
19+
it 'raises an error' do
2020
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. Class expected.")
2121
end
2222
end
2323

2424
context 'when provided class is not of the expected type' do
2525
let(:value) { String }
2626

27-
it 'railses an error' do
28-
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. #{expected_class} expected. Got String.")
27+
it 'raises an error' do
28+
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. #{expected_class} or descendants expected. Got String.")
2929
end
3030
end
3131

spec/services/mount_method/option_sanitizers/hash_checker_spec.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let(:key) { :any_option }
88

99
context 'when a single valid type is provided' do
10-
let(:element_type) { String }
10+
let(:element_type) { Numeric }
1111

1212
context 'when no value is provided' do
1313
let(:value) { nil }
@@ -24,22 +24,28 @@
2424
end
2525

2626
context 'when provided hash contains invalid elements' do
27-
let(:value) { { valid: String, invalid: Float } }
27+
let(:value) { { valid: Float, invalid: String } }
2828

2929
it 'raises an error' do
30-
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has invalid elements. [#{element_type}] or descendants expected. Got #{value}.")
30+
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key} -> invalid` option has an invalid value. #{element_type} or descendants expected. Got #{String}.")
3131
end
3232
end
3333

3434
context 'when provided array contains all valid elements' do
35-
let(:value) { { valid1: String, valid2: String } }
35+
let(:value) { { valid1: Numeric, valid2: Numeric } }
36+
37+
it { is_expected.to eq(value) }
38+
end
39+
40+
context 'when provided class has the expected type as an acestor' do
41+
let(:value) { { valid: Float } }
3642

3743
it { is_expected.to eq(value) }
3844
end
3945
end
4046

4147
context 'when multiple value types are allowed' do
42-
let(:element_type) { [String, Float] }
48+
let(:element_type) { [String, Numeric] }
4349

4450
context 'when no value is provided' do
4551
let(:value) { nil }
@@ -48,7 +54,13 @@
4854
end
4955

5056
context 'when provided array contains all valid elements' do
51-
let(:value) { { valid1: String, valid2: Float } }
57+
let(:value) { { valid1: String, valid2: Numeric } }
58+
59+
it { is_expected.to eq(value) }
60+
end
61+
62+
context 'when provided class has the expected type as an acestor' do
63+
let(:value) { { valid: Float } }
5264

5365
it { is_expected.to eq(value) }
5466
end
@@ -65,7 +77,7 @@
6577
let(:value) { { valid: String, invalid: StandardError } }
6678

6779
it 'raises an error' do
68-
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has invalid elements. [#{element_type.join(', ')}] or descendants expected. Got #{value}.")
80+
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key} -> invalid` option has an invalid value. #{element_type.join(', ')} or descendants expected. Got #{StandardError}.")
6981
end
7082
end
7183
end

spec/services/mount_method/option_sanitizers/string_checker_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
context 'when provided value is not a String' do
1717
let(:value) { 1000 }
1818

19-
it 'railses an error' do
19+
it 'raises an error' do
2020
expect { clean_value }.to raise_error(GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. String expected.")
2121
end
2222
end

0 commit comments

Comments
 (0)