Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate blocks raise an exception about missing messages #168

Closed
ThisIsMissEm opened this issue Apr 16, 2019 · 3 comments
Closed

validate blocks raise an exception about missing messages #168

ThisIsMissEm opened this issue Apr 16, 2019 · 3 comments
Assignees
Labels

Comments

@ThisIsMissEm
Copy link

Hi! I've the following code:

class MyValidator
  include Hanami::Validations::Form

  predicates Hanami::Validations::EmailPredicate
  validations do
    optional(:Properties).maybe(:hash?)
    validate(valid_properties: :Properties) do |rooms|
      properties.all? do |key, value|
        PropertyValidator.new(value).valid?
      end
    end
  end
end

Then else where I do: (there's a bit of additional abstraction, but it seems to not be important)

PropertyValidator.new(params).validate.success?

This then triggers the following error:

Dry::Validation::MissingMessageError (message for valid_properties was not found):

I've tried setting messages_path, and various other combinations of things that I've found between the Hanami::Validations and Dry::Validations READMEs and docs, but nothing seems to resolve this error.

What do I need to do in order to use the validate method in my validations block?

@ThisIsMissEm
Copy link
Author

I'm needing to use the validate block because I've a schema where I have arbitrary keys in the hash, as per: dry-rb/dry-schema#37

@jodosha
Copy link
Member

jodosha commented Jul 25, 2019

@ThisIsMissEm I can't reproduce the problem, as the validations context doesn't respond to properties, so it crashes. Any hint on how to fix this script? 👇

# frozen_string_literal: true

require "bundler/setup"
require "hanami/validations"
require "hanami/validations/form"

module EmailPredicate
  include Hanami::Validations::Predicates

  self.messages_path = "./errors.yml"

  predicate(:email?) do |current|
    current.match(/@/)
  end
end

class PropertyValidator
  include Hanami::Validations::Form

  validations do
  end
end

class MyValidator
  include Hanami::Validations::Form

  predicates EmailPredicate

  validations do
    optional(:Properties).maybe(:hash?)

    validate(valid_properties: :Properties) do |rooms|
      rooms.all? do |_key, value|
        PropertyValidator.new(value).valid?
      end
    end
  end
end

params = {}

PropertyValidator.new(params).validate.success?
MyValidator.new(params).validate.success?

__END__

ruby test.rb
Traceback (most recent call last):
	19: from test.rb:41:in `<main>'
	18: from /Users/luca/.gem/ruby/2.6.3/gems/hanami-validations-1.3.3/lib/hanami/validations.rb:365:in `validate'
	17: from /Users/luca/.gem/ruby/2.6.3/gems/dry-validation-0.11.2/lib/dry/validation/schema.rb:72:in `call'
	16: from /Users/luca/.gem/ruby/2.6.3/gems/dry-validation-0.11.2/lib/dry/validation/executor.rb:83:in `call'
	15: from /Users/luca/.gem/ruby/2.6.3/gems/dry-validation-0.11.2/lib/dry/validation/executor.rb:83:in `reduce'
	14: from /Users/luca/.gem/ruby/2.6.3/gems/dry-validation-0.11.2/lib/dry/validation/executor.rb:83:in `each'
	13: from /Users/luca/.gem/ruby/2.6.3/gems/dry-validation-0.11.2/lib/dry/validation/executor.rb:85:in `block in call'
	12: from /Users/luca/.gem/ruby/2.6.3/gems/dry-validation-0.11.2/lib/dry/validation/executor.rb:46:in `call'
	11: from /Users/luca/.gem/ruby/2.6.3/gems/dry-validation-0.11.2/lib/dry/validation/executor.rb:46:in `each_with_object'
	10: from /Users/luca/.gem/ruby/2.6.3/gems/dry-validation-0.11.2/lib/dry/validation/executor.rb:46:in `each'
	 9: from /Users/luca/.gem/ruby/2.6.3/gems/dry-validation-0.11.2/lib/dry/validation/executor.rb:47:in `block in call'
	 8: from /Users/luca/.gem/ruby/2.6.3/gems/dry-validation-0.11.2/lib/dry/validation/executor.rb:47:in `each'
	 7: from /Users/luca/.gem/ruby/2.6.3/gems/dry-validation-0.11.2/lib/dry/validation/executor.rb:48:in `block (2 levels) in call'
	 6: from /Users/luca/.gem/ruby/2.6.3/gems/dry-validation-0.11.2/lib/dry/validation/schema_compiler.rb:14:in `call'
	 5: from /Users/luca/.gem/ruby/2.6.3/gems/dry-logic-0.4.2/lib/dry/logic/operations/check.rb:33:in `call'
	 4: from /Users/luca/.gem/ruby/2.6.3/gems/dry-logic-0.4.2/lib/dry/logic/rule.rb:47:in `call'
	 3: from /Users/luca/.gem/ruby/2.6.3/gems/dry-logic-0.4.2/lib/dry/logic/rule.rb:51:in `[]'
	 2: from /Users/luca/.gem/ruby/2.6.3/gems/dry-logic-0.4.2/lib/dry/logic/rule.rb:69:in `block in bind'
	 1: from /Users/luca/.gem/ruby/2.6.3/gems/dry-logic-0.4.2/lib/dry/logic/rule.rb:69:in `instance_exec'
   test.rb:33:in `block (2 levels) in <class:MyValidator>': undefined local variable or method `properties' for #<#<Class:0x00007f88b59d7a98>:0x00007f88b68c17c0> (NameError)

@jodosha jodosha self-assigned this Jul 25, 2019
@adam12
Copy link
Member

adam12 commented Feb 15, 2021

Closing due to lack of feedback. If this is still an issue, please provide an update so we can re-open this issue.

@adam12 adam12 closed this as completed Feb 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants