-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Creating custom validators is difficult #2012
Comments
@stanhu First of all there is documentation about the required parse method. Probably, we need to update internal classes, a PR is welcome 🙂 I know that Dry-types supports custom types, however, I don't think we need to couple Grape and Dry-types that match. Maintenance of Virtus was dropped, we don't know what might happen to Dry-Types in the future. Also, there are differences in coercing, you might've seen this method, so custom types maintained by Dry-types could bring breaking changes. In general, what can be simpler than class Color
attr_reader :value
def initialize(color)
@value = color
end
def self.parse(value)
fail 'Invalid color' unless %w(blue red green).include?(value)
new(value)
end
end A simple plain Ruby class which everyone can understand without checking documentation. 😉 |
You could roll out a grape-dry-types gem that makes the connection, even if trivial. |
@dnesteryuk Ah, thank you! I don't know how I missed that block of documentation. I'll look at improving the internal classes to reflect this. |
@dnesteryuk Actually, the example above doesn't work because
I think we need to update the documentation to reflect that? Something like: class Color
attr_reader :value
def initialize(color)
@value = color
end
def self.parse(value)
fail 'Invalid color' unless self.coerced?(value)
new(value)
end
def self.coerced?(value)
%w(blue red green).include?(value)
end
end |
I'll revise my statement because it's not right. dry-types has stronger type checking now, so if the class Color
attr_reader :value
def initialize(color)
@value = color
end
def self.parse(value)
fail 'Invalid color' unless self.parsed?(value)
new SomeThing(value)
end
def self.parsed?(value)
%w(blue red green).include?(value)
end
end That's because the following block comes into play: grape/lib/grape/validations/types/custom_type_coercer.rb Lines 96 to 110 in 8deebb5
|
What do you think we shall we do with this issue, @stanhu? |
* Provide an example for taking an existing type and porting it to the dry-types. This would have saved me some time. * Removed examples of using `Virtus.model`. It seems confusing to support dry-types and Virtus at the same time. Closes ruby-grape#2012
* Provide an example for taking an existing type and porting it to the dry-types. This would have saved me some time. * Removed examples of using `Virtus.model`. It seems confusing to support dry-types and Virtus at the same time. Closes ruby-grape#2012
Thank you. Opened #2030 for a release, add +1. |
I'm attempting to port a custom validator to Grape v1.3.1:
At first, I tried to take https://github.com/ruby-grape/grape/blob/41ee988c06b87b036d12c4c7247d06b75bfa0b7f/lib/grape/validations/types/file.rb as an example:
In Grape v1.1.0, this worked (ignore the
coerced_value?
vs.coerced?
change and lack of inheritance ofVirtus::Attribute
) becauseBuildCoercer.create_coercer_instance
would just fall through and create a Virtus Attribute: https://github.com/ruby-grape/grape/blob/v1.1.0/lib/grape/validations/types/build_coercer.rb#L71.In Grape v1.3.1, with the switch to dry-types, all unknown types fall through to
PrimitiveCoercer
and fails:grape/lib/grape/validations/types/build_coercer.rb
Line 67 in 812284f
super
ingrape/lib/grape/validations/types/primitive_coercer.rb
Line 42 in 812284f
@coercer[val]
ingrape/lib/grape/validations/types/dry_type_coercer.rb
Line 30 in 812284f
@coercer
isnil
.To get around this, I saw the
custom?
check appears to look forself.parse
:grape/lib/grape/validations/types.rb
Lines 134 to 135 in 41ee988
This appears to work for my purposes, but it seems inconsistent (not to mention undocumented) compared to the built-in validators present in Grape.
Note that dry-types has a simple mechanism for checking types: https://dry-rb.org/gems/dry-types/master/custom-types. Ideally, we'd be able to pass in one of these custom types so we don't need all that code above, but this doesn't work either for the same reasons above:
@dnesteryuk My questions are:
Json
,File
, and custom ones (e.g. class vs. instance methods,self.parse
method)?The text was updated successfully, but these errors were encountered: