Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/errors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ en:

type?: "must be %{type}"

respond_to?: "must respond to %{method}"

size?:
arg:
default: "size must be %{size}"
Expand Down
1 change: 1 addition & 0 deletions lib/dry/schema/macros/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DSL < Core

undef :eql?
undef :nil?
undef :respond_to?

# @!attribute [r] chain
# Indicate if the macro should append its rules to the provided trace
Expand Down
377 changes: 377 additions & 0 deletions spec/integration/schema/predicates/respond_to_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,377 @@
# frozen_string_literal: true

RSpec.describe 'Predicates: Respond To' do
context 'with required' do
subject(:schema) do
Dry::Schema.define { required(:foo) { respond_to?(:bar) } }
end

context 'with valid input' do
let(:input) { { foo: double(bar: 23) } }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with missing input' do
let(:input) { {} }

it 'is not successful' do
expect(result).to be_failing ['is missing', 'must respond to bar']
end
end

context 'with nil input' do
let(:input) { { foo: nil } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end

context 'with blank input' do
let(:input) { { foo: '' } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end

context 'with invalid input' do
let(:input) { { foo: double(baz: 23) } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end
end

context 'with optional' do
subject(:schema) do
Dry::Schema.define { optional(:foo) { respond_to?(:bar) } }
end

context 'with valid input' do
let(:input) { { foo: double(bar: 23) } }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with missing input' do
let(:input) { {} }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with nil input' do
let(:input) { { foo: nil } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end

context 'with blank input' do
let(:input) { { foo: '' } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end

context 'with invalid input' do
let(:input) { { foo: double(baz: 23) } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end
end

context 'as macro' do
context 'with required' do
context 'with value' do
subject(:schema) do
Dry::Schema.define { required(:foo).value(respond_to?: :bar) }
end

context 'with valid input' do
let(:input) { { foo: double(bar: 23) } }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with missing input' do
let(:input) { {} }

it 'is not successful' do
expect(result).to be_failing ['is missing', 'must respond to bar']
end
end

context 'with nil input' do
let(:input) { { foo: nil } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end

context 'with blank input' do
let(:input) { { foo: '' } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end

context 'with invalid input' do
let(:input) { { foo: double(baz: 23) } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end
end

context 'with filled' do
subject(:schema) do
Dry::Schema.define { required(:foo).filled(respond_to?: :bar) }
end

context 'with valid input' do
let(:input) { { foo: double(bar: 23) } }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with missing input' do
let(:input) { {} }

it 'is not successful' do
expect(result).to be_failing ['is missing', 'must respond to bar']
end
end

context 'with nil input' do
let(:input) { { foo: nil } }

it 'is not successful' do
expect(result).to be_failing ['must be filled', 'must respond to bar']
end
end

context 'with blank input' do
let(:input) { { foo: '' } }

it 'is not successful' do
expect(result).to be_failing ['must be filled', 'must respond to bar']
end
end

context 'with invalid input' do
let(:input) { { foo: double(baz: 23) } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end
end

context 'with maybe' do
subject(:schema) do
Dry::Schema.define { required(:foo).maybe(respond_to?: :bar) }
end

context 'with valid input' do
let(:input) { { foo: double(bar: 23) } }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with missing input' do
let(:input) { {} }

it 'is not successful' do
expect(result).to be_failing ['is missing', 'must respond to bar']
end
end

context 'with nil input' do
let(:input) { { foo: nil } }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with blank input' do
let(:input) { { foo: '' } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end

context 'with invalid input' do
let(:input) { { foo: double(baz: 23) } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end
end
end

context 'with optional' do
context 'with value' do
subject(:schema) do
Dry::Schema.define { optional(:foo).value(respond_to?: :bar) }
end

context 'with valid input' do
let(:input) { { foo: double(bar: 23) } }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with missing input' do
let(:input) { {} }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with nil input' do
let(:input) { { foo: nil } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end

context 'with blank input' do
let(:input) { { foo: '' } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end

context 'with invalid input' do
let(:input) { { foo: double(baz: 23) } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end
end

context 'with filled' do
subject(:schema) do
Dry::Schema.define { optional(:foo).filled(respond_to?: :bar) }
end

context 'with valid input' do
let(:input) { { foo: double(bar: 23) } }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with missing input' do
let(:input) { {} }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with nil input' do
let(:input) { { foo: nil } }

it 'is not successful' do
expect(result).to be_failing ['must be filled', 'must respond to bar']
end
end

context 'with blank input' do
let(:input) { { foo: '' } }

it 'is not successful' do
expect(result).to be_failing ['must be filled', 'must respond to bar']
end
end

context 'with invalid input' do
let(:input) { { foo: double(baz: 23) } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end
end

context 'with maybe' do
subject(:schema) do
Dry::Schema.define { optional(:foo).maybe(respond_to?: :bar) }
end

context 'with valid input' do
let(:input) { { foo: double(bar: 23) } }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with missing input' do
let(:input) { {} }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with nil input' do
let(:input) { { foo: nil } }

it 'is successful' do
expect(result).to be_successful
end
end

context 'with blank input' do
let(:input) { { foo: '' } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end

context 'with invalid input' do
let(:input) { { foo: double(baz: 23) } }

it 'is not successful' do
expect(result).to be_failing ['must respond to bar']
end
end
end
end
end
end