Using strong params with dry-validation

This may be a very bad idea, but I wanted to use strong params with dry-validation, so I wrote an extention to get strong param permittance hash from dry-validation params:

module StrongParamsSchemaKeyMap
  def strong_params
    keys.map(&:strong_params)
  end
end

module StrongParamsSchemaKey
  def strong_params
    name
  end
end

module StrongParamsSchemaKeyHash
  def strong_params
    {name => members.map(&:strong_params)}
  end
end

module StrongParamsSchemaKeyArray
  def strong_params
    {name => member.map(&:strong_params)}
  end
end

Dry::Schema::KeyMap.include(StrongParamsSchemaKeyMap)
Dry::Schema::Key.include(StrongParamsSchemaKey)
Dry::Schema::Key::Hash.include(StrongParamsSchemaKeyHash)
Dry::Schema::Key::Array.include(StrongParamsSchemaKeyArray)

This works like KeyMap#dump, but returns something that works with permit! method.

In my ApplicationValidator:

  def call(input)
    super case input
    when ActionController::Parameters
      allowed_keys = schema.key_map.strong_params

      input.permit(allowed_keys).to_h.with_indifferent_access
    when Hash
      input.deep_symbolize_keys
    end
  end

Turns out, if I declare an array in my schema

      class CancellationRequestValidator < ApplicationValidator
        params do
          required(:id).filled(:integer)
          required(:reason_id).filled(:integer)
          optional(:file_ids).array(:integer)
        end
      end

key_map.strong_params return ["id", "reason_id", "file_ids"] instead of ["id", "reason_id", file_ids: []].

Is there a workaround, maybe a way to get key type from schema, or should I just give up and use permit! ?

If you’re using Dry::Validation already, I honestly can’t think of a good reason why you should use strong_params at all. Dry::Validation does everything strong_params can do and a lot more.

Honestly, I don’t know what I was thinking. Thanks.