Skip to content

Added support for coercion to Set or Set[Other] #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/grape/validations/coerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ def _valid_single_type?(klass, val)
end

def valid_type?(val)
if @option.is_a?(Array)
_valid_array_type?(@option[0], val)
if @option.is_a?(Array) || @option.is_a?(Set)
_valid_array_type?(@option.first, val)
else
_valid_single_type?(@option, val)
end
end

def coerce_value(type, val)
# Don't coerce things other than nil to Arrays or Hashes
return val || [] if type == Array
return val || {} if type == Hash
return val || [] if type == Array
return val || Set.new if type == Set
return val || {} if type == Hash

converter = Virtus::Attribute.build(type)
converter.coerce(val)
Expand Down