-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Processor#merge which allows merging schemas
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require 'dry/schema/processor' | ||
|
||
RSpec.describe Dry::Schema::Processor, '#merge' do | ||
context 'without parents' do | ||
subject(:schema) { left.merge(right) } | ||
|
||
let(:left) do | ||
Dry::Schema.define do | ||
after(:rule_applier) do |result| | ||
result.output[:left] = true | ||
end | ||
|
||
required(:name).filled(:string) | ||
end | ||
end | ||
|
||
let(:right) do | ||
Dry::Schema.define do | ||
after(:rule_applier) do |result| | ||
result.output[:right] = true | ||
end | ||
|
||
required(:age).value(Types::Params::Integer) | ||
end | ||
end | ||
|
||
it 'maintains rules' do | ||
expect(schema.(name: '', age: 'foo').errors.to_h).to eql( | ||
name: ['must be filled'], age: ['must be an integer'] | ||
) | ||
end | ||
|
||
it 'maintains types' do | ||
expect(schema.(name: '', age: '36').errors.to_h).to eql( | ||
name: ['must be filled'] | ||
) | ||
end | ||
|
||
it 'maintains hooks' do | ||
expect(schema.(name: 'Jane', age: 36).to_h).to eql( | ||
name: 'Jane', age: 36, left: true, right: true | ||
) | ||
end | ||
end | ||
|
||
context 'with parents' do | ||
subject(:schema) { left.merge(right) } | ||
|
||
let(:left_parent) do | ||
Dry::Schema.define do | ||
required(:email).filled(:string) | ||
end | ||
end | ||
|
||
let(:left) do | ||
Dry::Schema.define(parent: left_parent) do | ||
required(:name).filled(:string) | ||
end | ||
end | ||
|
||
let(:right_parent) do | ||
Dry::Schema.define do | ||
required(:address).filled(:string) | ||
end | ||
end | ||
|
||
let(:right) do | ||
Dry::Schema.define(parent: right_parent) do | ||
required(:age).value(:integer) | ||
end | ||
end | ||
|
||
it 'maintains all rules' do | ||
expect(schema.(name: '', age: 'foo').errors.to_h).to eql( | ||
name: ['must be filled'], | ||
age: ['must be an integer'], | ||
email: ['is missing'], | ||
address: ['is missing'] | ||
) | ||
end | ||
end | ||
end |