2
class PostSchema < Dry::Validation::Contract
  params do
    required(:title).value(:string, size: 20)
    required(:content).value(:string, size: 50)
  end 
  
  rule do 
     # prevent this rule from executing if schema validation did not pass
  end
end

My current work around is to use result.schema_result.success?. Even this is working but I look at the source code at https://github.com/dry-rb/dry-validation/blob/master/lib/dry/validation/result.rb#L41. It is a private API. Does anyone have any idea about this?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Uysim Ty
  • 131
  • 2
  • 5

1 Answers1

1

A rule without any keys specified will always be executed. That's by design. If you want it to not be executed, simply provide which keys it depends on, ie:

rule(:title, :content) do
  # won't be executed unless both title and content passed
  # the schema checks
end
solnic
  • 5,733
  • 2
  • 23
  • 19