Skip to content

Commit

Permalink
Better support for excluded_from?/included_in? predicates (close #483)
Browse files Browse the repository at this point in the history
Co-Authored-By: Erik Lövmo <[email protected]>
  • Loading branch information
flash-gordon and eriklovmo committed Jan 6, 2025
1 parent e1a940f commit 15d26d4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelog.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
- version: 1.14.0
date: 2025-01-03
added:
- 'Better support for sets in `excluded_from?` and `included_in?` predicates (via #480) (@flash-gordon)'
changed:
- "Set minimum Ruby version to 3.1 (@flash-gordon)"
- version: 1.13.4
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/schema/message_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def message_text(template, tokens, options)
def message_tokens(args)
tokens = args.each_with_object({}) do |arg, hash|
case arg[1]
when ::Array
when ::Array, ::Set
hash[arg[0]] = arg[1].join(LIST_SEPARATOR)
when ::Range
hash[:"#{arg[0]}_left"] = arg[1].first
Expand Down
14 changes: 13 additions & 1 deletion spec/integration/params/predicates/excluded_from_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

RSpec.describe "Predicates: Excluded From" do
context "with required" do
let(:list) { %w[1 3 5] }

subject(:schema) do
list = self.list

Dry::Schema.Params do
required(:foo).value(:string) { excluded_from?(%w[1 3 5]) }
required(:foo).value(:string) { excluded_from?(list) }
end
end

Expand All @@ -22,6 +26,14 @@
it "is not successful" do
expect(result).to be_failing ["is missing", "must be a string", "must not be one of: 1, 3, 5"]
end

context "when list is a set" do
let(:list) { Set.new(%w[1 3 5]) }

it "is not successful" do
expect(result).to be_failing ["is missing", "must be a string", "must not be one of: 1, 3, 5"]
end
end
end

context "with nil input" do
Expand Down
14 changes: 13 additions & 1 deletion spec/integration/params/predicates/included_in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

RSpec.describe "Predicates: Included In" do
context "with required" do
let(:list) { %w[1 3 5] }

subject(:schema) do
list = self.list

Dry::Schema.Params do
required(:foo) { included_in?(%w[1 3 5]) }
required(:foo) { included_in?(list) }
end
end

Expand All @@ -22,6 +26,14 @@
it "is not successful" do
expect(result).to be_failing ["is missing", "must be one of: 1, 3, 5"]
end

context "when list is a set" do
let(:list) { Set.new(%w[1 3 5]) }

it "is not successful" do
expect(result).to be_failing ["is missing", "must be one of: 1, 3, 5"]
end
end
end

context "with nil input" do
Expand Down

0 comments on commit 15d26d4

Please sign in to comment.