Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from graphql-devise/add-mount-method-option-sa…
…nitizer Add mount method option sanitizer
- Loading branch information
Showing
18 changed files
with
449 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require_relative 'supported_options' | ||
|
||
module GraphqlDevise | ||
module MountMethod | ||
class OptionSanitizer | ||
def initialize(options = {}, supported_options = MountMethod::SUPPORTED_OPTIONS) | ||
@options = options | ||
@supported_options = supported_options | ||
end | ||
|
||
def call! | ||
@supported_options.each_with_object(Struct.new(*@supported_options.keys).new) do |(key, checker), result| | ||
result[key] = checker.call!(@options[key], key) | ||
end | ||
end | ||
end | ||
end | ||
end |
26 changes: 26 additions & 0 deletions
26
lib/graphql_devise/mount_method/option_sanitizers/array_checker.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module GraphqlDevise | ||
module MountMethod | ||
module OptionSanitizers | ||
class ArrayChecker | ||
def initialize(element_type) | ||
@element_type = element_type | ||
@default_value = [] | ||
end | ||
|
||
def call!(value, key) | ||
return @default_value if value.blank? | ||
|
||
unless value.instance_of?(Array) | ||
raise GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. Array expected." | ||
end | ||
|
||
unless value.all? { |element| element.instance_of?(@element_type) } | ||
raise GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has invalid elements. #{@element_type} expected." | ||
end | ||
|
||
value | ||
end | ||
end | ||
end | ||
end | ||
end |
26 changes: 26 additions & 0 deletions
26
lib/graphql_devise/mount_method/option_sanitizers/class_checker.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module GraphqlDevise | ||
module MountMethod | ||
module OptionSanitizers | ||
class ClassChecker | ||
def initialize(klass) | ||
@klass_array = Array(klass) | ||
end | ||
|
||
def call!(value, key) | ||
return if value.nil? | ||
|
||
unless value.instance_of?(Class) | ||
raise GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. Class expected." | ||
end | ||
|
||
unless @klass_array.any? { |klass| value.ancestors.include?(klass) } | ||
raise GraphqlDevise::InvalidMountOptionsError, | ||
"`#{key}` option has an invalid value. #{@klass_array.join(', ')} or descendants expected. Got #{value}." | ||
end | ||
|
||
value | ||
end | ||
end | ||
end | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
lib/graphql_devise/mount_method/option_sanitizers/hash_checker.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module GraphqlDevise | ||
module MountMethod | ||
module OptionSanitizers | ||
class HashChecker | ||
def initialize(element_type_array) | ||
@element_type_array = Array(element_type_array) | ||
@default_value = {} | ||
end | ||
|
||
def call!(value, key) | ||
return @default_value if value.blank? | ||
|
||
unless value.instance_of?(Hash) | ||
raise GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. Hash expected. Got #{value.class}." | ||
end | ||
|
||
value.each { |internal_key, klass| ClassChecker.new(@element_type_array).call!(klass, "#{key} -> #{internal_key}") } | ||
|
||
value | ||
end | ||
end | ||
end | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
lib/graphql_devise/mount_method/option_sanitizers/string_checker.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module GraphqlDevise | ||
module MountMethod | ||
module OptionSanitizers | ||
class StringChecker | ||
def initialize(default_string = nil) | ||
@default_string = default_string | ||
end | ||
|
||
def call!(value, key) | ||
return @default_string if value.blank? | ||
|
||
unless value.instance_of?(String) | ||
raise GraphqlDevise::InvalidMountOptionsError, "`#{key}` option has an invalid value. String expected." | ||
end | ||
|
||
value | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require_relative 'option_sanitizers/array_checker' | ||
require_relative 'option_sanitizers/hash_checker' | ||
require_relative 'option_sanitizers/string_checker' | ||
require_relative 'option_sanitizers/class_checker' | ||
|
||
module GraphqlDevise | ||
module MountMethod | ||
SUPPORTED_OPTIONS = { | ||
at: OptionSanitizers::StringChecker.new('/graphql_auth'), | ||
operations: OptionSanitizers::HashChecker.new([GraphQL::Schema::Resolver, GraphQL::Schema::Mutation]), | ||
only: OptionSanitizers::ArrayChecker.new(Symbol), | ||
skip: OptionSanitizers::ArrayChecker.new(Symbol), | ||
additional_queries: OptionSanitizers::HashChecker.new(GraphQL::Schema::Resolver), | ||
additional_mutations: OptionSanitizers::HashChecker.new(GraphQL::Schema::Mutation), | ||
authenticatable_type: OptionSanitizers::ClassChecker.new(GraphQL::Schema::Member) | ||
}.freeze | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe GraphqlDevise::MountMethod::OptionSanitizer do | ||
subject(:clean_options) { described_class.new(options, supported_options).call! } | ||
|
||
describe '#call!' do | ||
let(:supported_options) do | ||
{ | ||
my_string: GraphqlDevise::MountMethod::OptionSanitizers::StringChecker.new('default string'), | ||
hash_multiple: GraphqlDevise::MountMethod::OptionSanitizers::HashChecker.new([String, Numeric]), | ||
array: GraphqlDevise::MountMethod::OptionSanitizers::ArrayChecker.new(Symbol), | ||
hash_single: GraphqlDevise::MountMethod::OptionSanitizers::HashChecker.new(Float), | ||
my_class: GraphqlDevise::MountMethod::OptionSanitizers::ClassChecker.new(Numeric) | ||
} | ||
end | ||
|
||
context 'when all options are provided and correct' do | ||
let(:options) do | ||
{ | ||
my_string: 'non default', | ||
hash_multiple: { first: String, second: Float, third: Float }, | ||
array: [:one, :two, :three], | ||
hash_single: { first: Float, second: Float }, | ||
my_class: Float | ||
} | ||
end | ||
|
||
it 'returns a struct with clean options' do | ||
expect( | ||
my_string: clean_options.my_string, | ||
hash_multiple: clean_options.hash_multiple, | ||
array: clean_options.array, | ||
hash_single: clean_options.hash_single, | ||
my_class: clean_options.my_class | ||
).to match( | ||
my_string: 'non default', | ||
hash_multiple: { first: String, second: Float, third: Float }, | ||
array: [:one, :two, :three], | ||
hash_single: { first: Float, second: Float }, | ||
my_class: Float | ||
) | ||
end | ||
end | ||
|
||
context 'when some options are provided but all correct' do | ||
let(:options) do | ||
{ | ||
hash_multiple: { first: String, second: Float, third: Float }, | ||
array: [:one, :two, :three], | ||
my_class: Float | ||
} | ||
end | ||
|
||
it 'returns a struct with clean options and default values' do | ||
expect( | ||
my_string: clean_options.my_string, | ||
hash_multiple: clean_options.hash_multiple, | ||
array: clean_options.array, | ||
hash_single: clean_options.hash_single, | ||
my_class: clean_options.my_class | ||
).to match( | ||
my_string: 'default string', | ||
hash_multiple: { first: String, second: Float, third: Float }, | ||
array: [:one, :two, :three], | ||
hash_single: {}, | ||
my_class: Float | ||
) | ||
end | ||
end | ||
|
||
context 'when an option provided is invalid' do | ||
let(:options) do | ||
{ | ||
hash_multiple: { first: String, second: Float, third: Float }, | ||
array: ['not symbol 1', 'not symbol 2'], | ||
my_class: Float | ||
} | ||
end | ||
|
||
it 'raises an error' do | ||
expect { clean_options }.to raise_error(GraphqlDevise::InvalidMountOptionsError, '`array` option has invalid elements. Symbol expected.') | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.