File tree 3 files changed +40
-1
lines changed
lib/grape/validations/validators
spec/grape/validations/validators
3 files changed +40
-1
lines changed Original file line number Diff line number Diff line change 13
13
#### Fixes
14
14
15
15
* [ #2328 ] ( https://github.com/ruby-grape/grape/pull/2328 ) : Don't cache Class.instance_methods - [ @byroot ] ( https://github.com/byroot ) .
16
+ * [ #2337 ] ( https://github.com/ruby-grape/grape/pull/2337 ) : Fix: allow custom validators that do not end with _ validator - [ @ericproulx ] ( https://github.com/ericproulx ) .
16
17
* Your contribution here.
17
18
18
19
## 1.7.1 (2023/05/14)
Original file line number Diff line number Diff line change @@ -64,7 +64,7 @@ def validate!(params)
64
64
def self . inherited ( klass )
65
65
return if klass . name . blank?
66
66
67
- short_validator_name = klass . name . demodulize . underscore . delete_suffix! ( '_validator' )
67
+ short_validator_name = klass . name . demodulize . underscore . delete_suffix ( '_validator' )
68
68
Validations . register_validator ( short_validator_name , klass )
69
69
end
70
70
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ RSpec . describe Grape ::Validations ::Validators ::Base do
4
+ describe '#inherited' do
5
+ context 'when validator is anonymous' do
6
+ subject ( :custom_validator ) { Class . new ( described_class ) }
7
+
8
+ it 'does not register the validator' do
9
+ expect ( Grape ::Validations ) . not_to receive ( :register_validator )
10
+ custom_validator
11
+ end
12
+ end
13
+
14
+ # Anonymous class does not have a name and class A < B would leak.
15
+ # Simulates inherited callback
16
+ context "when validator's underscored name does not end with _validator" do
17
+ subject ( :custom_validator ) { described_class . inherited ( TestModule ::CustomValidatorABC ) }
18
+
19
+ before { stub_const ( 'TestModule::CustomValidatorABC' , Class . new ) }
20
+
21
+ it 'registers the custom validator with a short name' do
22
+ expect ( Grape ::Validations ) . to receive ( :register_validator ) . with ( 'custom_validator_abc' , TestModule ::CustomValidatorABC )
23
+ custom_validator
24
+ end
25
+ end
26
+
27
+ context "when validator's underscored name ends with _validator" do
28
+ subject ( :custom_validator ) { described_class . inherited ( TestModule ::CustomValidator ) }
29
+
30
+ before { stub_const ( 'TestModule::CustomValidator' , Class . new ) }
31
+
32
+ it 'registers the custom validator with short name not ending with validator' do
33
+ expect ( Grape ::Validations ) . to receive ( :register_validator ) . with ( 'custom' , TestModule ::CustomValidator )
34
+ custom_validator
35
+ end
36
+ end
37
+ end
38
+ end
You can’t perform that action at this time.
0 commit comments