dry-rb / dry-logic

Predicate logic with rule composition

Home Page:https://dry-rb.org/gems/dry-logic/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot get AST from rules defined in a dry-validation schema

kwando opened this issue · comments

schema = Dry::Validation.Form{
   required(:awesome).filled(:str?)
}
puts schema.rules[:awesome]
puts schema.rules[:awesome].inspect
puts schema.rules[:awesome].to_ast
key?(:awesome) AND key[awesome](filled?) AND key[awesome](str?)
#<Dry::Logic::Operations::And rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#key?> options={:args=>[:awesome], :id=>:awesome}>, #<Dry::Logic::Operations::And rules=[#<Dry::Logic::Operations::Key rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#filled?> options={:args=>[]}>] options={:name=>:awesome, :evaluator=>#<Dry::Logic::Evaluator::Key path=[:awesome]>, :path=>:awesome, :id=>:awesome}>, #<Dry::Logic::Operations::Key rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#str?> options={:args=>[]}>] options={:name=>:awesome, :evaluator=>#<Dry::Logic::Evaluator::Key path=[:awesome]>, :path=>:awesome, :id=>:awesome}>] options={:id=>:awesome}>] options={:id=>:awesome}>

NoMethodError: undefined method `[]' for Undefined:Object
./vendor/bundle/gems/dry-logic-0.4.0/lib/dry/logic/evaluator.rb:31:in `block in call'
./vendor/bundle/gems/dry-logic-0.4.0/lib/dry/logic/evaluator.rb:31:in `each'
./vendor/bundle/gems/dry-logic-0.4.0/lib/dry/logic/evaluator.rb:31:in `reduce'
./vendor/bundle/gems/dry-logic-0.4.0/lib/dry/logic/evaluator.rb:31:in `call'
./vendor/bundle/gems/dry-logic-0.4.0/lib/dry/logic/operations/key.rb:54:in `ast'
./vendor/bundle/gems/dry-logic-0.4.0/lib/dry/logic/operations/binary.rb:17:in `ast'
./vendor/bundle/gems/dry-logic-0.4.0/lib/dry/logic/operations/binary.rb:17:in `ast'
./vendor/bundle/gems/dry-logic-0.4.0/lib/dry/logic/operations/abstract.rb:39:in `to_ast'

I could "fix" this by patching Dry::Core::Undefined with the [] method returning self.

def undefined.[](_)
  self
end

This is probably not how this should be fixed though..

Serializing back to ast is not 100% implemented in dry-logic. This is planned for 1.0.0. I'm gonna close this one since it's a dry-logic issue (I'll report it there instead).

and 3 seconds later I realized it is a dry-logic repo c(ˊᗜˋ*c)

Yep, I figured it should be fixed in here somewhere :)

I'm reverse engineering the AST since I want to convert our in-house schema spec to a dry-validation schema.

...and over 2 years later, this now works with dry-schema + latest dry-logic:

key?(:awesome) AND key[awesome](filled? AND str?)#<Dry::Logic::Operations::And rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Dry::Logic::Predicates.key?> options={:args=>[:awesome]}>, #<Dry::Logic::Operatio
ns::Key rules=[#<Dry::Logic::Operations::And rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Dry::Logic::Predicates.filled?> options={:args=>[]}>, #<Dry::Logic
::Rule::Predicate predicate=#<Method: Dry::Logic::Predicates.str?> options={:args=>[]}>] options={}>] options={:name=>:awesome, :evaluator=>#<Dry::Logic::Evaluator::Key
 path=[:awesome]>, :path=>:awesome}>] options={}>
[:and, [[:predicate, [:key?, [[:name, :awesome], [:input, Undefined]]]], [:key, [:awesome, [:and, [[:predicate, [:filled?, [[:input, Undefined]]]], [:predicate, [:str?,
 [[:input, Undefined]]]]]]]]]]