dry-rb/dry-validation

Macros do not support validation on root object

Open

#688 aperta il 12 ago 2021

Vedi su GitHub
 (1 commento) (1 reazione) (0 assegnatari)Ruby (193 fork)github user discovery
bughelp wanted

Metriche repository

Star
 (1421 star)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

Describe the bug

I extracted a macro into a reusable module, like this:

module ReusableRule
  extend ::ActiveSupport::Concern

  included do
    register_macro(:reusable_macro) do
      # do some validations here 
    end
  end
end

and use it like this:

class SomeContracr < ::Dry::Validation::Contract
  params do
    # define some params here
  end

  include ::ReusableRule
  rule(path_to: :inner_hash).validate(:reusable_macro)
end

which works fine.

But I cannot use it on the root hash, like this:

class SomeContracr < ::Dry::Validation::Contract
  params do
    # define some params here
  end

  include ::ReusableRule
  rule.validate(:reusable_macro)
end

Expected behavior

The macro should validate the root hash the same way as when I use it on an inner hash.

Actual behavior

dry-validation throws an ArgumentError

lib/dry/validation/values.rb:59:in `[]': +key+ must be a valid path specification (ArgumentError)

My environment

  • Ruby version: 3.0
  • OS: Docker image (Debian)

Suggestion

Probably adding

return data if args.size < 1

around here.

Guida contributor