# How to define a non-empty Array of non-empty Strings?

**URL:** <https://discourse.dry-rb.org/t/how-to-define-a-non-empty-array-of-non-empty-strings/1658>\
**Category:** dry-validation\
**Created:** [April 18, 2023, 8:23pm UTC](https://discourse.dry-rb.org/t/how-to-define-a-non-empty-array-of-non-empty-strings/1658 "2023-04-18T20:23:33Z")\
**Posts on this page:** 5\
**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 18, 2023, 8:23pm UTC](https://discourse.dry-rb.org/t/how-to-define-a-non-empty-array-of-non-empty-strings/1658/1 "2023-04-18T20:23:33Z")

</div>

Since I couldn’t quickly find an answer on Google or in the guides, what is the correct way to define a non-empty Array of non-empty Strings? I would to reject values such as `""`, `[]`, `[nil]`, and `[""]`.

This is what I’ve come up with, but I’m wondering if there’s a better way?

```ruby
class Validation < Dry::Validation::Contract

  params do
    required(:foo).filled(:array).each(:str?).each(:filled?)
  end

end

```

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [April 19, 2023, 7:51pm UTC](https://discourse.dry-rb.org/t/how-to-define-a-non-empty-array-of-non-empty-strings/1658/2 "2023-04-19T19:51:52Z")

</div>

If you are avoiding type constants, this seems like the shortest way:

```ruby
required(:foo).value(array[:string].constrained(min_size: 1)).each(:filled?)

```

It’s so useful to have a filled-string type that I always define one sooner or later

```ruby
Types = Dry.Types(default: :strict)

module Types
  FilledString = String.constrained(filled: true)
end

class Validation < Dry::Validation::Contract
  params do
    required(:foo).value(array[Types::FilledString].constrained(min_size: 1))
  end
end

```

---

<div class="post-metadata">

**Author:** ![fnordfish](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/fnordfish/32/715_2.png) [@fnordfish](https://discourse.dry-rb.org/u/fnordfish)\
**Post date:** [April 20, 2023, 9:46am UTC](https://discourse.dry-rb.org/t/how-to-define-a-non-empty-array-of-non-empty-strings/1658/3 "2023-04-20T09:46:36Z")

</div>

Or, construct a “array of non-empty strings”-Type and require foo to be filled to get the non-empty array:

```ruby
class Validation < Dry::Validation::Contract
  params do
    required(:foo).filled(Types::Array.of(Types::String.constrained(filled: true)))
  end
end

```

Which, as I realize now, is quite the same as @alassek wrote. Sorry for the noise. (I’m just more used to using `Types` everywhere)

---

<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 20, 2023, 9:43pm UTC](https://discourse.dry-rb.org/t/how-to-define-a-non-empty-array-of-non-empty-strings/1658/4 "2023-04-20T21:43:03Z")

</div>

Considering requiring a non-empty Array of non-empty Strings is probably fairly common, is there a built-in type specifically for this? Or perhaps, should there be a built-in type for this?

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)\
**Post date:** [May 9, 2023, 4:26pm UTC](https://discourse.dry-rb.org/t/how-to-define-a-non-empty-array-of-non-empty-strings/1658/5 "2023-05-09T16:26:55Z")

</div>

I think a custom rule would work better, especially since it can use custom error messages
