-
Notifications
You must be signed in to change notification settings - Fork 122
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
Implement ActiveModel::Validations::Confirm compiler #1644
Implement ActiveModel::Validations::Confirm compiler #1644
Conversation
8df0b1c
to
2e7702f
Compare
2e7702f
to
592b369
Compare
lib/tapioca/dsl/compilers/active_model_validations_confirmation.rb
Outdated
Show resolved
Hide resolved
lib/tapioca/dsl/compilers/active_model_validations_confirmation.rb
Outdated
Show resolved
Hide resolved
# Create RBI definitions for all the attributes that use confirmation validation | ||
confirmation_validators.each do |validator| | ||
validator.attributes.each do |attr_name| | ||
klass.create_method("#{attr_name}_confirmation", return_type: "T.untyped") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to figure out the right types based on the columns?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the it's a subclass of ActiveRecord::Base, we can probably use one of ActiveRecord's API to figure out the column type and translate it to a proper return type. The same could be said for classes that implement ActiveModel::Attributes.
However, since this validations can be added to POROs, we'd have to add logic to differentiate the two. Are we ok with doing that here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Do these validations require you to use Active Model attributes or something that would allow us to fetch the type?
If not, I think it's fine to use T.untyped
. Just trying to think if there's a way to get better typing support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do these validations require you to use Active Model attributes or something that would allow us to fetch the type?
Unfortunately no, they only require `ActiveModel::Validations
class Foo
attr_reader :foo
def initialize(foo)
@foo = foo
end
include ActiveModel::Validations
validates_confirmation_of :foo
end
f = Foo.new("foo")
f.foo_confirmation = "bar"
f.valid?
=> false
f.errors
=> #<ActiveModel::Errors [#<ActiveModel::Error attribute=foo_confirmation, type=confirmation, options={:attribute=>"Foo"}>]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Yeah, it seems like this can be used in pretty much any context, let's go with T.untyped
. Thanks for investigating!
# Create RBI definitions for all the attributes that use confirmation validation | ||
confirmation_validators.each do |validator| | ||
validator.attributes.each do |attr_name| | ||
klass.create_method("#{attr_name}_confirmation", return_type: "T.untyped") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Yeah, it seems like this can be used in pretty much any context, let's go with T.untyped
. Thanks for investigating!
It appears you need to run |
All fixed 🙇 |
I think you might need to rebase the branch because of #1643. There are some typechecking and test errors. |
3ff555a
to
71d0647
Compare
All done @vinistock 🙇 |
It seems that typechecking is still failing. |
@vinistock looks like this will be fixed by #1648? |
Okay, rebasing now should fix the CI. |
This validation generates a virtual attribute that allows for checking confirmation logic over the given attribute name
Co-authored-by: Vinicius Stock <[email protected]>
71d0647
to
4c49c4a
Compare
Done! |
Thank you for the contribution! |
Motivation
Given a class implementing a
confirmation
validation fromActiveModel::Validations
will generate virtual attributes
password_confirmation
andpassword_confirmation=
. Without this compiler, usage of those methods will cause an error with Sorbet unless they are shimmed out.Implementation
The compiler is pretty straightforward iterating over all validators of a gathered constant, selecting only those that implement
ConfirmationValidator
and generating the necessary methods for each of the attributes in therbi
file. It also skipsActiveRecord::Base
since that class includesActiveModel::Validations
but does not implement any of its validations.Tests
Tests were added