-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As we know Virtus isn't maintained anymore, so it makes sense to use something different for coercing. There is a dry-types lib which was created as a replacement for Virtus (at least a part of it). Although, it is only about coercion. This change gets rid of Virtus and adds dry-types to the stack. The structure inside didn't change match. Dry-types was integrated without huge refactoring. If people want to use Virtus for custom types, it is possible after implementing a parse method which initializes a model. class User include Virtus.model attribute :id, Integer attribute :name, String def self.parse(*args) new(*args) end end
- Loading branch information
1 parent
fb11882
commit 9b352a8
Showing
30 changed files
with
473 additions
and
388 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
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
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
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,54 @@ | ||
require_relative 'dry_type_coercer' | ||
|
||
module Grape | ||
module Validations | ||
module Types | ||
# Coerces elements in an array. It might be an array of strings or integers or | ||
# anything else. | ||
# | ||
# It could've been possible to use an +of+ | ||
# method (https://dry-rb.org/gems/dry-types/1.2/array-with-member/) | ||
# provided by dry-types. Unfortunately, it doesn't work for Grape because of | ||
# behavior of Virtus which was used earlier, a `Grape::Validations::Types::PrimitiveCoercer` | ||
# maintains Virtus behavior in coercing. | ||
class ArrayCoercer < DryTypeCoercer | ||
def initialize(type, strict = false) | ||
super | ||
|
||
@coercer = scope::Array | ||
@elem_coercer = PrimitiveCoercer.new(type.first, strict) | ||
end | ||
|
||
def call(_val) | ||
collection = super | ||
|
||
return collection if collection.is_a?(InvalidValue) | ||
|
||
coerce_elements collection | ||
end | ||
|
||
protected | ||
|
||
def coerce_elements(collection) | ||
collection.each_with_index do |elem, index| | ||
return InvalidValue.new if reject?(elem) | ||
|
||
coerced_elem = @elem_coercer.call(elem) | ||
|
||
return coerced_elem if coerced_elem.is_a?(InvalidValue) | ||
|
||
collection[index] = coerced_elem | ||
end | ||
|
||
collection | ||
end | ||
|
||
# This method maintaine logic which was defined by Virtus for arrays. | ||
# Virtus doesn't allow nil in arrays. | ||
def reject?(val) | ||
val.nil? | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.