-
Notifications
You must be signed in to change notification settings - Fork 88
Fix transform with default value #93
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
Fix transform with default value #93
Conversation
@1075461847 thank you for opening this PR. context "when param is optional & not present" do
it "doesn't transform the value" do
allow(controller).to receive(:params).and_return({ })
expect { controller.param! :foo, String, required: true, transform: :upcase }.not_to raise_error
end
end Under the current implementation, I would expect this test to fail with |
@iMacTia I think it will throw an InvalidParameterError instead of an NoMethodError when an optional parameter is not provided, the param! method has validate presence before apply transformation # validate presence
if params[name].nil? && options[:required]
raise InvalidParameterError.new(
"Parameter #{parameter_name} is required",
param: parameter_name,
options: options
)
end
``` |
@iMacTia I've added a spec to test it |
spec/rails_param/param_spec.rb
Outdated
context "when param is optional & not present" do | ||
it "doesn't transform the value" do | ||
allow(controller).to receive(:params).and_return({ }) | ||
expect { controller.param! :foo, String, required: true, transform: :upcase }.to raise_error(RailsParam::InvalidParameterError, "Parameter foo is required") |
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.
Isn't this testing the same thing as the spec above it? The context says the param is optional but has required: true
, which is confusing me.
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.
Yes, you're right, I didn't see the above case, now I've modified it to fit the context.
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.
Really sorry, I copy-pasted from the test above and forgot to make the parameter optional 🙈
@1075461847 I've just merged #94 in |
d273dac
to
4c1bf6c
Compare
@iMacTia I've rebase master. |
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.
Thank you @1075461847, to be honest I'm quite surprised the test we added is passing, but there might be something else going on here.
Regardless, you made a good addition and all tests are green, so I'll merge your PR as-is and investigate further before shipping in a new release 👍
As it turns out, we have the following guard at the beginning of the return unless params.include?(name) || check_param_presence?(options[:default]) || options[:required] Since the parameter is not present, optional ( |
Description
Fixes #92