Using `filled?` with `each`

I can’t seem to get filled to work with each. I’m not sure if this should be supported or I’m doing it the wrong way.

Example:

require 'dry-validation'

schema = Dry::Validation.Schema do
  required(:foo).filled(:array?).each do
    schema do
      required(:bar).filled(:str?)
    end
  end
end

# 1.
p schema.({}).errors
# => {:foo=>["is missing"]}

# 2.
p schema.(foo: []).errors
# => {:foo=>["must be filled"]}

# 3.
p schema.(foo: [1]).errors
# => {}

# 4.
p schema.(foo: [{}]).errors
# => {}

# 5.
p schema.(foo: [{bar: 'baz'}]).errors
# => {}

With the above example I would expect 3. and 4. to return errors related to the internal schema. If I remove the filled(:array?) from the schema the internal schema validates as expected.

Cheers

This is a known limitation, chaining is not suported, so you need:

schema = Dry::Validation.Schema do
  required(:foo).value(:array?, :filled?) do
    each do
      schema do
        required(:bar).filled(:str?)
      end
    end
  end
end
1 Like

Awesome, thanks @solnic.

@solnic Hi! Is it possible to run the loop only when the array is filled?

schema = Dry::Validation.Schema do
  required(:foo).maybe(:array?) do
    each do
      schema do
        required(:bar).filled(:str?)
      end
    end
  end
end