controller icon indicating copy to clipboard operation
controller copied to clipboard

Duplicate error messages when using `config.validate_keys = true` in params validation

Open ybougo opened this issue 6 months ago • 0 comments

Bug Description

When using config.validate_keys = true in Hanami params validation, extra/disallowed parameters produce duplicate error messages in the validation errors hash.

Environment

  • Hanami version: ~> 2.2
  • hanami-controller version: ~> 2.2
  • Ruby version: 3.4.4

Reproduction Steps

  1. Create an action with params validation that has config.validate_keys = true
  2. Define required parameters
  3. Send a request with extra parameters that are not defined in the params block
  4. Check the validation errors

Expected Behavior

Each disallowed key should appear only once in the errors hash:

{ errors: { foo: ["is not allowed"] } }

Actual Behavior

Each disallowed key appears multiple times with duplicate error messages:

{ errors: { foo: ["is not allowed", "is not allowed"] } }

Code Example

module Bookshelf
  module Actions
    module Home
      class Index < Bookshelf::Action
        params do
          config.validate_keys = true
          required(:name).filled(:string)
        end

        def handle(request, response)
          halt 422, { errors: request.params.errors } unless request.params.valid?
          response.body = "Welcome to Bookshelf, #{request.params[:name]}!"
        end
      end
    end
  end
end

Test Case

# Request: GET /?name=Bookshelf&foo=bar
# Expected errors: { foo: ["is not allowed"] }
# Actual errors: { foo: ["is not allowed", "is not allowed"] }

ybougo avatar Jul 08 '25 01:07 ybougo