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
我目前的解决方法是使用 result.schema_result.success?
。即使这样也行得通,但我在 https://github.com/dry-rb/dry-validation/blob/master/lib/dry/validation/result.rb#L41 查看了源代码.它是一个私有(private) API。有人对此有任何想法吗?
最佳答案
没有指定任何键的规则将始终被执行。这是设计使然。如果您希望它不被执行,只需提供它所依赖的键,即:
rule(:title, :content) do
# won't be executed unless both title and content passed
# the schema checks
end
关于ruby - 如果模式验证未通过,如何防止干式验证执行规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57354801/