Skip to content

Commit 0477baf

Browse files
authored
Fix ContractScope validator + small tweaks (#2510)
* ContractScope's validator inherits from Grape::Validations::Validator::Base initialize method has been updated accordingly opts adds fail_fast Use << when concatenating string Use map instead of each [] * Add CHANGELOG.md Add inherits spec * Fix cop * Fix spec
1 parent 139e96b commit 0477baf

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* [#2504](https://github.com/ruby-grape/grape/pull/2504): Fix leaky modules in specs - [@ericproulx](https://github.com/ericproulx).
1414
* [#2506](https://github.com/ruby-grape/grape/pull/2506): Fix fetch_formatter api_format - [@ericproulx](https://github.com/ericproulx).
1515
* [#2507](https://github.com/ruby-grape/grape/pull/2507): Fix type: Set with values - [@nikolai-b](https://github.com/nikolai-b).
16+
* [#2510](https://github.com/ruby-grape/grape/pull/2510): Fix ContractScope's validator inheritance - [@ericproulx](https://github.com/ericproulx).
1617
* Your contribution here.
1718

1819
### 2.2.0 (2024-09-14)

lib/grape/validations/contract_scope.rb

+13-16
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ def initialize(api, contract = nil, &block)
2424

2525
validator_options = {
2626
validator_class: Validator,
27-
opts: { schema: contract }
27+
opts: { schema: contract, fail_fast: false }
2828
}
2929

3030
api.namespace_stackable(:validations, validator_options)
3131
end
3232

33-
class Validator
33+
class Validator < Grape::Validations::Validators::Base
3434
attr_reader :schema
3535

36-
def initialize(*_args, schema:)
37-
@schema = schema
36+
def initialize(_attrs, _options, _required, _scope, opts)
37+
super
38+
@schema = opts.fetch(:schema)
3839
end
3940

4041
# Validates a given request.
@@ -49,21 +50,17 @@ def validate(request)
4950
return
5051
end
5152

52-
errors = []
53-
54-
res.errors.messages.each do |message|
55-
full_name = message.path.first.to_s
53+
raise Grape::Exceptions::ValidationArrayErrors.new(build_errors_from_messages(res.errors.messages))
54+
end
5655

57-
full_name += "[#{message.path[1..].join('][')}]" if message.path.size > 1
56+
private
5857

59-
errors << Grape::Exceptions::Validation.new(params: [full_name], message: message.text)
58+
def build_errors_from_messages(messages)
59+
messages.map do |message|
60+
full_name = message.path.first.to_s
61+
full_name << "[#{message.path[1..].join('][')}]" if message.path.size > 1
62+
Grape::Exceptions::Validation.new(params: [full_name], message: message.text)
6063
end
61-
62-
raise Grape::Exceptions::ValidationArrayErrors.new(errors)
63-
end
64-
65-
def fail_fast?
66-
false
6764
end
6865
end
6966
end

spec/integration/dry_validation/dry_validation_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,12 @@
236236
end
237237
end
238238
end
239+
240+
describe Grape::Validations::ContractScope::Validator do
241+
describe '.inherits' do
242+
subject { described_class }
243+
244+
it { is_expected.to be < Grape::Validations::Validators::Base }
245+
end
246+
end
239247
end

0 commit comments

Comments
 (0)