From 23ab494f2b6fb4b219cc4df363ab050a19a036ea Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Sun, 27 Oct 2024 18:51:02 +0100 Subject: [PATCH 1/4] 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 [] --- lib/grape/validations/contract_scope.rb | 29 +++++++++++-------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/grape/validations/contract_scope.rb b/lib/grape/validations/contract_scope.rb index 0255051b45..3b66df5722 100644 --- a/lib/grape/validations/contract_scope.rb +++ b/lib/grape/validations/contract_scope.rb @@ -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. @@ -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 From f0fcf49a4b55d93c8a29c9e1a22041c96880022a Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Sun, 27 Oct 2024 18:56:33 +0100 Subject: [PATCH 2/4] Add CHANGELOG.md Add inherits spec --- CHANGELOG.md | 1 + spec/integration/dry_validation/dry_validation_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a66dacc322..37ff1642b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/spec/integration/dry_validation/dry_validation_spec.rb b/spec/integration/dry_validation/dry_validation_spec.rb index d7b2f8efab..f1ec6baf95 100644 --- a/spec/integration/dry_validation/dry_validation_spec.rb +++ b/spec/integration/dry_validation/dry_validation_spec.rb @@ -62,6 +62,12 @@ end describe 'Grape::Validations::ContractScope' do + describe '.inherits' do + subject { described_class } + + it { is_expected.to be < Grape::Validations::Validators::Base } + end + let(:validated_params) { {} } let(:app) do vp = validated_params From db8afd6b5958c2248bb75aac9ea1b6f950c19f61 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Sun, 27 Oct 2024 18:57:56 +0100 Subject: [PATCH 3/4] Fix cop --- .../dry_validation/dry_validation_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/integration/dry_validation/dry_validation_spec.rb b/spec/integration/dry_validation/dry_validation_spec.rb index f1ec6baf95..699891d324 100644 --- a/spec/integration/dry_validation/dry_validation_spec.rb +++ b/spec/integration/dry_validation/dry_validation_spec.rb @@ -62,12 +62,6 @@ end describe 'Grape::Validations::ContractScope' do - describe '.inherits' do - subject { described_class } - - it { is_expected.to be < Grape::Validations::Validators::Base } - end - let(:validated_params) { {} } let(:app) do vp = validated_params @@ -79,6 +73,12 @@ end end + describe '.inherits' do + subject { described_class } + + it { is_expected.to be < Grape::Validations::Validators::Base } + end + context 'with simple schema, pre-defined' do let(:contract) do Dry::Schema.Params do From 2a72175c2d0b88c39f8b89ee708ee49a58ceba68 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Sun, 27 Oct 2024 18:59:56 +0100 Subject: [PATCH 4/4] Fix spec --- .../dry_validation/dry_validation_spec.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/spec/integration/dry_validation/dry_validation_spec.rb b/spec/integration/dry_validation/dry_validation_spec.rb index 699891d324..6333bdacdd 100644 --- a/spec/integration/dry_validation/dry_validation_spec.rb +++ b/spec/integration/dry_validation/dry_validation_spec.rb @@ -73,12 +73,6 @@ end end - describe '.inherits' do - subject { described_class } - - it { is_expected.to be < Grape::Validations::Validators::Base } - end - context 'with simple schema, pre-defined' do let(:contract) do Dry::Schema.Params do @@ -242,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