Hello,
I’ve been scratching my head for a couple hours but I can’t figure out how to do this:
require "dry-validation"
class MySchema < Dry::Validation::Contract
option :sizes, default: -> { %w[small medium large] }
params do
required(:size).value(included_in?: sizes)
end
end
# MySchema.new(sizes: %w[sm md lg])
I would like to do this because using Schema’s built-in predicates (included_in?
) means I don’t have to write a custom rule or message for the value to be included in a dynamic list of things.
This is the correct version:
require "dry-validation"
class MySchema < Dry::Validation::Contract
option :sizes, default: -> { %w[small medium large] }
params do
required(:size).value(:string)
end
rule(:size) do
key.failure("invalid size") unless sizes.include?(value)
end
end