Skip to content
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 custom validator not ending with _validator #2337

Merged
merged 4 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#### Fixes

* [#2328](https://github.com/ruby-grape/grape/pull/2328): Don't cache Class.instance_methods - [@byroot](https://github.com/byroot).
* [#2337](https://github.com/ruby-grape/grape/pull/2337): Fix: allow custom validators that do not end with _validator - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

## 1.7.1 (2023/05/14)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def validate!(params)
def self.inherited(klass)
return if klass.name.blank?

short_validator_name = klass.name.demodulize.underscore.delete_suffix!('_validator')
short_validator_name = klass.name.demodulize.underscore.delete_suffix('_validator')
Validations.register_validator(short_validator_name, klass)
end

Expand Down
38 changes: 38 additions & 0 deletions spec/grape/validations/validators/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

RSpec.describe Grape::Validations::Validators::Base do
describe '#inherited' do
context 'when validator is anonymous' do
subject(:custom_validator) { Class.new(described_class) }

it 'does not register the validator' do
expect(Grape::Validations).not_to receive(:register_validator)
custom_validator
end
end

# Anonymous class does not have a name and class A < B would leak.
# Simulates inherited callback
context "when validator's underscored name does not end with _validator" do
subject(:custom_validator) { described_class.inherited(TestModule::CustomValidatorABC) }

before { stub_const('TestModule::CustomValidatorABC', Class.new) }

it 'registers the custom validator with a short name' do
expect(Grape::Validations).to receive(:register_validator).with('custom_validator_abc', TestModule::CustomValidatorABC)
custom_validator
end
end

context "when validator's underscored name ends with _validator" do
subject(:custom_validator) { described_class.inherited(TestModule::CustomValidator) }

before { stub_const('TestModule::CustomValidator', Class.new) }

it 'registers the custom validator with short name not ending with validator' do
expect(Grape::Validations).to receive(:register_validator).with('custom', TestModule::CustomValidator)
custom_validator
end
end
end
end