以干验证为例:
require "dry-validation"
module Types
include Dry::Types.module
Name = Types::String.constructor do |str|
str ? str.strip.chomp : str
end
end
SignUpForm = Dry::Validation.Params do
configure do
config.type_specs = true
end
required(:code, Types::StrictString).filled(max_size?: 4)
required(:name, Types::Name).filled(min_size?: 1)
required(:password, :string).filled(min_size?: 6)
end
result = SignUpForm.call(
"name" => "\t François \n",
"code" => "francois",
"password" => "some password")
result.success?
# true
# This is what I WANT
result[:code]
# "fran"
我想创建一个新类型,StrictString
它将使用谓词信息,喜欢max_size
并截断它。
问题:我无权访问Types::String.constructor
. 如果我反过来,即通过自定义谓词,我不能只返回真或假,我看不到如何更改参数。
我想用霰弹枪杀死一只苍蝇吗?