【问题标题】:Ruby on Rails: dry-schema dry-validation not validating presenceRuby on Rails:干模式干验证不验证存在
【发布时间】:2019-07-26 20:46:45
【问题描述】:
module QleKinds
  class CreateParamsValidator < MyCustomClass
    define do
      required(:title).value(:filled?)
    end
  end
end

我在 Rails 应用程序中使用干验证和干模式并实施建议以检查如何检查 title 属性的存在。尽管如此,在做了上述之后,它仍然说

#<Dry::Schema::Result{} errors={:title=>["is missing"]

但是,title 属性肯定包含在发送的原始参数中。有人有什么建议吗?

以下是发送的原始参数,其中明确包含“标题”:

[1] pry(#<Admin::QleKinds::CreateService>)> qle_kind_data
=> <ActionController::Parameters {"effective_on_kinds"=>["date_of_event"], "end_on"=>"06/01/2005", "event_kind_label"=>"Date of birth", "is_self_attested"=>"true", "market_kind"=>"shop", "post_event_sep_in_days"=>"1", "pre_event_sep_in_days"=>"1", "questions_attributes"=><ActionController::Parameters {"0"=><ActionController::Parameters {"answer_attributes"=><ActionController::Parameters {"responses_attributes"=><ActionController::Parameters {"0"=><ActionController::Parameters {"name"=>"true", "result"=>"contact_call_center"} permitted: true>, "1"=><ActionController::Parameters {"name"=>"false", "result"=>"contact_call_center"} permitted: true>, "2"=><ActionController::Parameters {"operator"=>"before", "value"=>"", "value_2"=>""} permitted: true>, "3"=><ActionController::Parameters {"name"=>"", "result"=>"proceed"} permitted: true>} permitted: true>} permitted: true>, "content"=>"When was Your Dog Born?", "type"=>"date"} permitted: true>} permitted: true>, "reason"=>"birth", "start_on"=>"06/01/1990", "title"=>"Got a New Dog", "tool_tip"=>"Household adds a new dog for emotional support"} permitted: true>
[2] pry(#<Admin::QleKinds::CreateService>)> qle_kind_data["title"]
=> "Got a New Dog"

【问题讨论】:

  • 发送的原始参数是什么?
  • 已更新参数。

标签: ruby-on-rails ruby dry-rb dry-validation


【解决方案1】:

不支持ActionController::Parameters,您需要先将它们强制为哈希。

【讨论】:

  • 谢谢,添加params.require("data").permit!.to_h 有效。
  • @solnic 我已经搜索了干验证文档,但没有相关信息。是否有任何文档如何在 Rails 中使用干式验证 gem?或者,gem 的设计主要是与 Hanami 框架一起使用?
  • @GregDan 还没有,但我计划启动一个 dry-rails 项目,将各种 Dry-rb gem 与 rails 集成,它将包括对dry-validation 的支持。
猜你喜欢
  • 1970-01-01
  • 2012-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多