Skip to content

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

@ybougo

Description

@ybougo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions