dry-rb/dry-validation

Missing way to set rules for nested arrays easily

Open

#745 geöffnet am 10. Dez. 2025

Auf GitHub ansehen
 (0 Kommentare) (1 Reaktion) (0 zugewiesene Personen)Ruby (193 Forks)github user discovery
bughelp wanted

Repository-Metriken

Stars
 (1.421 Stars)
PR-Merge-Metriken
 (Keine gemergten PRs in 30 T)

Beschreibung

Describe the bug

I'd like to validate something with structure like:

{
  foos: [
    {
      bars: [
        {some_entry: 1},
        {some_entry: 2}
      ]
    }
  ]
}

As far as I can tell, there's no good way to do that. I can't nest rules. There's a way to validate the nested entries with key paths, like in here: https://github.com/dry-rb/dry-validation/issues/732#issuecomment-1770474838 But this is getting really messy. At that point the rule is not really for :foos either - it's for something much deeper.

Expected behavior

I'd really like to either stack contracts, so that the rules can be attached there, like:

  class FooContract < Dry::Validation::Contract
    params do
      required(:bars).hash(BarContract)
    end
    rule(:some_entry) ...
  end

  class MainContract < Dry::Validation::Contract
    params do
      required(:foos).array { hash(FooContract) }
    end
  end

Or the ability to stack rules, so:

  class MainContract < Dry::Validation::Contract
    params do
      required(:foos).array { hash(FooParams) }
    end
    rule(:foos).each do |foo|
      rule(foos: foo).each do |bar|
        key(....)....
      end
    end
  end

My environment

  • Affects my production application: No

Contributor Guide