0


我有一个jsonapi端点,我在其中获取查询参数“include”,其中几个对象由“,”分隔 为了实现这一点,我根据文档制作了这个:

module CustomTypes
  include Dry::Types.module

  IncludeRelatedObject = Types::String.constructor do |itm|
    itm.split(',')&.map :chomp
  end
end

现在,当我运行我的测试时,我得到了这个错误:

失败/错误:IncludeRelatedObject = Types::String.constructor do |itm| itm.split(',')&.map :chomp end

NameError: 未初始化的常量 CustomTypes::Types

这是我的验证:

Dry::Validation.Params do
  configure do
    config.type_specs = true
  end
  optional(:include, CustomTypes::IncludeRelatedObject).each { :filled? & :str? }
end

任何想法我的代码有什么问题?

4

2 回答 2

3

include Dry::Types.module基本上将常量变形到它所包含的模块中。您得到CustomTypes::String了其他人,这是您的自定义类型中应该引用的内容:

module CustomTypes
  include Dry::Types.module

  # IncludeRelatedObject = Types::String.constructor do |itm|
  IncludeRelatedObject = CustomTypes::String.constructor do |itm|
    itm.split(',').map(&:chomp)
  end
end
于 2018-10-29T15:36:35.873 回答
-1

要为验证定义自定义类型,您应该使用 Types 模块。因此,您应该将模块名称从 更改CustomTypesTypes

module Types
  include Dry::Types.module

  IncludeRelatedObject = Types::String.constructor do |itm|
    itm.split(',')&.map :chomp
  end
end
于 2018-10-29T15:12:31.633 回答