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 ContractScope validator + small tweaks #2510

Merged
merged 4 commits into from
Oct 28, 2024
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 @@
* [#2504](https://github.com/ruby-grape/grape/pull/2504): Fix leaky modules in specs - [@ericproulx](https://github.com/ericproulx).
* [#2506](https://github.com/ruby-grape/grape/pull/2506): Fix fetch_formatter api_format - [@ericproulx](https://github.com/ericproulx).
* [#2507](https://github.com/ruby-grape/grape/pull/2507): Fix type: Set with values - [@nikolai-b](https://github.com/nikolai-b).
* [#2510](https://github.com/ruby-grape/grape/pull/2510): Fix ContractScope's validator inheritance - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

### 2.2.0 (2024-09-14)
Expand Down
29 changes: 13 additions & 16 deletions lib/grape/validations/contract_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ def initialize(api, contract = nil, &block)

validator_options = {
validator_class: Validator,
opts: { schema: contract }
opts: { schema: contract, fail_fast: false }
}

api.namespace_stackable(:validations, validator_options)
end

class Validator
class Validator < Grape::Validations::Validators::Base
attr_reader :schema

def initialize(*_args, schema:)
@schema = schema
def initialize(_attrs, _options, _required, _scope, opts)
super
@schema = opts.fetch(:schema)
end

# Validates a given request.
Expand All @@ -49,21 +50,17 @@ def validate(request)
return
end

errors = []

res.errors.messages.each do |message|
full_name = message.path.first.to_s
raise Grape::Exceptions::ValidationArrayErrors.new(build_errors_from_messages(res.errors.messages))
end

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

errors << Grape::Exceptions::Validation.new(params: [full_name], message: message.text)
def build_errors_from_messages(messages)
messages.map do |message|
full_name = message.path.first.to_s
full_name << "[#{message.path[1..].join('][')}]" if message.path.size > 1
Grape::Exceptions::Validation.new(params: [full_name], message: message.text)
end

raise Grape::Exceptions::ValidationArrayErrors.new(errors)
end

def fail_fast?
false
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/integration/dry_validation/dry_validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,12 @@
end
end
end

describe Grape::Validations::ContractScope::Validator do
describe '.inherits' do
subject { described_class }

it { is_expected.to be < Grape::Validations::Validators::Base }
end
end
end