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 to execute if shema validation did not passed
  end
end

我目前的工作是使用 result.schema_result.success?。即使这个工作,但我查看源代码https://github.com/dry-rb/dry-validation/blob/master/lib/dry/validation/result.rb#L41。它是私有 api。有人对此有任何想法吗?

4

1 回答 1

1

将始终执行未指定任何键的规则。这是设计使然。如果您希望它不被执行,只需提供它所依赖的键,即:

rule(:title, :content) do
  # won't be executed unless both title and content passed
  # the schema checks
end
于 2019-10-21T09:26:57.630 回答