# How to remove empty Strings from \`params\` Hash

**URL:** <https://discourse.dry-rb.org/t/how-to-remove-empty-strings-from-params-hash/1648>\
**Category:** dry-validation\
**Created:** [April 8, 2023, 2:51am UTC](https://discourse.dry-rb.org/t/how-to-remove-empty-strings-from-params-hash/1648 "2023-04-08T02:51:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![postmodern](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/postmodern/32/696_2.png) [@postmodern](https://discourse.dry-rb.org/u/postmodern)\
**Post date:** [April 8, 2023, 2:51am UTC](https://discourse.dry-rb.org/t/how-to-remove-empty-strings-from-params-hash/1648/1 "2023-04-08T02:51:34Z")

</div>

How do I use dry-validation or dry-schema to remove empty Strings from a HTTP `params` Hash entirely? Instead of coercing them to `nil`, I would prefer to just omit them entirely, so that the `params` Hash is much smaller before sending it to a backend Sidekiq worker.

---

<div class="post-metadata">

**Author:** ![cpgo](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/cpgo/32/81_2.png) [@cpgo](https://discourse.dry-rb.org/u/cpgo)\
**Post date:** [April 11, 2023, 2:08pm UTC](https://discourse.dry-rb.org/t/how-to-remove-empty-strings-from-params-hash/1648/2 "2023-04-11T14:08:14Z")

</div>

I think you can use this [dry-rb - dry-schema v1.10 - Processor steps](https://dry-rb.org/gems/dry-schema/1.10/advanced/processor-steps/)

If what you want to do is remove keys with empty values the example is doing exactly that.

---

<div class="post-metadata">

**Author:** ![postmodern](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/postmodern/32/696_2.png) [@postmodern](https://discourse.dry-rb.org/u/postmodern)\
**Post date:** [April 11, 2023, 6:49pm UTC](https://discourse.dry-rb.org/t/how-to-remove-empty-strings-from-params-hash/1648/3 "2023-04-11T18:49:52Z")

</div>

I guess I could call `.reject { |k,v| v.empty? }`.

```auto
schema = Dry::Schema.Params do
  required(:name).value(:string)
  optional(:age).value(:integer)

  before(:value_coercer) do |result|
    result.to_h.reject { |k,v| v.empty? }
  end
end

schema.call(name: 'Jane', age: '')
# => #<Dry::Schema::Result{:name=>"Jane"} errors={} path=[]>

```

Is this the ideal way of filtering out empty Strings?

---

<div class="post-metadata">

**Author:** ![cpgo](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/cpgo/32/81_2.png) [@cpgo](https://discourse.dry-rb.org/u/cpgo)\
**Post date:** [April 12, 2023, 11:36am UTC](https://discourse.dry-rb.org/t/how-to-remove-empty-strings-from-params-hash/1648/4 "2023-04-12T11:36:54Z")

</div>

> [@postmodern](#):
>
> Is this the ideal way of filtering out empty Strings?

Well, I also dont know if there is a better way, I have used this feature in the past but the `experimental` warning on the docs made me leave a TODO on the code to investigate this a bit better.

---

<div class="post-metadata">

**Author:** ![postmodern](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/postmodern/32/696_2.png) [@postmodern](https://discourse.dry-rb.org/u/postmodern)\
**Post date:** [April 21, 2023, 3:11am UTC](https://discourse.dry-rb.org/t/how-to-remove-empty-strings-from-params-hash/1648/5 "2023-04-21T03:11:30Z")

</div>

How would I “DRY” up that `before(:value_coercer)` code if I needed to add it to multiple `Dry::Schema`s or `Dry::Validation:Contract`s? I suppose I could store the block as a constant and pass it in as `&BLOCK`. Since the actual schema block is evaluated within a `Dry::Schema::DSL` instance, I cannot call `include` to include Mixin modules that have `self.included` hooks. Would I define a Mixin `module` with a `self.extended` method and extend the Mixin `module` inside the `Dry::Schema`?

---

<div class="post-metadata">

**Author:** ![cpgo](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/cpgo/32/81_2.png) [@cpgo](https://discourse.dry-rb.org/u/cpgo)\
**Post date:** [May 3, 2023, 11:52pm UTC](https://discourse.dry-rb.org/t/how-to-remove-empty-strings-from-params-hash/1648/6 "2023-05-03T23:52:25Z")

</div>

I dont know the proper way of extracting that.  
This _really_ ugly (and probably wrong) monkey patch on `Dry::Schema::ProcessorSteps` apparently works but I’m positive it will break other stuff

```ruby
class Dry::Schema::ProcessorSteps
  reject_empty = [proc {|r| r.to_h.reject { |k,v| v.empty? }}]
  option :before_steps, default: -> { {value_coercer: reject_empty} }
end

schema = Dry::Schema.Params do
  required(:name).value(:string)
  optional(:age).value(:integer)
end

p schema.call(name: 'Jane', age: '')
#<Dry::Schema::Result{:name=>"Jane", :age=>""} errors={:age=>["must be an integer"]} path=[]>

```
