hanami / hanami/hanami-action

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

Open
#474 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Ruby
Stars
242
Forks
123
PR merge metrics
No merged PRs in 30d

Description

## 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:
```ruby
{ errors: { foo: ["is not allowed"] } }
```

## Actual Behavior

Each disallowed key appears multiple times with duplicate error messages:
```ruby
{ errors: { foo: ["is not allowed", "is not allowed"] } }
```

## Code Example

```ruby
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"] }
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the duplicate errors using Hanami params validation with config.validate_keys = true and an extra key such as foo. Start by tracing the params validation path; add a regression test for the request example, and consider the issue done when each disallowed key appears once with "is not allowed".

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.