controller
controller copied to clipboard
Duplicate error messages when using `config.validate_keys = true` in params validation
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
- Create an action with params validation that has
config.validate_keys = true - Define required parameters
- Send a request with extra parameters that are not defined in the params block
- 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"] }